summaryrefslogtreecommitdiffstats
path: root/src/js/firstparties/google-search.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/firstparties/google-search.js')
-rw-r--r--src/js/firstparties/google-search.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/js/firstparties/google-search.js b/src/js/firstparties/google-search.js
new file mode 100644
index 0000000..d5eea9a
--- /dev/null
+++ b/src/js/firstparties/google-search.js
@@ -0,0 +1,39 @@
+/* globals findInAllFrames:false */
+// In Firefox, outbound google links have the `rwt(...)` mousedown trigger.
+// In Chrome, they just have a `ping` attribute.
+let trap_link = "a[onmousedown^='return rwt(this,'], a[ping]";
+
+// Remove excessive attributes and event listeners from link a
+function cleanLink(a) {
+ // remove all attributes except for href,
+ // target (to support "Open each selected result in a new browser window"),
+ // class, style and ARIA attributes
+ for (let i = a.attributes.length - 1; i >= 0; --i) {
+ const attr = a.attributes[i];
+ if (attr.name !== 'href' && attr.name !== 'target' &&
+ attr.name !== 'class' && attr.name !== 'style' &&
+ !attr.name.startsWith('aria-')) {
+ a.removeAttribute(attr.name);
+ }
+ }
+ a.rel = "noreferrer noopener";
+
+ // block event listeners on the link
+ a.addEventListener("click", function (e) { e.stopImmediatePropagation(); }, true);
+ a.addEventListener("mousedown", function (e) { e.stopImmediatePropagation(); }, true);
+}
+
+// TODO race condition; fix waiting on https://crbug.com/478183
+chrome.runtime.sendMessage({
+ type: "checkEnabled"
+}, function (enabled) {
+ if (!enabled) {
+ return;
+ }
+
+ // since the page is rendered all at once, no need to set up a
+ // mutationObserver or setInterval
+ findInAllFrames(trap_link).forEach((link) => {
+ cleanLink(link);
+ });
+});