-
Notifications
You must be signed in to change notification settings - Fork 0
/
amplifier.js
80 lines (68 loc) · 2.8 KB
/
amplifier.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Copyright 2016 Taylor Raack <[email protected]>.
This file is part of PageAccel.
PageAccel is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PageAccel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PageAccel. If not, see <http://www.gnu.org/licenses/>.
*/
var observerConfiguration = { childList: true, subtree: true };
function createMutationObserver(baseElement, childTag, callback) {
var mutationObserver = new MutationObserver(function(mutations, observer) {
mutations.forEach(function(mutation) {
for (var i = 0; i < mutation.addedNodes.length; i++) {
if (mutation.addedNodes[i].tagName === childTag && callback(mutation.addedNodes[i])) {
observer.disconnect();
}
}
});
});
mutationObserver.observe(baseElement, observerConfiguration);
}
chrome.runtime.sendMessage({ sentinel: "__SIMPLIFYMESSAGE__", method: "getBack"}, function(goBack) {
if(goBack == true) {
window.history.back();
}
});
// clear out canonical and amp urls
chrome.runtime.sendMessage({
sentinel: "__SIMPLIFYMESSAGE__",
method: "clear"
});
chrome.runtime.sendMessage({
sentinel: "__SIMPLIFYMESSAGE__",
method: "origin",
data: location.origin
})
// look for amp header
chrome.runtime.sendMessage({
sentinel: "__SIMPLIFYMESSAGE__",
method: "onAmpPage",
data: document.documentElement.hasAttribute('amp') || document.documentElement.hasAttribute('⚡')
});
// look for new canonical and amp urls
createMutationObserver(document.documentElement, "HEAD", function (headTag) {
var node = headTag.querySelector('link[rel="amphtml"]');
if(node) {
chrome.runtime.sendMessage({ sentinel: "__SIMPLIFYMESSAGE__", method: "ampUrl", data: node.getAttribute("href") });
}
node = headTag.querySelector('link[rel="canonical"]');
if(node) {
chrome.runtime.sendMessage({ sentinel: "__SIMPLIFYMESSAGE__", method: "canonicalUrl", data: node.getAttribute("href") });
}
createMutationObserver(headTag, "LINK", function (linkTag) {
if (linkTag.getAttribute('rel').toLowerCase() === 'amphtml') {
chrome.runtime.sendMessage({ sentinel: "__SIMPLIFYMESSAGE__", method: "ampUrl", data: linkTag.getAttribute("href") });
} else if (linkTag.getAttribute('rel').toLowerCase() === 'canonical') {
chrome.runtime.sendMessage({ sentinel: "__SIMPLIFYMESSAGE__", method: "canonicalUrl", data: linkTag.getAttribute("href") });
}
return false;
});
return true;
});