Changed to runtime for beastify extension when getting content url

This commit is contained in:
Michal Wozniak
2022-06-11 23:14:27 +01:00
parent 6452f29990
commit 8c98d1a05b

View File

@@ -11,71 +11,70 @@ const hidePage = `body > :not(.beastify-image) {
* the content script in the page. * the content script in the page.
*/ */
function listenForClicks() { function listenForClicks() {
document.addEventListener("click", (e) => { document.addEventListener("click", (e) => {
/** /**
* Given the name of a beast, get the URL to the corresponding image. * Given the name of a beast, get the URL to the corresponding image.
*/ */
function beastNameToURL(beastName) { function beastNameToURL(beastName) {
switch (beastName) { switch (beastName) {
case "Frog": case "Frog":
return browser.extension.getURL("beasts/frog.jpg"); return browser.runtime.getURL("beasts/frog.jpg");
case "Snake": case "Snake":
return browser.extension.getURL("beasts/snake.jpg"); return browser.runtime.getURL("beasts/snake.jpg");
case "Turtle": case "Turtle":
return browser.extension.getURL("beasts/turtle.jpg"); return browser.runtime.getURL("beasts/turtle.jpg");
} }
} }
/** /**
* Insert the page-hiding CSS into the active tab, * Insert the page-hiding CSS into the active tab,
* then get the beast URL and * then get the beast URL and
* send a "beastify" message to the content script in the active tab. * send a "beastify" message to the content script in the active tab.
*/ */
function beastify(tabs) { function beastify(tabs) {
browser.tabs.insertCSS({code: hidePage}).then(() => { browser.tabs.insertCSS({ code: hidePage }).then(() => {
let url = beastNameToURL(e.target.textContent); let url = beastNameToURL(e.target.textContent);
browser.tabs.sendMessage(tabs[0].id, { browser.tabs.sendMessage(tabs[0].id, {
command: "beastify", command: "beastify",
beastURL: url beastURL: url
}); });
}); });
} }
/** /**
* Remove the page-hiding CSS from the active tab, * Remove the page-hiding CSS from the active tab,
* send a "reset" message to the content script in the active tab. * send a "reset" message to the content script in the active tab.
*/ */
function reset(tabs) { function reset(tabs) {
browser.tabs.removeCSS({code: hidePage}).then(() => { browser.tabs.removeCSS({ code: hidePage }).then(() => {
browser.tabs.sendMessage(tabs[0].id, { browser.tabs.sendMessage(tabs[0].id, {
command: "reset", command: "reset",
}); });
}); });
} }
/** /**
* Just log the error to the console. * Just log the error to the console.
*/ */
function reportError(error) { function reportError(error) {
console.error(`Could not beastify: ${error}`); console.error(`Could not beastify: ${error}`);
} }
/** /**
* Get the active tab, * Get the active tab,
* then call "beastify()" or "reset()" as appropriate. * then call "beastify()" or "reset()" as appropriate.
*/ */
if (e.target.classList.contains("beast")) { if (e.target.classList.contains("beast")) {
browser.tabs.query({active: true, currentWindow: true}) browser.tabs.query({ active: true, currentWindow: true })
.then(beastify) .then(beastify)
.catch(reportError); .catch(reportError);
} } else if (e.target.classList.contains("reset")) {
else if (e.target.classList.contains("reset")) { browser.tabs.query({ active: true, currentWindow: true })
browser.tabs.query({active: true, currentWindow: true}) .then(reset)
.then(reset) .catch(reportError);
.catch(reportError); }
} });
});
} }
/** /**
@@ -83,9 +82,9 @@ function listenForClicks() {
* Display the popup's error message, and hide the normal UI. * Display the popup's error message, and hide the normal UI.
*/ */
function reportExecuteScriptError(error) { function reportExecuteScriptError(error) {
document.querySelector("#popup-content").classList.add("hidden"); document.querySelector("#popup-content").classList.add("hidden");
document.querySelector("#error-content").classList.remove("hidden"); document.querySelector("#error-content").classList.remove("hidden");
console.error(`Failed to execute beastify content script: ${error.message}`); console.error(`Failed to execute beastify content script: ${error.message}`);
} }
/** /**
@@ -93,6 +92,6 @@ function reportExecuteScriptError(error) {
* and add a click handler. * and add a click handler.
* If we couldn't inject the script, handle the error. * If we couldn't inject the script, handle the error.
*/ */
browser.tabs.executeScript({file: "/content_scripts/beastify.js"}) browser.tabs.executeScript({ file: "/content_scripts/beastify.js" })
.then(listenForClicks) .then(listenForClicks)
.catch(reportExecuteScriptError); .catch(reportExecuteScriptError);