mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-17 23:08:33 +02:00
74 lines
2.1 KiB
JavaScript
74 lines
2.1 KiB
JavaScript
|
|
var latestDownloadId;
|
|
|
|
/*
|
|
Callback from getFileIcon.
|
|
Log an error, or initialize the displayed icon.
|
|
*/
|
|
function updateIconUrl(iconUrl) {
|
|
/*
|
|
If there was an error getting the icon URL,
|
|
then lastError will be set. So check lastError
|
|
and handle it.
|
|
*/
|
|
if (chrome.runtime.lastError) {
|
|
console.error(chrome.runtime.lastError);
|
|
iconUrl = "";
|
|
}
|
|
var downloadIcon = document.querySelector("#icon");
|
|
downloadIcon.setAttribute("src", iconUrl);
|
|
}
|
|
|
|
/*
|
|
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) {
|
|
var downloadUrl = document.querySelector("#url");
|
|
if (downloadItems.length > 0) {
|
|
latestDownloadId = downloadItems[0].id;
|
|
chrome.downloads.getFileIcon(latestDownloadId, updateIconUrl);
|
|
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()
|
|
*/
|
|
chrome.downloads.search({
|
|
limit: 1,
|
|
orderBy: ["-startTime"]
|
|
}, initializeLatestDownload);
|
|
|
|
/*
|
|
Open the item using the associated application.
|
|
*/
|
|
function openItem() {
|
|
if (!document.querySelector("#open").classList.contains("disabled")) {
|
|
chrome.downloads.open(latestDownloadId);
|
|
}
|
|
}
|
|
|
|
/*
|
|
Remove item from disk (removeFile) and from the download history (erase)
|
|
*/
|
|
function removeItem() {
|
|
if (!document.querySelector("#remove").classList.contains("disabled")) {
|
|
chrome.downloads.removeFile(latestDownloadId);
|
|
chrome.downloads.erase({id: latestDownloadId});
|
|
window.close();
|
|
}
|
|
}
|
|
|
|
document.querySelector("#open").addEventListener("click", openItem);
|
|
document.querySelector("#remove").addEventListener("click", removeItem);
|