Files
webextensions-examples/latest-download/popup/latest_download.js
rebloor 82217a05bc Replace var with let in examples (#484)
* Replace var with let in examples

store-collected-images/webextension-plain/deps/uuidv4.js

* Reverted third–party code
2022-08-11 04:27:28 +12:00

71 lines
2.0 KiB
JavaScript

let latestDownloadId;
/*
Callback from getFileIcon.
Initialize the displayed icon.
*/
function updateIconUrl(iconUrl) {
let downloadIcon = document.querySelector("#icon");
downloadIcon.setAttribute("src", iconUrl);
}
function onError(error) {
console.log(`Error: ${error}`);
}
/*
If there was a download item,
- remember its ID as latestDownloadId
- initialize the displayed icon using getFileIcon
- initialize the displayed URL
If there wasn't a download item, disable the "open" and "remove" buttons.
*/
function initializeLatestDownload(downloadItems) {
let downloadUrl = document.querySelector("#url");
if (downloadItems.length > 0) {
latestDownloadId = downloadItems[0].id;
let gettingIconUrl = browser.downloads.getFileIcon(latestDownloadId);
gettingIconUrl.then(updateIconUrl, onError);
downloadUrl.textContent = downloadItems[0].url;
document.querySelector("#open").classList.remove("disabled");
document.querySelector("#remove").classList.remove("disabled");
} else {
downloadUrl.textContent = "No downloaded items found."
document.querySelector("#open").classList.add("disabled");
document.querySelector("#remove").classList.add("disabled");
}
}
/*
Search for the most recent download, and pass it to initializeLatestDownload()
*/
let searching = browser.downloads.search({
limit: 1,
orderBy: ["-startTime"]
});
searching.then(initializeLatestDownload);
/*
Open the item using the associated application.
*/
function openItem() {
if (!document.querySelector("#open").classList.contains("disabled")) {
browser.downloads.open(latestDownloadId);
}
}
/*
Remove item from disk (removeFile) and from the download history (erase)
*/
function removeItem() {
if (!document.querySelector("#remove").classList.contains("disabled")) {
browser.downloads.removeFile(latestDownloadId);
browser.downloads.erase({id: latestDownloadId});
window.close();
}
}
document.querySelector("#open").addEventListener("click", openItem);
document.querySelector("#remove").addEventListener("click", removeItem);