From 5d7441e02d2674c0aaf40f363442ad61a9dc5032 Mon Sep 17 00:00:00 2001 From: Will Bamberg Date: Wed, 4 May 2016 15:55:31 -0700 Subject: [PATCH] New example add-on: latest-download --- latest-download/README.md | 27 +++++++++ latest-download/icons/LICENSE | 2 + latest-download/icons/page-32.png | Bin 0 -> 344 bytes latest-download/icons/page-48.png | Bin 0 -> 310 bytes latest-download/manifest.json | 30 ++++++++++ latest-download/popup/latest_download.css | 8 +++ latest-download/popup/latest_download.html | 20 +++++++ latest-download/popup/latest_download.js | 65 +++++++++++++++++++++ 8 files changed, 152 insertions(+) create mode 100644 latest-download/README.md create mode 100644 latest-download/icons/LICENSE create mode 100644 latest-download/icons/page-32.png create mode 100644 latest-download/icons/page-48.png create mode 100644 latest-download/manifest.json create mode 100644 latest-download/popup/latest_download.css create mode 100644 latest-download/popup/latest_download.html create mode 100644 latest-download/popup/latest_download.js 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 0000000000000000000000000000000000000000..dae663dbb4989c607cb29de15c2d5300060c459d GIT binary patch literal 344 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdzmSQK*5Dp-y;YjHK@;M7UB8wRq zxP?KOkzv*x383IFPZ!6Kh}O5)?7f%+1&)984`Ja~^yE|2Fx%-6#VyUBIMq6$fry* z{)J8Fd3GP*J~orN!QCe1*}CZ82Uh4?Y-tpd$}%(TVU<-8+kGtZ$xq&UR(}?6>*N%R z?OZ7x(RII8_N44~Um5j%-1W&1s|yC9yDm!jKi9ar+^ zNc`vx(*!cU_)S_JTgN^rL~;dB2%8r3)W%Z|sSAt*ZXekB?EQ`wKU1GS7wQxq3b@a* kp0>Z(uE>6;pio#-$q%07HSn)78&qol`;+0OH(%rvLx| literal 0 HcmV?d00001 diff --git a/latest-download/icons/page-48.png b/latest-download/icons/page-48.png new file mode 100644 index 0000000000000000000000000000000000000000..ba042cdcd5f88b6666d52e2ff60c3c963bc5ac35 GIT binary patch literal 310 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpTCmSQK*5Dp-y;YjHK@;M7UB8wRq zxP?KOkzv*x383H&PZ!6KjK;S!y}1rK2(Wq!+J}Fc($%%7d_v6O)^v&R%>f)IRW4U{ ztzR(xvhCEgduj>$=CgS?mNJ`NWD1;qzO;ARiRMMW)E+RcVNTn_+x2SpYo7^6LYO!$ z7+0@w46L<(?UCAP)_Cc7RMEeHU?;^14YnJ4L?lj6&g+crbqr*Re&G1?Sy!jf3Zrtn zgRV}IJr>c`+M5;I|5Y(J?~BN-t78@mcq|eGM9GY%eu^N3MIiVSo5+6-tCyP_)&7XR fz0 * { + 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);