| 1 |
/* globals findInAllFrames:false, observeMutations:false */ |
| 2 |
|
| 3 |
let link_selector = [ |
| 4 |
"a[href^='www.google.com/url?']", |
| 5 |
"a[href^='https://www.google.com/url?']", |
| 6 |
"a[href^='/url?q=']", |
| 7 |
`a[href^='${document.location.hostname}/url?']`, |
| 8 |
].join(', '); |
| 9 |
|
| 10 |
function cleanLink(a, norecurse) { |
| 11 |
if (!a.href.startsWith("https://www.google.com/url?") && |
| 12 |
!a.href.startsWith(document.location.origin + "/url?")) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
let searchParams = (new URL(a.href)).searchParams, |
| 17 |
href = searchParams.get('url') || searchParams.get('q'); |
| 18 |
|
| 19 |
if (!href || !window.isURL(href)) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
a.href = href; |
| 24 |
a.rel = "noreferrer noopener"; |
| 25 |
|
| 26 |
// links could be double wrapped (`/mobilebasic` Google doc) |
| 27 |
if (!norecurse) { |
| 28 |
cleanLink(a, true); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// TODO race condition; fix waiting on https://crbug.com/478183 |
| 33 |
chrome.runtime.sendMessage({ |
| 34 |
type: "checkEnabled" |
| 35 |
}, function (enabled) { |
| 36 |
if (!enabled) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// clean already present links |
| 41 |
findInAllFrames(link_selector).forEach((link) => { |
| 42 |
cleanLink(link); |
| 43 |
}); |
| 44 |
|
| 45 |
// clean dynamically added links |
| 46 |
observeMutations(link_selector, cleanLink); |
| 47 |
}); |