O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Greasemonkey Hacks
By Mark Pilgrim
November 2005
More Info

HACK
#83
Restore Functionality in Google Print
Google Print goes to extraordinary lengths to keep you from downloading images, but you don't need to go to the same extraordinary lengths to get them anyway
The Code
[Discuss (0) | Link to this hack]

The Code

This user script runs on Google Print pages. Right out of the gate, it reenables the right-click context menu by setting document.oncontextmenu=null. Then, it uses an XPath query to find all the transparent GIFs named cleardot.gif. These are the GIFs obscuring other images. For each one, it replaces the URL of the transparent GIF with the URL of the obscured image. For bonus points, it makes the image clickable by wrapping it in an <a> element that links to the image URL.

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

	// ==UserScript==
	// @name		  Restore Google Print
	// @namespace	  http://diveintomark.org/projects/greasemonkey/
	// @description	  restore normal browser functionality in Google Print
	// @include		  http://print.google.tld/print*
	// ==/UserScript==

	// restore context menu
	unsafeWindow.document.oncontextmenu = null;

	// remove clear GIFs that obscure divs with background images
	var snapDots = document.evaluate("//img[@src='images/cleardot.gif']",
	    document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
	for(var i = snapDots.snapshotLength - 1; i >= 0; i--) {
	    var elmDot = snapDots.snapshotItem(i);
		var elmWrapper = elmDot.parentNode;
		while (elmWrapper.nodeName.toLowerCase() != 'div') {
		    elmWrapper = elmWrapper.parentNode;
		}
		var urlImage = getComputedStyle(elmWrapper, '').backgroundImage;
		urlImage = urlImage.replace(/url\((.*?)\)/g, '$1');
		// make image clickable
		var elmClone = elmDot.cloneNode(true);
		elmClone.style.border = 'none';
		elmClone.src = urlImage;
		var elmLink = document.createElement('a');
		elmLink.href = urlImage;
		elmLink.appendChild(elmClone);
		elmDot.parentNode.insertBefore(elmLink, elmDot);
		elmDot.parentNode.removeChild(elmDot);
	}


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.