-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
47 lines (39 loc) · 1.43 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// By John Pettitt
// 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'});
} else {
chrome.pageAction.setTitle({tabId: sender.tab.id, title: 'AMP version not detected'});
}
}
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";
}
});
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 });
}
} else {
chrome.tabs.update(tab.id, { url: amp.ampurl });
}
});