mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
* MDN get started tutorial examples to manifest V3 * Migrate to Scripting API * Updated to readmes and code comments * Apply suggestions to readmes from review Co-authored-by: Simeon Vincent <svincent@gmail.com> * Convert choose_beast.js to async * Apply suggestions from review Co-authored-by: Simeon Vincent <svincent@gmail.com> * Updated illustrated API list for beastify * Apply suggestions from review Co-authored-by: Rob Wu <rob@robwu.nl> --------- Co-authored-by: Simeon Vincent <svincent@gmail.com> Co-authored-by: Rob Wu <rob@robwu.nl>
52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
(function() {
|
|
/**
|
|
* Check and set a global guard variable to
|
|
* ensure that if this content script is injected into a page again,
|
|
* it returns (and does nothing).
|
|
*/
|
|
if (window.hasRun) {
|
|
return;
|
|
}
|
|
window.hasRun = true;
|
|
|
|
/**
|
|
* Given a URL for a beast image, remove all beasts,
|
|
* then create and style an IMG node pointing to the image and
|
|
* insert the node into the document.
|
|
*/
|
|
function insertBeast(beastURL) {
|
|
removeExistingBeasts();
|
|
let beastImage = document.createElement("img");
|
|
beastImage.setAttribute("src", beastURL);
|
|
beastImage.style.objectFit = "contain";
|
|
beastImage.style.position = "fixed";
|
|
beastImage.style.height = "100%";
|
|
beastImage.style.width = "100%";
|
|
beastImage.className = "beastify-image";
|
|
document.body.appendChild(beastImage);
|
|
}
|
|
|
|
/**
|
|
* Remove all beasts from the page.
|
|
*/
|
|
function removeExistingBeasts() {
|
|
let existingBeasts = document.querySelectorAll(".beastify-image");
|
|
for (let beast of existingBeasts) {
|
|
beast.remove();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Listen for messages from the background script.
|
|
* Depending on the message, call "beastify()" or "reset()".
|
|
*/
|
|
browser.runtime.onMessage.addListener((message) => {
|
|
if (message.command === "beastify") {
|
|
insertBeast(message.beastURL);
|
|
} else if (message.command === "reset") {
|
|
removeExistingBeasts();
|
|
}
|
|
});
|
|
|
|
})();
|