我正在开发在 Win10 pro 上运行的 MS Edge(版本 122.0.2365.80)的扩展。扩展程序一直可以正常工作,但现在不再工作了。 我在 javascript 中使用 chrome.downloads.download ,它用来执行的操作是调用指向最后保存到文件夹的“另存为”对话框。现在它指向浏览器的默认目录。 还有其他人看到这个吗? chrome.downloads.download 中是否有一个选项可以将 SaveAs 指向正确的方向? 这是background.js的代码:
chrome.runtime.onInstalled.addListener(function() {
console.log('Extension installed / updated');
});
chrome.runtime.onMessage.addListener(function (request, sender) {
if (request.type === 'popupMessage') {
// Access parameters from the content script
var receivedParams = request.data;
// Do something with the parameters
console.log(receivedParams);
chrome.downloads.download({
url: receivedParams,
filename: 'desired_filename.ext',
saveAs: true
}, function(downloadId) {
console.log('Download initiated with ID:', downloadId);
});
}
});
预先感谢您提供的任何指示。
当我调用background.js时,我希望它能打开指向最后保存到目录的“另存为”对话框。 相反,代码(未更改)现在会显示指向浏览器默认目录的“另存为”对话框。
Sooooo,事实证明,对于扩展来说,完成工作的方法不止一种。 就我而言,它使用了完全不同的 API。您不再使用 chrome.downloads.download 调用,该调用必须隐藏在 backround.js“worker”文件中。相反,您可以使用主 JavaScript 中的异步函数,使用以下代码:
async function saveImage(currentURL) {try{
// Fetch the image file
const response = await fetch(currentURL);
const blob = await response.blob();
const destination = currentURL.split("/").reverse()[0];
// Request the user to choose where to save the file
const handle = await window.showSaveFilePicker({suggestedName: destination});
// Create a writable stream to the file
const writableStream = await handle.createWritable();
// Write the image data to the file
await writableStream.write(blob);
// Close the file and save changes
await writableStream.close();
} catch(err){alert(err);}}