Last active
August 9, 2021 21:03
-
-
Save vuori/a0410992c0c17213129d9c59fd4e26d9 to your computer and use it in GitHub Desktop.
A quick hack for TamperMonkey to clean up spying bits from Google search results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name FixGoogleLinks | |
// @namespace http://github.com/vuori | |
// @version 1.0 | |
// @description Quick hack to make Google links go directly to destination | |
// @author vuori | |
// @include /^https?:\/\/(www\.)?google\.[a-z]+\// | |
// @homepageURL https://gist.github.com/vuori/a0410992c0c17213129d9c59fd4e26d9 | |
// @updateURL https://gist.github.com/vuori/a0410992c0c17213129d9c59fd4e26d9/raw | |
// @run-at document-body | |
// @grant GM_log | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const A_RE = /^\/url/; | |
function cleanHref(orig) { | |
let query = orig.split('?')[1]; | |
if (!query) | |
return orig; | |
for (const kv of query.split('&')) { | |
if (!kv.startsWith('url=')) | |
continue; | |
return decodeURIComponent(kv.slice(4)); | |
} | |
return orig; | |
} | |
function cleanNodes(nodes) { | |
for (let elem of nodes) { | |
const href = elem.href; | |
if (!href) | |
continue; | |
// If this looks like a redirect through Google servers, clean it up. | |
if (href.match(A_RE)) | |
href = cleanHref(href); | |
//GM_log(`FixGoogleLinks: cleaned link to ${href}`); | |
// Besides that remove event listeners and other junk. | |
elem.removeAttribute('onmousedown'); | |
elem.setAttribute('href', href); | |
delete elem.dataset.ved; | |
elem.replaceWith(elem.cloneNode(true)); | |
} | |
} | |
const linkObserver = new MutationObserver((recordList) => { | |
for (let record of recordList) { | |
if (!record.addedNodes) | |
return; | |
for (let elem of record.addedNodes) { | |
if (!(elem.tagName === 'DIV' && elem.dataset?.ved && elem.dataset?.hveid)) | |
continue; | |
// Remove suspicious data on the div | |
delete elem.dataset.ved; | |
// Clean up link elements | |
cleanNodes(elem.querySelectorAll('a')); | |
} | |
} | |
}); | |
linkObserver.observe(document.body, { childList: true, subtree: true }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment