Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically load AMP pages #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
See https://www.ampproject.org/ and https://github.com/ampproject
32 changes: 14 additions & 18 deletions amplified.js
Original file line number Diff line number Diff line change
@@ -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);

})();
chrome.runtime.sendMessage(amp, function (response) {
if (response && response.stop) {
stop();
}
});
}, 0);
65 changes: 38 additions & 27 deletions background.js
Original file line number Diff line number Diff line change
@@ -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});
}
});
38 changes: 22 additions & 16 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
13 changes: 6 additions & 7 deletions options.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<!DOCTYPE html>

<html>
<head>
<title>Options for Amplifier AMP switcher.</title>
<title>Options for Amplifier AMP switcher</title>
</head>
<body>
<h1>Options for Amplifier AMP switcher.</h1>
<label for="devmode">Load AMP pages in developer mode</label>
<input type="checkbox" id="devmode">
<script type="text/javascript" src="options.js"></script>
<h1>Options for Amplifier AMP switcher:</h1>
<p><label><input type="checkbox" id="automode"> Load AMP pages automatically</label></p>
<p><label><input type="checkbox" id="devmode"> Load AMP pages in developer mode</label></p>
<script type="text/javascript" src="options.js"></script>
</body>
</html>
</html>
25 changes: 12 additions & 13 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@




document.getElementById("devmode").checked = (localStorage["devMode"] == "true"); // local storage stringifies things





document.getElementById("devmode").addEventListener("change",function() {
localStorage["devMode"] = this.checked;
});
var automode = document.getElementById("automode");
var devmode = document.getElementById("devmode");

automode.checked = localStorage.getItem("autoMode") === "true";
devmode.checked = localStorage.getItem("devMode") === "true";

automode.addEventListener("change", function () {
localStorage.setItem("autoMode", this.checked);
});
devmode.addEventListener("change", function () {
localStorage.setItem("devMode", this.checked);
});