Files
webextensions-examples/beastify/popup/choose_beast.js
Will Bamberg 3bdf6a97cc Fix comment
2017-07-08 09:50:04 -07:00

55 lines
1.5 KiB
JavaScript

/*
Given the name of a beast, get the URL to the corresponding image.
*/
function beastNameToURL(beastName) {
switch (beastName) {
case "Frog":
return browser.extension.getURL("beasts/frog.jpg");
case "Snake":
return browser.extension.getURL("beasts/snake.jpg");
case "Turtle":
return browser.extension.getURL("beasts/turtle.jpg");
}
}
/*
Listen for clicks in the popup.
If the click is on one of the beasts:
(1) inject the beastify.js content script.
(2) send a message to "beastify.js" that contains the URL to
the chosen beast's image.
If it's on a button which contains class "clear":
Reload the page.
Close the popup. This is needed, as the content script malfunctions after page reloads.
*/
document.addEventListener("click", (e) => {
function messageBeastify() {
var chosenBeast = e.target.textContent;
var chosenBeastURL = beastNameToURL(chosenBeast);
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
gettingActiveTab.then((tabs) => {
browser.tabs.sendMessage(tabs[0].id, {beastURL: chosenBeastURL});
});
}
function reportError(error) {
console.error(`Could not inject content script: ${error}`);
}
if (e.target.classList.contains("beast")) {
browser.tabs.executeScript({
file: "/content_scripts/beastify.js"
}).then(messageBeastify)
.catch(reportError);
}
else if (e.target.classList.contains("clear")) {
browser.tabs.reload();
window.close();
return;
}
});