SHARE
TWEET

Parashuram

a guest Mar 21st, 2008 126 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           Google Search link cleaner
  3. // @namespace      tgr
  4. // @description    A simple script to clean urls of the result links on the Google Search page
  5. //                 An onmousedown event handler changes those urls to point to www.google.*/url?... to count clicks
  6. //                 which can be extremely annoying because it messes up right-click "copy link location" functionality
  7. //                 This script installs a second event handler which instantly restores the original url
  8. // @include        http://www.google.co*/search?*
  9. // ==/UserScript==
  10.  
  11. // return a query parameter from an url; null if not found
  12. function getParameter(url, name) {
  13.     var paramsStart = url.indexOf("?");
  14.     if(paramsStart == -1) return null;
  15.     var paramsEnd = url.indexOf("#");
  16.     if(paramsEnd == -1)
  17.         queryString = url.substr(paramsStart + 1);
  18.     else
  19.         queryString = url.substr(paramsStart + 1, paramsEnd - paramsStart - 1);
  20.     var parameters = queryString.split('&');
  21.     for(var i = 0; i < parameters.length; i++) {
  22.         var values = parameters[i].split('=');
  23.         if(values[0] == name)
  24.             return unescape(values[1]);
  25.     }
  26.     return null;
  27. }
  28.  
  29. // if the url begins with "/url", get the original url (which is now in the "url" parameter of the query string)
  30. // then remove our event listener (the original event handler runs only once so there's no further need)
  31. function clearUrl(event) {
  32.         if (event.button != 2)
  33.         {
  34.                 return;
  35.         }
  36.     var link = this.getAttribute('href');
  37.         window.axe = event;
  38.     if(link.match(/^\/url/));
  39.         {
  40.                 var originalLink = getParameter(link, 'url');
  41.         if(originalLink)
  42.                 {
  43.             this.setAttribute('href', originalLink);
  44.             this.removeEventListener("mousedown", clearUrl, true);
  45.         }
  46.                
  47.                 GM_xmlhttpRequest({
  48.                     method: 'GET',
  49.                     url: link,
  50.                     headers: {'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey','Accept': 'application/atom+xml,application/xml,text/xml'},
  51.                     onload: function(responseDetails) {console.log(originalLink + "HW" + link)}
  52.                 });
  53.         }
  54. }
  55.  
  56. // find links to search results and add an event handler to them, which undoes the effect of the original event handler
  57. // (the original event handler cannot be removed due to GreaseMonkey's Javascript limitations)
  58. var linkList = document.getElementById("res").getElementsByTagName("a");
  59. for(var i = 0; i < linkList.length; i++) {
  60.     var link = linkList[i];
  61.     if(link.className == "l")
  62.         {
  63.         link.addEventListener("mousedown", clearUrl, true);
  64.     }
  65. }
  66.  
  67.  
  68.  
  69. window.setTimeout(
  70. 'var linkList = document.getElementById("res").getElementsByTagName("a"); for(var i = 0; i < linkList.length; i++) {if(linkList[i].className == "l") linkList[i].onmousedown.call(linkList[i]);}'
  71. ,1000);
RAW Paste Data
Top