mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
49 lines
1.4 KiB
JavaScript
49 lines
1.4 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:
|
|
Inject the "beastify.js" content script in the active tab.
|
|
|
|
Then get the active tab and send "beastify.js" a message
|
|
containing the URL to the chosen beast's image.
|
|
|
|
If it's on a button wich contains class "clear":
|
|
Reload the page.
|
|
Close the popup. This is needed, as the content script malfunctions after page reloads.
|
|
*/
|
|
document.addEventListener("click", (e) => {
|
|
if (e.target.classList.contains("beast")) {
|
|
var chosenBeast = e.target.textContent;
|
|
var chosenBeastURL = beastNameToURL(chosenBeast);
|
|
|
|
browser.tabs.executeScript(null, {
|
|
file: "/content_scripts/beastify.js"
|
|
});
|
|
|
|
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
|
|
gettingActiveTab.then((tabs) => {
|
|
browser.tabs.sendMessage(tabs[0].id, {beastURL: chosenBeastURL});
|
|
});
|
|
}
|
|
else if (e.target.classList.contains("clear")) {
|
|
browser.tabs.reload();
|
|
window.close();
|
|
|
|
return;
|
|
}
|
|
});
|