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..53e4d27 100644 --- a/amplified.js +++ b/amplified.js @@ -1,23 +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 = { + + 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: amphtml !== null, + noredirect: location.hash === "#noredirect=1" }; - if (amplink !== null) { - amp.ampurl = amplink.href; - - } - if (canonical !== null) { - amp.canonical = canonical.href; - } - - - chrome.runtime.sendMessage(amp); - -})(); \ No newline at end of file + chrome.runtime.sendMessage(amp, function (response) { + if (response && response.stop) { + stop(); + } + }); +}, 0); diff --git a/background.js b/background.js index d389111..3d2a5a5 100644 --- a/background.js +++ b/background.js @@ -1,40 +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 = []; +var ampTabs = {}; - -chrome.runtime.onMessage.addListener(function(amp, sender, sendResponse) { - if (amp.sentinel === undefined || amp.sentinel != "__AMPLIFIER__") { - return; //not from amplifier +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'}); + 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'}); + 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; - - 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.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 }); - } - } else { - chrome.tabs.update(tab.id, { url: amp.ampurl }); - } +chrome.tabs.onRemoved.addListener(function (tabId) { + delete ampTabs[tabId]; }); +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}); + } +}); diff --git a/manifest.json b/manifest.json index 4d29b53..fc1aa2f 100644 --- a/manifest.json +++ b/manifest.json @@ -2,28 +2,34 @@ "manifest_version": 2, "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", - "icons": { - "48": "amplifier48.png", - "128": "amplifier128.png" + "description": "Quickly switch between Canonical and AMP version of a page", + "version": "0.0.6", + "author": "John Pettitt & Timothy Cyrus & Jerzy Głowacki", + "icons": { + "48": "amplifier48.png", + "128": "amplifier128.png" }, - "background" : { - "scripts": ["background.js"], + "background": { + "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." + "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 5f4617e..54510d9 100644 --- a/options.html +++ b/options.html @@ -1,13 +1,12 @@ -
-