Exit Full View

Google Image Relinker

This is a Greasemonkey Extension which improves google's image search results :

  • Removes the annoying frameset when you click the image thumbnail
  • Adds a link directly to the image

Code

// ==UserScript==
// @name          Google Image Relinker
// @namespace     nickthecoder
// @description	  Rewrites Google Image Search links to point straight to the pictures
// @include       http://images.google.*/*
// ==/UserScript==

// Please change the version before every modification
var version = 2.02;

// Add stubs for logging if firebug isn't enabled on this browser
if ( ! console ) {
  console = new Object();
  var doNothing = function() {};
  console.debug = console.info = console.log = doNothing;
}
console.debug( "begin greasemonkey script google_image_relinker" );


(function() 
{
	function selectNodes(doc, context, xpath) 
	{
	   var nodes = doc.evaluate(xpath, context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	   var result = new Array( nodes.snapshotLength );
	   
	   for (var x=0; x<result.length; x++) 
	   {
	      result[x] = nodes.snapshotItem(x);
	   }
	   
	   return result;
	}
	
	doc = window.document;
	
	// Get a list of all A tags that have an href attribute containing the start and stop key strings.
   	var googLinks = selectNodes(doc, doc.body, "//A[contains(@href,'/imgres?imgurl=')][contains(@href,'&imgrefurl=')]");
   
	for (var x=0; x<googLinks.length; x++) 
	{
		// Capture the stuff between the start and stop key strings.
		var gmatch = googLinks[x].href.match( /\/imgres\?imgurl\=(.*?)\&imgrefurl\=(.*?)&/ );
	
		// If it matched successfully...
		if (gmatch) 
		{
			// Replace the link's href with the contents of the text captured in the regular expression's parenthesis.
      //console.debug( "Plain", gmatch[1], gmatch[2] );
			//console.debug( "Unencoded", decodeURI( gmatch[1] ), decodeURI( gmatch[2] ) );
			//console.debug( "Unescaped", unescape( gmatch[1] ), unescape( gmatch[2] ) );
			googLinks[x].href = unescape(gmatch[2]);
      var imageURL = unescape(gmatch[ 1 ]);

      var newBit = document.createElement("div");
      var html = '<span style="position: relative; top: -3px; font-size: 10px; line-height: 1em; padding: 1px; background: #ccc; border: 1px solid #333;">';
      html += '<a title="direct link to image" href="' + imageURL + '" style="text-decoration: none; color: #333; ">IMG</a>';
      html += ' <a title="about this greasemonkey addition (version ' + version + ' )" style="color: #666;" href="http://nickthecoder.co.uk/pinkwino/view/Google+Image+Relinker">(ntc)</a>';
      html += '</span>';
      newBit.innerHTML = html;

      googLinks[x].parentNode.insertBefore( newBit, googLinks[x] );
		}

	}
})();

console.debug( "end greasemonkey script google_image_relinker" );

Version History

2.01

I started on verion 2.01, because this is a fork of the original Google Image Relinker written by Patrick Cavit, (http://patcavit.com) and Eric Hamiter( http://roachfiend.com/)

2.02

  • Added logging to Firebug.
  • Change from decodeURI to unescape. There are two ways that google could have encoded the urls, and I believe they use the "escape" way, not the "encodeURI" way.