diff --git a/latest-download/README.md b/latest-download/README.md new file mode 100644 index 0000000..26443f1 --- /dev/null +++ b/latest-download/README.md @@ -0,0 +1,27 @@ +# beastify + +## What it does ## + +The extension includes: + +* a browser action with a popup including HTML, CSS, and JS +* a content script +* three images, each of a different beast, packaged as web accessible resources + +When the user clicks the browser action button, the popup is shown, enabling +the user to choose one of three beasts. + +When they choose a beast, the extension injects the content script into +the current page, and sends the content script a message containing +the name of the chosen beast. + +When the content script receives this message, it replaces the current page +content with an image of the chosen beast. + +## What it shows ## + +* write a browser action with a popup +* give the popup style and behavior using CSS and JS +* inject a content script programmatically using `tabs.executeScript()` +* send a message from the main extension to a content script +* use web accessible resources to enable web pages to load packaged content diff --git a/latest-download/icons/LICENSE b/latest-download/icons/LICENSE new file mode 100644 index 0000000..20e821d --- /dev/null +++ b/latest-download/icons/LICENSE @@ -0,0 +1,2 @@ + +The "page-32.png" and "page-48.png" icons are taken from the miu iconset created by Linh Pham Thi Dieu, and are used under the terms of its license: http://linhpham.me/miu/. diff --git a/latest-download/icons/page-32.png b/latest-download/icons/page-32.png new file mode 100644 index 0000000..dae663d Binary files /dev/null and b/latest-download/icons/page-32.png differ diff --git a/latest-download/icons/page-48.png b/latest-download/icons/page-48.png new file mode 100644 index 0000000..ba042cd Binary files /dev/null and b/latest-download/icons/page-48.png differ diff --git a/latest-download/manifest.json b/latest-download/manifest.json new file mode 100644 index 0000000..4b92ff3 --- /dev/null +++ b/latest-download/manifest.json @@ -0,0 +1,30 @@ +{ + + "description": "Shows the last downloaded item, and lets you open or delete it", + "manifest_version": 2, + "name": "latest-download", + "version": "1.0", + "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/latest-download", + "icons": { + "48": "icons/page-48.png" + }, + + "applications": { + "gecko": { + "id": "latest-download@mozilla.org", + "strict_min_version": "48.0" + } + }, + + "permissions": [ + "downloads", + "downloads.open" + ], + + "browser_action": { + "default_icon": "icons/page-32.png", + "default_title": "Latest download", + "default_popup": "popup/latest_download.html" + } + +} diff --git a/latest-download/popup/latest_download.css b/latest-download/popup/latest_download.css new file mode 100644 index 0000000..48393cc --- /dev/null +++ b/latest-download/popup/latest_download.css @@ -0,0 +1,8 @@ +html, body { + font: "Open Sans",Arial,sans-serif; + height: 150px; +} + +#download-entry > * { + padding: 0.5em; +} diff --git a/latest-download/popup/latest_download.html b/latest-download/popup/latest_download.html new file mode 100644 index 0000000..3278ccc --- /dev/null +++ b/latest-download/popup/latest_download.html @@ -0,0 +1,20 @@ + + + + + + + + + + +
+

+  
+ + + + + + + diff --git a/latest-download/popup/latest_download.js b/latest-download/popup/latest_download.js new file mode 100644 index 0000000..63dde71 --- /dev/null +++ b/latest-download/popup/latest_download.js @@ -0,0 +1,65 @@ + +var latestDownloadId; + +/* +Callback from getFileIcon. +Log an error, or initialize the displayed icon. +*/ +function updateIconUrl(iconUrl) { + 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) { + if (downloadItems.length > 0) { + latestDownloadId = downloadItems[0].id; + chrome.downloads.getFileIcon(latestDownloadId, updateIconUrl); + var downloadUrl = document.querySelector("#url"); + downloadUrl.textContent = downloadItems[0].url; + } else { + document.querySelector("#open").disabled = true; + document.querySelector("#remove").disabled = true; + } +} + +/* +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() { + chrome.downloads.open(latestDownloadId); + window.close(); +} + +/* +Remove item from disk (removeFile) and from the download history (erase) +*/ +function removeItem() { + chrome.downloads.removeFile(latestDownloadId); + chrome.downloads.erase({id: latestDownloadId}); + window.close(); +} + +var openItemButton = document.querySelector("#open"); +openItemButton.addEventListener("click", openItem); + +var removeItemButton = document.querySelector("#remove"); +removeItemButton.addEventListener("click", removeItem);