Hack 16. Remove URL Redirections

 < Day Day Up > 

Cut out the middleman and make links point directly to where you want to go.

Many portal sites use redirection links for links that point to other sites. The link first goes to a tracking page on the portal, which logs your click and sends you on your way to the external site. Not only is this an invasion of privacy, but it's also slower, since you need to load the tracking page before you are redirected to the page you actually want to read. This hack detects such redirection links and converts them to direct links that take you straight to the final destination.

2.5.1. The Code

This user script runs on all pages, except for a small list of pages where it is known to cause problems with false positives. It uses the document.links collection to find all the links on the page and checks whether the URL of the link includes another URL within it. If it finds one, it extracts it and unescapes it, and replaces the original URL.

Save the following user script as nomiddleman.user.js:

 // ==UserScript== // @name        NoMiddleMan // @namespace   http://0x539.blogspot.com/ // @description Rewrites URLs to remove redirection scripts // @include     * // @exclude http://del.icio.us/* // @exclude     http://*bloglines.com/* // @exclude http://web.archive.org/* // @exclude http://*wists.com/* // ==/UserScript== // based on code by Albert Bachand // and included here with his gracious permission // http://kungfoo.webhop.org/nomiddleman.user.js for (var i=0; i<document.links.length; i++) {   var link, temp, start, url, qindex, end;   link = document.links[i];  // Special case for Google results (assumes English language)  if (link.text == 'Cached' ||  /Similar.*?pages/.exec(link.text)) {  continue;  }  temp = link.href.toLowerCase();  // ignore javascript links and GeoURL  if (temp.indexOf('javascript:') == 0 ||  temp.indexOf('geourl.org') != -1) {  continue;  }  // find the start of the (last) real url  start = Math.max(temp.lastIndexOf('http%3a'),   temp.lastIndexOf('http%253a'),   temp.lastIndexOf('http:'));  if (start <= 0) {   // special case: handle redirect url without a 'http:' part   start = link.href.lastIndexOf('www.');   if (start < 10) {  start = 0;  } else {   link.href = link.href.substring(0, start) +   'http://' + link.href.substring(start);  }  }  // we are most likely looking at a redirection link  if (start > 0) {  url = link.href.substring(start);  // check whether the real url is a parameter   qindex = link.href.indexOf('?');   if (qindex > -1 && qindex < start) {      // it's a parameter, extract only the url  end = url.indexOf('&');  if (end > -1) {  url = url.substring(0, end);  }  }  // handle Yahoo's chained redirections  var temp = url;  url = unescape(url);  while (temp != url) {  temp = url;  url = unescape(url);  }  // and we're done  link.href = url.replace(/&amp;/g, '&');  } } 

2.5.2. Running the Hack

Before installing the user script, go to http://www.yahoo.com and search for greasemonkey. In the list of search results, each linked page is really a redirect through Yahoo!'s servers, as shown in Figure 2-7.

Figure 2-7. Yahoo! Redirection URL


Now, install the user script (Tools Install This User Script), go back to http://www.yahoo.com, and execute the same search. The link to the search result page now points directly to http://dream.sims.berkeley.edu/~ryanshaw/wordpress/2005/02/18/greasemonkey-stole-your-job-and-your-business-model/, as shown in Figure 2-8.

Figure 2-8. Yahoo! Direct URL


There are a variety of ways that sites can redirect links through a tracking page. This script doesn't handle all of them, but it handles the most common cases and a few special cases used by popular sites (such as Yahoo!). The author maintains a weblog at http://0x539.blogspot.com/ where you can check for updates to this script.

     < Day Day Up > 


    Greasemonkey Hacks
    Greasemonkey Hacks: Tips & Tools for Remixing the Web with Firefox
    ISBN: 0596101651
    EAN: 2147483647
    Year: 2005
    Pages: 168
    Authors: Mark Pilgrim

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net