From 60a75b89580ab9271a4e79b4055f5611f78451be Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Mon, 23 Jun 2014 12:17:27 +0000 Subject: [PATCH] Update and simplify Google-Tracking-Be-Gone; now uses document.querySelectorAll instead of XPath helper and removes click event listeners added via addEventListener --- googletracking-b-gone.user.js | 93 ++++++----------------------------- 1 file changed, 14 insertions(+), 79 deletions(-) diff --git a/googletracking-b-gone.user.js b/googletracking-b-gone.user.js index cbc834c..affe178 100644 --- a/googletracking-b-gone.user.js +++ b/googletracking-b-gone.user.js @@ -1,8 +1,7 @@ // Google Tracking-B-Gone - FIXED version (by Vitaliy Filippov) -// version 2.4 -// Release Date: 2013-02-06 -// Original link http://sbdev.org -// Now http://userscripts.org/scripts/show/120330 +// version 2.5 +// Release Date: 2014-06-14 +// Homepage http://userscripts.org/scripts/show/120330 // See also http://userscripts.org/scripts/show/132237 // // ===== INSTRUCTIONS ===== @@ -33,17 +32,16 @@ // ==/UserScript== doIt(); // make sure we run at least once, regardless of search results page version -doRTR(); // strip tracking from inital batch of real-time search results document.addEventListener('DOMAttrModified', function (event) { - doRTR(event.target); + doIt(event.target); if (event.target.id == 'xfoot' || event.target.parentNode.id == 'bfoot') { doIt(); } }, false); document.addEventListener('DOMSubtreeModified', function (event) { - doRTR(event.target); + doIt(event.target); if (event.target.id == 'xfoot') { doIt(); } @@ -51,88 +49,25 @@ document.addEventListener('DOMSubtreeModified', function (event) { document.addEventListener('DOMNodeInserted', function (event) { if (event.target.parentNode.id == 'gsr') { - doRTR(); + doIt(); } }, false); -function doIt() { - var resultLinks = $x("//a[@onmousedown]", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE); - resultLinks.forEach(function(link) { // loop over links - if (link.getAttribute('onmousedown')) { - link.removeAttribute('onmousedown'); - } - }); - - resultLinks = $x("//a"); - resultLinks.forEach(function(link) { // loop over links +function doIt(e) { + var resultLinks = e ? e.querySelectorAll('h3.r') : document.querySelectorAll('div#search h3.r'); + for (var i = 0; i < resultLinks.length; i++) { + var link = resultLinks[i].childNodes[0]; var oldLink = link.href; - if (/^https?:\/\/www.google/.test(oldLink) || /^https:\/\/encrypted.google/.test(oldLink)) { - var matches = /url\?(url|q)=(.+?)&/.exec(oldLink); - if (matches != null) { - link.href = unescape(matches[2]); - } - } - }); -} - -function doRTR() { - if (arguments[0] == undefined) { - // get all real-time result links - var resultLinks = $x("//div[@id='search']//a", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE); - } else { - // get all links from the current real-time result - var resultLinks = $x(arguments[0], "//a", XPathResult.ORDERED_NODE_SNAPSHOT_TYPE); - } - resultLinks.forEach(function(link) { // loop over every link - var oldLink = link.href; - if (/^http:\/\/www.google.|^\/?url/.test(oldLink) || /^https:\/\/encrypted.google./.test(oldLink)) { - var matches = /url\?(url|q)=(.+?)&/.exec(oldLink); + if (/^(https?:\/\/(www\.|encrypted\.)?google\.[^\/]*)?\/?url/.test(oldLink)) { + var matches = /[\?&](url|q)=(.+?)&/.exec(oldLink); if (matches != null) { link.href = unescape(matches[2]); } } + // Clear attached event listeners so google can't mangle urls on mouse click if (link.getAttribute('onmousedown')) { link.removeAttribute('onmousedown'); } - }); -} - -// XPath helper, from -// http://wiki.greasespot.net/Code_snippets -function $x() { - var x='', // default values - node=document, - type=0, - fix=true, - i=0, - toAr=function(xp){ // XPathResult to array - var final=[], next; - while(next=xp.iterateNext()) - final.push(next); - return final - }, - cur; - while (cur=arguments[i++]) // argument handler - switch(typeof cur) { - case "string":x+=(x=='') ? cur : " | " + cur;continue; - case "number":type=cur;continue; - case "object":node=cur;continue; - case "boolean":fix=cur;continue; - } - if (fix) { // array conversion logic - if (type==6) type=4; - if (type==7) type=5; + resultLinks[i].innerHTML = resultLinks[i].innerHTML; } - if (!/^\//.test(x)) x="//"+x; // selection mistake helper - if (node!=document && !/^\./.test(x)) x="."+x; // context mistake helper - var temp=document.evaluate(x,node,null,type,null); //evaluate! - if (fix) - switch(type) { // automatically return special type - case 1:return temp.numberValue; - case 2:return temp.stringValue; - case 3:return temp.booleanValue; - case 8:return temp.singleNodeValue; - case 9:return temp.singleNodeValue; - } - return fix ? toAr(temp) : temp; }