diff --git a/beastify/content_scripts/beastify.js b/beastify/content_scripts/beastify.js index 9b6c5f5..5204ddf 100644 --- a/beastify/content_scripts/beastify.js +++ b/beastify/content_scripts/beastify.js @@ -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": diff --git a/borderify/borderify.js b/borderify/borderify.js index 9c3728b..fa08e19 100644 --- a/borderify/borderify.js +++ b/borderify/borderify.js @@ -1 +1,4 @@ +/* +Just draw a border round the document.body. +*/ document.body.style.border = "5px solid red"; diff --git a/notify-link-clicks-i18n/background-script.js b/notify-link-clicks-i18n/background-script.js index 6bd5baa..b9b1aa2 100644 --- a/notify-link-clicks-i18n/background-script.js +++ b/notify-link-clicks-i18n/background-script.js @@ -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"); diff --git a/notify-link-clicks-i18n/content-script.js b/notify-link-clicks-i18n/content-script.js index 1f0fc94..9eb83da 100644 --- a/notify-link-clicks-i18n/content-script.js +++ b/notify-link-clicks-i18n/content-script.js @@ -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) { diff --git a/notify-link-clicks/background-script.js b/notify-link-clicks/background-script.js index 6d70b9a..a111929 100644 --- a/notify-link-clicks/background-script.js +++ b/notify-link-clicks/background-script.js @@ -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({ diff --git a/notify-link-clicks/content-script.js b/notify-link-clicks/content-script.js index 1f0fc94..9eb83da 100644 --- a/notify-link-clicks/content-script.js +++ b/notify-link-clicks/content-script.js @@ -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) { diff --git a/open-my-page-button/background.js b/open-my-page-button/background.js index d2afdff..d627e4a 100644 --- a/open-my-page-button/background.js +++ b/open-my-page-button/background.js @@ -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({ diff --git a/user-agent-rewriter/background.js b/user-agent-rewriter/background.js index 8ef8a39..40255f6 100644 --- a/user-agent-rewriter/background.js +++ b/user-agent-rewriter/background.js @@ -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]; } diff --git a/user-agent-rewriter/popup/choose_ua.js b/user-agent-rewriter/popup/choose_ua.js index a8321f7..0a1869b 100644 --- a/user-agent-rewriter/popup/choose_ua.js +++ b/user-agent-rewriter/popup/choose_ua.js @@ -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;