I’m using Firefox 56.0 under Linux Mint 18.1 (64 bit)
This simple add-on waits until you download a file, then creates a new file called “url.txt” in the standard Downloads directory.
“url.txt” contains the referrer and url of the download.
manifest.json
{
“manifest_version”: 2,
“name”: “Download Test”,
“version”: “1.0.0”,
“description”: “Test DownloadItem.byExtensionId in downloads.onCreated”,
"background": {
"scripts": ["background.js"]
},
"permissions": [
"downloads"
]
}
background.js
var addon_id;
function handle_onCreated(downloadItem) {
console.log(downloadItem.byExtensionId + " / " + addon_id);
if (downloadItem.byExtensionId !== addon_id) {
var blob = new Blob([downloadItem.referrer, "\n", downloadItem.url], {type: "text/plain", endings: "native"});
var objectURL = URL.createObjectURL(blob);
var downloading = browser.downloads.download(
{
filename: "url.txt",
url: objectURL
}
);
}
}
addon_id = browser.runtime.id;
browser.downloads.onCreated.addListener(handle_onCreated);
Problems with this code
When I download a file, the add-on creates a blob and"downloads" it.
This causes a new onCreated event --> infinite loop.
The line “if (downloadItem.byExtensionId !== addon_id)” is supposed to prevent this.
But downloadItem.byExtensionId is always undefined.
Am I doing something wrong when creating the blob download, or is this a bug in Firefox?
edit:
what’s the best way to quote code on this forum?