added short comments for all JS functions

This commit is contained in:
Will Bamberg
2016-01-14 15:06:44 -08:00
parent d65a87520c
commit b636b41f1b
9 changed files with 85 additions and 1 deletions

View File

@@ -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);
/*
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(beastNameToURL(request.beast));
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);
@@ -21,6 +36,9 @@ function insertBeast(beastURL) {
document.body.appendChild(beastImage);
}
/*
Given the name of a beast, get the URL to the corresponding image.
*/
function beastNameToURL(beastName) {
switch (beastName) {
case "Frog":

View File

@@ -1 +1,4 @@
/*
Just draw a border round the document.body.
*/
document.body.style.border = "5px solid red";

View File

@@ -1,5 +1,13 @@
/*
Assign `notify()` as a listener to messages from the content script.
*/
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) {
console.log("background script received message");
var title = chrome.i18n.getMessage("notificationTitle");

View File

@@ -1,5 +1,12 @@
/*
Add notifyExtension() as a listener to click events.
*/
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) {
var target = e.target;
while ((target.tagName != "A" || !target.href) && target.parentNode) {

View File

@@ -1,5 +1,13 @@
/*
Assign `notify()` as a listener to messages from the content script.
*/
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) {
console.log("background script received message");
chrome.notifications.create({

View File

@@ -1,5 +1,12 @@
/*
Add notifyExtension() as a listener to click events.
*/
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) {
var target = e.target;
while ((target.tagName != "A" || !target.href) && target.parentNode) {

View File

@@ -1,5 +1,11 @@
/*
Add openMyPage() as a listener to clicks on the browser action.
*/
chrome.browserAction.onClicked.addListener(openMyPage);
/*
Open a new tab, and load "my-page.html" into it.
*/
function openMyPage() {
console.log("injecting");
chrome.tabs.create({

View File

@@ -1,19 +1,37 @@
"use strict";
/*
This is the page for which we want to rewrite the User-Agent header.
*/
var targetPage = "http://useragentstring.com/*";
/*
Map browser names to UA strings.
*/
var uaStrings = {
"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",
"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"];
/*
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,
{urls: [targetPage]},
["blocking", "requestHeaders"]);
/*
Rewrite the User-Agent header to "ua".
*/
function rewriteUserAgentHeader(e) {
for (var header of e.requestHeaders) {
if (header.name == "User-Agent") {
@@ -23,6 +41,9 @@ function rewriteUserAgentHeader(e) {
return {requestHeaders: e.requestHeaders};
}
/*
Update ua to a new value, mapped from the uaString parameter.
*/
function setUaString(uaString) {
ua = uaStrings[uaString];
}

View File

@@ -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) {
if (!e.target.classList.contains("ua-choice")) {
return;