Monday, September 7, 2009

Greasemonkey script to change Google url?sa=t links to direct links

Google recently decided to change the links in Google results from direct links to tracking links. So instead of getting a nice http://blog.teusink.net/ link in your results, you will be getting a http://www.google.com/url?sa=t&source=web&ct=res.. etc link which will redirect you to the correct site.

I was rather annoyed by this as I use Google a lot when pentesting and often like to copy & paste links from the search results. Instead of getting a direct link to a site, I end up with a huge google.com link. So I decided to write a little Greasemonkey script to fix this:

// ==UserScript==
// @name                Google Direct Links
// @namespace           http://blog.teusink.net/
// @description         Script that changes annoying new-style Google links to direct links
// @include             http://google.tld/search?*
// @include             http://www.google.tld/search?*
// ==/UserScript==

var allElements, thisElement;
allElements = document.evaluate('//*[@onmousedown]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
for (var i = 0; i < allElements.snapshotLength; i++) {
 thisElement = allElements.snapshotItem(i);
 if(thisElement.nodeName.toUpperCase() == 'A'){
  thisElement.removeAttribute('onmousedown');
 }
}
This script removes the onmousedown handlers on all links in Google search results. Another way to achieve this is to disable Javascript on these pages. To use the script paste it into a text file, save it as googledirectlinks.user.js, install Greasemonkey and drag the googledirectlinks.user.js onto a Firefox window (Greasemonkey will prompt you to install the script).

Another nice extension is Adaptive Referer Remover. This add-on allows you to remove Referer headers if they contain certain patterns. So I added the following pattern to prevent sites from seeing what Google query I entered to find them (you could add rules for other Google features as well or even block it entirely):

^http://www\.google\..*search

This add-on refuses to install if you use Firefox 3.5, but it works fine in that version. You can install the Nightly Tester Tools extension, which enables you to install it anyway (or you could edit the .xpi file manually).

6 comments:

AnonWinsAgain said...

handy if you must use google, but i prefer Bing anyway :D

Unknown said...

dont work here, german google. can you help me? installed greasemonkey, installed your script, dont work.

Unknown said...

not working here, can you update this?

Niels Teusink said...

Hi Bernhard,

Sorry for the late response. Yes it doesn't seem to work anymore. I'll try to find the time in the next couple of days to fix it.

Niels Teusink said...

Google ajaxified their search, which broke my script. I came up with this klugde (which works, but isn't pretty):

setTimeout('rwt=function(a,b,c,d,e,f,g,h){};',500);

But as it turns out, there's a working script on userscripts.org now: http://userscripts.org/scripts/show/47300
You should probably use that one as the author will probably maintain it better ;)

Anonymous said...

This script worked for me, but I had to modify it. First, the greasonkey script didn't work. I then changed the lines

// @include http://google.tld/search?*
// @include http://www.google.tld/search?*

to

// @include *google.*

and then it worked. I don't know if there is some code that I should rather use – i.e. if this code enables the script to run even for other sites than google. If so, maybe someone can tell me what to write instead?