From 81248666c19ce624efac593761b892ea98134a6f Mon Sep 17 00:00:00 2001 From: Timothy Cyrus Date: Thu, 20 Oct 2016 13:56:37 -0400 Subject: [PATCH 1/3] Cleanup Code Modified `amplified.js` to use ternary operators Combined `querySelector`s in `amplified.js` Minor Changes --- README.md | 4 ++-- amplified.js | 22 ++++++---------------- manifest.json | 19 +++++++++---------- options.html | 6 +++--- options.js | 16 +++++----------- 5 files changed, 25 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 8d12926..0d23348 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,6 @@ Amplifier AMP/Canonical switcher Chrome Extension This extension is designed for developers working with the AMP (Accelerated Mobile Pages) standard. It detects AMP links in the page header and allows quick switching between the AMP and Canonical version of a page. Optionally AMP pages can be loaded -with the AMP validation enabled (#development=1). +with the AMP validation enabled (`#development=1`). -See http://www.ampproject.org/ and https://github.com/ampproject \ No newline at end of file +See https://www.ampproject.org/ and https://github.com/ampproject diff --git a/amplified.js b/amplified.js index 6722437..58504dc 100644 --- a/amplified.js +++ b/amplified.js @@ -1,23 +1,13 @@ (function() { var amplink = document.querySelector("link[rel='amphtml']"); var canonical = document.querySelector("link[rel='canonical']"); - - var amp = { + + var amp = { sentinel: "__AMPLIFIER__", - ampurl : null, - canonical : null, - isamp : (document.querySelector("html[amp]") !== null || document.querySelector("html[⚡]") !== null) + ampurl : (amplink !== null) ? amplink.href : null, + canonical : (canonical !== null) ? canonical.href : null, + isamp : (document.querySelector("html[amp], html[⚡]") !== null) }; - if (amplink !== null) { - amp.ampurl = amplink.href; - - } - if (canonical !== null) { - amp.canonical = canonical.href; - } - - chrome.runtime.sendMessage(amp); - -})(); \ No newline at end of file +})(); diff --git a/manifest.json b/manifest.json index 4d29b53..4d23a7f 100644 --- a/manifest.json +++ b/manifest.json @@ -2,14 +2,14 @@ "manifest_version": 2, "name": "Amplifier AMP/Canonical switcher", "short_name": "Amplifier", - "description": "Quickly switch between canonical and AMP version of a page", + "description": "Quickly switch between Canonical and AMP version of a page", "version": "0.0.5", "author": "John Pettitt", - "icons": { - "48": "amplifier48.png", - "128": "amplifier128.png" + "icons": { + "48": "amplifier48.png", + "128": "amplifier128.png" }, - "background" : { + "background": { "scripts": ["background.js"], "persistent": true }, @@ -17,13 +17,12 @@ { "matches": ["http://*/*","https://*/*"], "js": ["amplified.js"], - "run_at" : "document_end" + "run_at": "document_end" } ], - "page_action" : - { - "default_icon" : "switcher.png", - "default_title" : "AMP version not detected." + "page_action": { + "default_icon": "switcher.png", + "default_title": "AMP version not detected." }, "options_page" : "options.html" } diff --git a/options.html b/options.html index 5f4617e..d1ab447 100644 --- a/options.html +++ b/options.html @@ -7,7 +7,7 @@

Options for Amplifier AMP switcher.

- - + + - \ No newline at end of file + diff --git a/options.js b/options.js index f3daef7..5d2dfe3 100644 --- a/options.js +++ b/options.js @@ -1,13 +1,7 @@ +var devmode = document.getElementById("devmode"); +devmode.checked = (localStorage["devMode"] == "true"); // local storage stringifies things - - -document.getElementById("devmode").checked = (localStorage["devMode"] == "true"); // local storage stringifies things - - - - - -document.getElementById("devmode").addEventListener("change",function() { - localStorage["devMode"] = this.checked; -}); \ No newline at end of file +devmode.addEventListener("change", function() { + localStorage["devMode"] = this.checked; +}); From 5bf6423ae6e3cab429be38be1d683b08aad53dc9 Mon Sep 17 00:00:00 2001 From: Timothy Cyrus Date: Thu, 20 Oct 2016 14:02:25 -0400 Subject: [PATCH 2/3] Minor Changes Changed `ampTabs` from array to dictionary Made indentation consistant Added Case for "AMP version not detected" Removed period from `default_title` to match other titles Delete tab information if closed using `chrome.tabs.onRemoved` --- background.js | 55 +++++++++++++++++++++++++++++---------------------- manifest.json | 2 +- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/background.js b/background.js index d389111..b868133 100644 --- a/background.js +++ b/background.js @@ -1,40 +1,47 @@ // By John Pettitt // Crreative Commons CC(0) Plublic domain -var ampTabs = []; - +var ampTabs = {}; chrome.runtime.onMessage.addListener(function(amp, sender, sendResponse) { - if (amp.sentinel === undefined || amp.sentinel != "__AMPLIFIER__") { - return; //not from amplifier - } - if (amp.isamp) { - chrome.pageAction.setIcon({tabId:sender.tab.id, path: 'canonical.png'}); - chrome.pageAction.setTitle({tabId:sender.tab.id, title: 'Show the Canonical version of this page'}); + if (amp.sentinel === undefined || amp.sentinel !== "__AMPLIFIER__") { + return; //not from amplifier + } + + if (amp.isamp) { + chrome.pageAction.setIcon({tabId: sender.tab.id, path: 'canonical.png'}); + chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'Show the Canonical version of this page'}); + } else { + if (amp.ampurl !== null) { + chrome.pageAction.setIcon({tabId: sender.tab.id, path: 'amplify.png'}); + chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'Show the AMP version of this page'}); } else { - chrome.pageAction.setIcon({tabId:sender.tab.id, path: 'amplify.png'}); - chrome.pageAction.setTitle({tabId:sender.tab.id, title: 'Show the AMP version of this page'}); - } - if(amp.ampurl !== null || (amp.canonical !=null && amp.isamp)) { - chrome.pageAction.show(sender.tab.id); + chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'AMP version not detected'}); } - ampTabs[sender.tab.id] = amp; - - if (localStorage["devMode"] == "true") { - amp.ampurl += "#development=1"; - } - + } + + if (amp.ampurl !== null || (amp.canonical !== null && amp.isamp)) { + chrome.pageAction.show(sender.tab.id); + } + + ampTabs[sender.tab.id] = amp; + + if (localStorage["devMode"] == "true") { + amp.ampurl += "#development=1"; + } }); -// -chrome.pageAction.onClicked.addListener(function(tab){ +chrome.tabs.onRemoved.addListener(function(tabId) { + delete ampTabs[tabId]; +}); + +chrome.pageAction.onClicked.addListener(function(tab) { var amp = ampTabs[tab.id]; if (amp.isamp) { - if (amp.canonical != null) { + if (amp.canonical !== null) { chrome.tabs.update(tab.id, { url: amp.canonical }); - } + } } else { chrome.tabs.update(tab.id, { url: amp.ampurl }); } }); - diff --git a/manifest.json b/manifest.json index 4d23a7f..fb98d2b 100644 --- a/manifest.json +++ b/manifest.json @@ -22,7 +22,7 @@ ], "page_action": { "default_icon": "switcher.png", - "default_title": "AMP version not detected." + "default_title": "AMP version not detected" }, "options_page" : "options.html" } From d7d767acf64b906b302798036e7d26bf60f91c9d Mon Sep 17 00:00:00 2001 From: niu tech Date: Tue, 9 May 2017 00:54:19 +0200 Subject: [PATCH 3/3] Add auto mode, run content script on document start, reformat code --- amplified.js | 18 ++++++++----- background.js | 70 +++++++++++++++++++++++++++------------------------ manifest.json | 21 ++++++++++------ options.html | 11 ++++---- options.js | 11 +++++--- 5 files changed, 76 insertions(+), 55 deletions(-) diff --git a/amplified.js b/amplified.js index 58504dc..53e4d27 100644 --- a/amplified.js +++ b/amplified.js @@ -1,13 +1,19 @@ -(function() { +setTimeout(function () { + var amphtml = document.querySelector("html[amp], html[⚡]"); var amplink = document.querySelector("link[rel='amphtml']"); var canonical = document.querySelector("link[rel='canonical']"); var amp = { sentinel: "__AMPLIFIER__", - ampurl : (amplink !== null) ? amplink.href : null, - canonical : (canonical !== null) ? canonical.href : null, - isamp : (document.querySelector("html[amp], html[⚡]") !== null) + ampurl: (amplink !== null) ? amplink.href : null, + canonical: (canonical !== null) ? canonical.href : null, + isamp: amphtml !== null, + noredirect: location.hash === "#noredirect=1" }; - chrome.runtime.sendMessage(amp); -})(); + chrome.runtime.sendMessage(amp, function (response) { + if (response && response.stop) { + stop(); + } + }); +}, 0); diff --git a/background.js b/background.js index b868133..3d2a5a5 100644 --- a/background.js +++ b/background.js @@ -1,47 +1,51 @@ -// By John Pettitt -// Crreative Commons CC(0) Plublic domain +// By John Pettitt & Timothy Cyrus & Jerzy Głowacki +// Creative Commons CC(0) Public domain var ampTabs = {}; -chrome.runtime.onMessage.addListener(function(amp, sender, sendResponse) { - if (amp.sentinel === undefined || amp.sentinel !== "__AMPLIFIER__") { - return; //not from amplifier - } - - if (amp.isamp) { - chrome.pageAction.setIcon({tabId: sender.tab.id, path: 'canonical.png'}); - chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'Show the Canonical version of this page'}); - } else { - if (amp.ampurl !== null) { - chrome.pageAction.setIcon({tabId: sender.tab.id, path: 'amplify.png'}); - chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'Show the AMP version of this page'}); +chrome.runtime.onMessage.addListener(function (amp, sender, sendResponse) { + if (amp.sentinel !== "__AMPLIFIER__") { + return; //not from amplifier + } + + if (amp.isamp) { + chrome.pageAction.setIcon({tabId: sender.tab.id, path: 'canonical.png'}); + chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'Show the Canonical version of this page'}); + } else if (amp.ampurl !== null) { + chrome.pageAction.setIcon({tabId: sender.tab.id, path: 'amplify.png'}); + chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'Show the AMP version of this page'}); } else { - chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'AMP version not detected'}); + chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'AMP version not detected'}); } - } - if (amp.ampurl !== null || (amp.canonical !== null && amp.isamp)) { - chrome.pageAction.show(sender.tab.id); - } + if (amp.ampurl !== null || (amp.isamp && amp.canonical !== null)) { + chrome.pageAction.show(sender.tab.id); + } - ampTabs[sender.tab.id] = amp; + ampTabs[sender.tab.id] = amp; - if (localStorage["devMode"] == "true") { - amp.ampurl += "#development=1"; - } + if (localStorage.getItem("devMode") === "true") { + amp.ampurl += "#development=1"; + } + + if (localStorage.getItem("autoMode") === "true") { + amp.canonical += "#noredirect=1"; + if (!amp.isamp && !amp.noredirect && amp.ampurl !== null) { + sendResponse({stop: true}); + chrome.tabs.update(sender.tab.id, {url: amp.ampurl}); + } + } }); -chrome.tabs.onRemoved.addListener(function(tabId) { - delete ampTabs[tabId]; +chrome.tabs.onRemoved.addListener(function (tabId) { + delete ampTabs[tabId]; }); -chrome.pageAction.onClicked.addListener(function(tab) { - var amp = ampTabs[tab.id]; - if (amp.isamp) { - if (amp.canonical !== null) { - chrome.tabs.update(tab.id, { url: amp.canonical }); +chrome.pageAction.onClicked.addListener(function (tab) { + var amp = ampTabs[tab.id]; + if (amp.isamp && amp.canonical !== null) { + chrome.tabs.update(tab.id, {url: amp.canonical}); + } else { + chrome.tabs.update(tab.id, {url: amp.ampurl}); } - } else { - chrome.tabs.update(tab.id, { url: amp.ampurl }); - } }); diff --git a/manifest.json b/manifest.json index fb98d2b..fc1aa2f 100644 --- a/manifest.json +++ b/manifest.json @@ -3,26 +3,33 @@ "name": "Amplifier AMP/Canonical switcher", "short_name": "Amplifier", "description": "Quickly switch between Canonical and AMP version of a page", - "version": "0.0.5", - "author": "John Pettitt", + "version": "0.0.6", + "author": "John Pettitt & Timothy Cyrus & Jerzy Głowacki", "icons": { "48": "amplifier48.png", "128": "amplifier128.png" }, "background": { - "scripts": ["background.js"], + "scripts": [ + "background.js" + ], "persistent": true }, "content_scripts": [ { - "matches": ["http://*/*","https://*/*"], - "js": ["amplified.js"], - "run_at": "document_end" + "matches": [ + "http://*/*", + "https://*/*" + ], + "js": [ + "amplified.js" + ], + "run_at": "document_start" } ], "page_action": { "default_icon": "switcher.png", "default_title": "AMP version not detected" }, - "options_page" : "options.html" + "options_page": "options.html" } diff --git a/options.html b/options.html index d1ab447..54510d9 100644 --- a/options.html +++ b/options.html @@ -1,13 +1,12 @@ - - Options for Amplifier AMP switcher. + Options for Amplifier AMP switcher -

Options for Amplifier AMP switcher.

- - - +

Options for Amplifier AMP switcher:

+

+

+ diff --git a/options.js b/options.js index 5d2dfe3..e48f182 100644 --- a/options.js +++ b/options.js @@ -1,7 +1,12 @@ +var automode = document.getElementById("automode"); var devmode = document.getElementById("devmode"); -devmode.checked = (localStorage["devMode"] == "true"); // local storage stringifies things +automode.checked = localStorage.getItem("autoMode") === "true"; +devmode.checked = localStorage.getItem("devMode") === "true"; -devmode.addEventListener("change", function() { - localStorage["devMode"] = this.checked; +automode.addEventListener("change", function () { + localStorage.setItem("autoMode", this.checked); +}); +devmode.addEventListener("change", function () { + localStorage.setItem("devMode", this.checked); });