mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-17 14:59:12 +02:00
added short comments for all JS functions
This commit is contained in:
@@ -1,18 +1,33 @@
|
|||||||
// Assign beastify() as a listener for messages from the extension.
|
/*
|
||||||
|
Assign beastify() as a listener for messages from the extension.
|
||||||
|
*/
|
||||||
chrome.runtime.onMessage.addListener(beastify);
|
chrome.runtime.onMessage.addListener(beastify);
|
||||||
|
|
||||||
|
/*
|
||||||
|
beastify():
|
||||||
|
* removes every node in the document.body,
|
||||||
|
* then inserts the chosen beast
|
||||||
|
* then removes itself as a listener
|
||||||
|
*/
|
||||||
function beastify(request, sender, sendResponse) {
|
function beastify(request, sender, sendResponse) {
|
||||||
removeEverything();
|
removeEverything();
|
||||||
insertBeast(beastNameToURL(request.beast));
|
insertBeast(beastNameToURL(request.beast));
|
||||||
chrome.runtime.onMessage.removeListener(beastify);
|
chrome.runtime.onMessage.removeListener(beastify);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Remove every node under document.body
|
||||||
|
*/
|
||||||
function removeEverything() {
|
function removeEverything() {
|
||||||
while (document.body.firstChild) {
|
while (document.body.firstChild) {
|
||||||
document.body.firstChild.remove();
|
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) {
|
function insertBeast(beastURL) {
|
||||||
var beastImage = document.createElement("img");
|
var beastImage = document.createElement("img");
|
||||||
beastImage.setAttribute("src", beastURL);
|
beastImage.setAttribute("src", beastURL);
|
||||||
@@ -21,6 +36,9 @@ function insertBeast(beastURL) {
|
|||||||
document.body.appendChild(beastImage);
|
document.body.appendChild(beastImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
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":
|
||||||
|
|||||||
@@ -1 +1,4 @@
|
|||||||
|
/*
|
||||||
|
Just draw a border round the document.body.
|
||||||
|
*/
|
||||||
document.body.style.border = "5px solid red";
|
document.body.style.border = "5px solid red";
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
|
/*
|
||||||
|
Assign `notify()` as a listener to messages from the content script.
|
||||||
|
*/
|
||||||
chrome.runtime.onMessage.addListener(notify);
|
chrome.runtime.onMessage.addListener(notify);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Log that we received the message.
|
||||||
|
Then display a notification. The notification contains the URL,
|
||||||
|
which we read from the message.
|
||||||
|
*/
|
||||||
function notify(message) {
|
function notify(message) {
|
||||||
console.log("background script received message");
|
console.log("background script received message");
|
||||||
var title = chrome.i18n.getMessage("notificationTitle");
|
var title = chrome.i18n.getMessage("notificationTitle");
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
|
/*
|
||||||
|
Add notifyExtension() as a listener to click events.
|
||||||
|
*/
|
||||||
window.addEventListener("click", notifyExtension);
|
window.addEventListener("click", notifyExtension);
|
||||||
|
|
||||||
|
/*
|
||||||
|
If the click was on a link, send a message to the background page.
|
||||||
|
The message contains the link's URL.
|
||||||
|
*/
|
||||||
function notifyExtension(e) {
|
function notifyExtension(e) {
|
||||||
var target = e.target;
|
var target = e.target;
|
||||||
while ((target.tagName != "A" || !target.href) && target.parentNode) {
|
while ((target.tagName != "A" || !target.href) && target.parentNode) {
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
|
/*
|
||||||
|
Assign `notify()` as a listener to messages from the content script.
|
||||||
|
*/
|
||||||
chrome.runtime.onMessage.addListener(notify);
|
chrome.runtime.onMessage.addListener(notify);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Log that we received the message.
|
||||||
|
Then display a notification. The notification contains the URL,
|
||||||
|
which we read from the message.
|
||||||
|
*/
|
||||||
function notify(message) {
|
function notify(message) {
|
||||||
console.log("background script received message");
|
console.log("background script received message");
|
||||||
chrome.notifications.create({
|
chrome.notifications.create({
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
|
/*
|
||||||
|
Add notifyExtension() as a listener to click events.
|
||||||
|
*/
|
||||||
window.addEventListener("click", notifyExtension);
|
window.addEventListener("click", notifyExtension);
|
||||||
|
|
||||||
|
/*
|
||||||
|
If the click was on a link, send a message to the background page.
|
||||||
|
The message contains the link's URL.
|
||||||
|
*/
|
||||||
function notifyExtension(e) {
|
function notifyExtension(e) {
|
||||||
var target = e.target;
|
var target = e.target;
|
||||||
while ((target.tagName != "A" || !target.href) && target.parentNode) {
|
while ((target.tagName != "A" || !target.href) && target.parentNode) {
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
|
/*
|
||||||
|
Add openMyPage() as a listener to clicks on the browser action.
|
||||||
|
*/
|
||||||
chrome.browserAction.onClicked.addListener(openMyPage);
|
chrome.browserAction.onClicked.addListener(openMyPage);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Open a new tab, and load "my-page.html" into it.
|
||||||
|
*/
|
||||||
function openMyPage() {
|
function openMyPage() {
|
||||||
console.log("injecting");
|
console.log("injecting");
|
||||||
chrome.tabs.create({
|
chrome.tabs.create({
|
||||||
|
|||||||
@@ -1,19 +1,37 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
/*
|
||||||
|
This is the page for which we want to rewrite the User-Agent header.
|
||||||
|
*/
|
||||||
var targetPage = "http://useragentstring.com/*";
|
var targetPage = "http://useragentstring.com/*";
|
||||||
|
|
||||||
|
/*
|
||||||
|
Map browser names to UA strings.
|
||||||
|
*/
|
||||||
var uaStrings = {
|
var uaStrings = {
|
||||||
"Firefox 41": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0",
|
"Firefox 41": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0",
|
||||||
"Chrome 41": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
|
"Chrome 41": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
|
||||||
"IE 11": "Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"
|
"IE 11": "Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Initialize the UA to Firefox 41.
|
||||||
|
*/
|
||||||
var ua = uaStrings["Firefox 41"];
|
var ua = uaStrings["Firefox 41"];
|
||||||
|
|
||||||
|
/*
|
||||||
|
Add rewriteUserAgentHeader as a listener to onBeforeSendHeaders,
|
||||||
|
only for the target page.
|
||||||
|
|
||||||
|
Make it "blocking" so we can modify the headers.
|
||||||
|
*/
|
||||||
chrome.webRequest.onBeforeSendHeaders.addListener(rewriteUserAgentHeader,
|
chrome.webRequest.onBeforeSendHeaders.addListener(rewriteUserAgentHeader,
|
||||||
{urls: [targetPage]},
|
{urls: [targetPage]},
|
||||||
["blocking", "requestHeaders"]);
|
["blocking", "requestHeaders"]);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Rewrite the User-Agent header to "ua".
|
||||||
|
*/
|
||||||
function rewriteUserAgentHeader(e) {
|
function rewriteUserAgentHeader(e) {
|
||||||
for (var header of e.requestHeaders) {
|
for (var header of e.requestHeaders) {
|
||||||
if (header.name == "User-Agent") {
|
if (header.name == "User-Agent") {
|
||||||
@@ -23,6 +41,9 @@ function rewriteUserAgentHeader(e) {
|
|||||||
return {requestHeaders: e.requestHeaders};
|
return {requestHeaders: e.requestHeaders};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Update ua to a new value, mapped from the uaString parameter.
|
||||||
|
*/
|
||||||
function setUaString(uaString) {
|
function setUaString(uaString) {
|
||||||
ua = uaStrings[uaString];
|
ua = uaStrings[uaString];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,9 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
If the user clicks on an element which has the class "ua-choice":
|
||||||
|
* fetch the element's textContent: for example, "IE 11"
|
||||||
|
* pass it into the background page's setUaString() function
|
||||||
|
*/
|
||||||
document.addEventListener("click", function(e) {
|
document.addEventListener("click", function(e) {
|
||||||
if (!e.target.classList.contains("ua-choice")) {
|
if (!e.target.classList.contains("ua-choice")) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user