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 link in your results, you will be getting a 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 link. So I decided to write a little Greasemonkey script to fix this:

// ==UserScript==
// @name                Google Direct Links
// @namespace 
// @description         Script that changes annoying new-style Google links to direct links
// @include             http://google.tld/search?*
// @include   *
// ==/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'){
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):


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).


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):


But as it turns out, there's a working script on now:
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*


// @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?