[英]chrome.downloads.download not working from background service worker
我正在嘗試創建一個 Chrome 擴展程序,它將在單擊工具欄按鈕時對頁面內容進行一些分析,然后將結果保存(即下載)到用戶的計算機上。 單擊按鈕時我可以進行分析,但無法觸發下載。
這是我第一次編寫 Chrome 擴展程序,所以如果能幫助我解決為什么它不起作用,我們將不勝感激!
清單.json
{
...
"permissions": [
"activeTab",
"downloads",
"scripting"
],
"background": {
"service_worker": "background.js"
}
}
背景.js
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['analysis.js']
});
});
chrome.runtime.onMessage.addListener((arg, sender, reply) => {
chrome.downloads.download({
url: 'data:text/plain,' + arg.text,
filename: arg.name,
saveAs: true
})
});
分析.js
// Do some analysis here
chrome.runtime.sendMessage({name: "results.txt", text: "ANALYSIS RESULTS HERE"});
當我單擊工具欄按鈕時,分析開始運行,但我沒有看到“保存”對話框,也沒有任何文件出現在磁盤上。 我究竟做錯了什么?
不確定您是否解決了這個問題或者此修復程序是否解決了您的問題,但我剛剛發現如果我在常規 Chrome 設置中取消選中“下載前詢問每個文件的保存位置”,則下載 API 僅適用於我。 我的解決方法(我認為它只適用於 Manifest v3)是使用活動選項卡中的錨標記強制下載:
const anchor = document.createElement('a');
anchor.href = filedata;
anchor.download = filename;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
這似乎是 chrome manifest V3 中的一個錯誤。 詳情見下文。
TL&DL, chrome.downloads.download
在清單 V3 中曾經並且仍然(截至 2022 年 5 月)完全損壞。 在清單 V2 上,它似乎有效。
--
問題 1246717:chrome.downloads.download 從未啟動,但沒有錯誤
chrome.downloads.download 從不啟動,但沒有錯誤
https://bugs.chromium.org/p/chromium/issues/detail?id=1246717&
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.