lars.b
(lars.beiderbecke@gmail.com)
April 30, 2022, 11:46am
1
For an extension, I’m trying to use browser.downloads in a background script like this:
browser.tabs.executeScript(tabId, {file: "foo.js"}).then(
function (info) {
browser.downloads.download({
url: info.url,
filename: info.name.
}).then(...)
});
This fails with browser.downloads undefined . From a post in this forum regarding browser.downloads undefined I’ve learned that I cannot use browser.downloads in content scripts, but why does this restriction apply to my case?
And what would be the correct way to do this? The post suggests using a background script, which I’m already doing.
lars.b
(lars.beiderbecke@gmail.com)
April 30, 2022, 12:52pm
3
This is … strange. Maybe I abstracted too much? How about this code?
async function foo() {
for (var i = 0; i < count; ++i) {
let tabs = await browser.tabs.query({currentWindow: true, index: i});
let info = await browser.tabs.executeScript(tabId, {file: "foo.js"});
bar(tabs, info);
};
}
async function bar(tabs, info) {
browser.downloads.download({
url: info.url,
filename: info.name
});
}
yields Uncaught (in promise) TypeError: browser.downloads is undefined .
1 Like
lars.b
(lars.beiderbecke@gmail.com)
April 30, 2022, 1:02pm
4
EDIT: There’s an even more exhaustive call stack:
TypeError: browser.downloads is undefined
fun3 moz-extension://123/file.js:68
fun2 moz-extension://123/file.js:49
AsyncFunctionNextself-hosted:638
fun1/< moz-extension://123/file.js:13
fun1 moz-extension://123/file.js:10
applyself-hosted:2675
applySafeWithoutCloneresource://gre/modules/ExtensionCommon.jsm:694
fireresource://gre/modules/ExtensionChild.jsm:826
withHandlingUserInputresource://gre/modules/ExtensionCommon.jsm:111
recvRunListenerresource://gre/modules/ExtensionChild.jsm:829
recvRunListenerself-hosted:1121
_recvresource://gre/modules/ConduitsChild.jsm:82
receiveMessageresource://gre/modules/ConduitsChild.jsm:188
Maybe you are missing downloads
permission in your manifest file?
lars.b
(lars.beiderbecke@gmail.com)
May 1, 2022, 7:42am
6
Thanks for your suggestion, that was indeed the case. My apologies, I totally missed that permission requirement in the MDN docs.
But I also didn’t think that a missing permission would result in something being undefined rather than rejected or forbidden .
Yeah, it’s a bit strange but this is how all permissions works.
So, if you don’t see some API, it usually means you don’t have the permission or the API is not available in the browser or the API is not available in the current context (like in a content script).