forked from insightbrowser/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disable_amp.js
55 lines (45 loc) · 1.46 KB
/
disable_amp.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
(() => {
const isProbableAmpPage = () => {
const url = document.location;
return url.pathname.includes('/amp/')
|| (url.hostname.startsWith('amp.') && url.hostname.split('.').length > 2);
};
const hasAmpAttr = () => {
return document.documentElement.attributes.getNamedItem('amp')
|| document.documentElement.attributes.getNamedItem('\u26A1');
}
const redirectIfInAmpPage = () => {
if (!isProbableAmpPage() || !hasAmpAttr()) {
return false;
}
const canonicalEl = document.head.querySelector("link[rel~='canonical'][href]");
if (!canonicalEl) {
return false;
}
try {
const newURL = new URL(canonicalEl.href);
if (newURL.toString() === document.referrer || document.referrer === document.location.toString()) {
return false;
}
window.location.replace(newURL);
return true;
} catch {
return false;
}
}
if (redirectIfInAmpPage()) { return; }
const cleanupAmp = () => {
document.querySelectorAll('a.amp_r').forEach(function(a) {
a.href = a.getAttribute('href');
if (!a.href.indexOf('?') !== -1) a.href = a.href + '?';
a.removeAttribute('data-amp');
a.removeAttribute('data-amp-cur');
a.removeAttribute('ping');
});
document.querySelectorAll('span[aria-label=\"AMP logo\"]').forEach(function(a) {
a.style.display='none';
});
}
document.addEventListener('DOMNodeInserted', cleanupAmp);
cleanupAmp();
})();