Chrome扩展弹窗页面下载链接失效问题求助
Hey there, sorry to hear your download link stopped working out of nowhere—let’s break down the likely causes and fixes, since Chrome’s been tweaking security policies that can affect this sort of thing.
1. Chrome’s Security Update Blocked Data URI Downloads in Extension Popups
Over the past few months, Chrome tightened restrictions on data: URI downloads initiated from extension popups. Direct <a download> links using data URIs might now be silently blocked without error logs, which matches exactly what you’re seeing.
The Fix: Use the Official chrome.downloads API
This is the recommended, reliable way to handle downloads in extensions, and it avoids the data URI limitations entirely. Here’s how to switch over:
Step 1: Add the downloads Permission to Your Manifest
Update your manifest.json to include the required permission for download operations:
{ "manifest_version": 3, "permissions": ["downloads"], // ... rest of your extension manifest content }
Step 2: Replace the <a> Tag with a Programmatic Download
Swap your existing download link for a button (or keep the link but override its click behavior), then use the API to trigger the download explicitly:
Popup HTML:
<button id="downloadBtn">Click me</button>
Popup JavaScript:
document.getElementById('downloadBtn').addEventListener('click', () => { // Your target file content (remove any unexpected null characters like \00 if present) const fileContent = 'testtest1234567!@#$%^&*()_+'; const blob = new Blob([fileContent], { type: 'text/plain;charset=utf-8' }); const blobUrl = URL.createObjectURL(blob); chrome.downloads.download({ url: blobUrl, filename: 'test.txt', conflictAction: 'uniquify' // Optional: automatically renames duplicate files }, (downloadId) => { if (chrome.runtime.lastError) { console.error('Download failed:', chrome.runtime.lastError.message); } else { console.log('Download started successfully (ID:', downloadId, ')'); } // Clean up the blob URL to free up memory URL.revokeObjectURL(blobUrl); }); });
2. Check for Invalid Characters in Your Data URI
Looking at your example link, there’s a \00 (null character) at the end of the data string. Chrome might have started flagging control characters in data URIs as unsafe recently. Try removing that null character first—even if you switch to the API, invalid content could cause unexpected issues.
3. Verify Popup Lifecycle Doesn’t Interrupt the Download
If your popup closes immediately after the click, it might terminate the download process prematurely. Make sure your click handler doesn’t trigger a popup close before the download is initiated. The chrome.downloads API runs in the extension background, so this is less of an issue than with a direct link, but it’s still worth double-checking.
Why This Works
The chrome.downloads API is built specifically for extension download operations, so it’s not subject to the same security restrictions as arbitrary data URI links. It also gives you better control over the process—like handling duplicate filenames, tracking download progress, and getting clear error feedback if something goes wrong.
Give these steps a try—this should get your download functionality working again.
内容的提问来源于stack exchange,提问作者thusimon

