diff --git a/beastify/README.md b/beastify/README.md index 941189a..c5c8288 100644 --- a/beastify/README.md +++ b/beastify/README.md @@ -1,7 +1,5 @@ # beastify -**This add-on injects JavaScript into web pages. The `addons.mozilla.org` domain disallows this operation, so this add-on will not work properly when it's run on pages in the `addons.mozilla.org` domain.** - ## What it does ## The extension includes: @@ -13,8 +11,9 @@ The extension includes: 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 +When it is shown, the popup injects a content script into the current page. + +When the user chooses a beast, the extension 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 @@ -22,6 +21,14 @@ content with an image of the chosen beast. When the user clicks the reset button, the page reloads, and reverts to its original form. +Note that: + +* if the user reloads the tab, or switches tabs, while the popup is open, then the popup won't be able to beastify the page any more (because the content script was injected into the original tab). + +* by default [`tabs.executeScript()`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/executeScript) injects the script only when the web page and its resources have finished loading. This means that clicks in the popup will have no effect until the page has finished loading. + +* it's not possible to inject content scripts into certain pages, including privileged browser pages like "about:debugging" and the [addons.mozilla.org](https://addons.mozilla.org/) website. If the user clicks the beastify icon when such a page is loaded into the active tab, the popup displays an error message. + ## What it shows ## * write a browser action with a popup diff --git a/beastify/popup/choose_beast.css b/beastify/popup/choose_beast.css index e35ca25..db4c5a5 100644 --- a/beastify/popup/choose_beast.css +++ b/beastify/popup/choose_beast.css @@ -2,6 +2,10 @@ html, body { width: 100px; } +.hidden { + display: none; +} + .button { margin: 3% auto; padding: 4px; diff --git a/beastify/popup/choose_beast.html b/beastify/popup/choose_beast.html index 83b49f4..5f68622 100644 --- a/beastify/popup/choose_beast.html +++ b/beastify/popup/choose_beast.html @@ -7,10 +7,15 @@ -
Frog
-
Turtle
-
Snake
-
Reset
+ + diff --git a/beastify/popup/choose_beast.js b/beastify/popup/choose_beast.js index 2401edc..0f2da48 100644 --- a/beastify/popup/choose_beast.js +++ b/beastify/popup/choose_beast.js @@ -7,11 +7,10 @@ const hidePage = `body > :not(.beastify-image) { }`; /** - * When the popup loads, inject a content script into the active tab, - * and add a clck handler. + * Listen for clicks on the buttons, and send the appropriate message to + * the content script in the page. */ -browser.tabs.executeScript({file: "/content_scripts/beastify.js"}) -.then(() => { +function listenForClicks() { document.addEventListener("click", (e) => { /** @@ -77,5 +76,22 @@ browser.tabs.executeScript({file: "/content_scripts/beastify.js"}) .catch(reportError); } }); - -}); +} + +/** + * There was an error executing the script. + * Display the popup's error message, and hide the normal UI. + */ +function reportExecuteScriptError() { + document.querySelector("#popup-content").classList.add("hidden"); + document.querySelector("#error-content").classList.remove("hidden"); +} + +/** + * When the popup loads, inject a content script into the active tab, + * and add a click handler. + * If we couldn't inject the script, handle the error. + */ +browser.tabs.executeScript({file: "/content_scripts/beastify.js"}) +.then(listenForClicks) +.catch(reportExecuteScriptError);