-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
google.js
47 lines (38 loc) · 1.11 KB
/
google.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
/* globals findInAllFrames:false, observeMutations:false */
let link_selector = [
"a[href^='www.google.com/url?']",
"a[href^='https://www.google.com/url?']",
"a[href^='/url?q=']",
`a[href^='${document.location.hostname}/url?']`,
].join(', ');
function cleanLink(a, norecurse) {
if (!a.href.startsWith("https://www.google.com/url?") &&
!a.href.startsWith(document.location.origin + "/url?")) {
return;
}
let searchParams = (new URL(a.href)).searchParams,
href = searchParams.get('url') || searchParams.get('q');
if (!href || !window.isURL(href)) {
return;
}
a.href = href;
a.rel = "noreferrer noopener";
// links could be double wrapped (`/mobilebasic` Google doc)
if (!norecurse) {
cleanLink(a, true);
}
}
// TODO race condition; fix waiting on https://crbug.com/478183
chrome.runtime.sendMessage({
type: "checkEnabled"
}, function (enabled) {
if (!enabled) {
return;
}
// clean already present links
findInAllFrames(link_selector).forEach((link) => {
cleanLink(link);
});
// clean dynamically added links
observeMutations(link_selector, cleanLink);
});