mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
* added README * do name->URL conversion in the popup * define functions before they are added as listeners
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
/*
|
|
beastify():
|
|
* removes every node in the document.body,
|
|
* then inserts the chosen beast
|
|
* then removes itself as a listener
|
|
*/
|
|
function beastify(request, sender, sendResponse) {
|
|
removeEverything();
|
|
insertBeast(request.beastURL);
|
|
chrome.runtime.onMessage.removeListener(beastify);
|
|
}
|
|
|
|
/*
|
|
Remove every node under document.body
|
|
*/
|
|
function removeEverything() {
|
|
while (document.body.firstChild) {
|
|
document.body.firstChild.remove();
|
|
}
|
|
}
|
|
|
|
/*
|
|
Given a URL to a beast image, create and style an IMG node pointing to
|
|
that image, then insert the node into the document.
|
|
*/
|
|
function insertBeast(beastURL) {
|
|
var beastImage = document.createElement("img");
|
|
beastImage.setAttribute("src", beastURL);
|
|
beastImage.setAttribute("style", "width: 100vw");
|
|
beastImage.setAttribute("style", "height: 100vh");
|
|
document.body.appendChild(beastImage);
|
|
}
|
|
|
|
/*
|
|
Assign beastify() as a listener for messages from the extension.
|
|
*/
|
|
chrome.runtime.onMessage.addListener(beastify);
|
|
|
|
|
|
/*
|
|
Given the name of a beast, get the URL to the corresponding image.
|
|
*/
|
|
function beastNameToURL(beastName) {
|
|
switch (beastName) {
|
|
case "Frog":
|
|
return chrome.extension.getURL("beasts/frog.jpg");
|
|
case "Snake":
|
|
return chrome.extension.getURL("beasts/snake.jpg");
|
|
case "Turtle":
|
|
return chrome.extension.getURL("beasts/turtle.jpg");
|
|
}
|
|
}
|