Bookmarklet to clean Sparknotes for printing

Apparently I can’t buy the pdf-versions of the stuff on http://www.sparknotes.com, because I don’t have an american home address? That makes me grumpy. I guess I can just print out the articles myself, though that’s not very convenient.

But OK. To create a nice printable Sparknote page (without the mess, just the text), I use the following javascript code. It extracts the div element that has the className of studyGuideText and removes everything else (including all style sheets and an advert, floatingad).

javascript: ( function() {

var body = document.getElementsByTagName('body')[0];
var divs = body.getElementsByTagName('div');
var text = null;
for(var i=0; i<divs.length; i++){
	if(divs[i].className=='studyGuideText') {
		text = divs[i];
		break;
	}
}
if (text == null) {
	alert('Did not find any text to extract (looking for divs of class studyGuideText)');
	return;
}
while (body.hasChildNodes()){
	body.removeChild(body.firstChild);
}

body.appendChild(text);

for(i=0;i<document.styleSheets.length;i++){
	void(document.styleSheets.item(i).disabled=true);
}
var els=document.getElementsByTagName('*');
for(i=0;i<els.length;i++){
	void(els[i].style.cssText='');
	if(els[i].className=='floatingad'){
		els[i].parentNode.removeChild(els[i]);
	}
}

})();

The bookmarklet version — all in one line etc:

javascript:(%20function()%20{%20var%20body%20=%20document.getElementsByTagName('body')[0];%20var%20divs%20=%20body.getElementsByTagName('div');%20var%20text%20=%20null;%20for(var%20i=0;%20i<divs.length;%20i++){%20if(divs[i].className=='studyGuideText')%20{%20text%20=%20divs[i];%20break;%20}%20}%20if%20(text%20==%20null)%20{%20alert('Did%20not%20find%20any%20text%20to%20extract%20(looking%20for%20divs%20of%20class%20studyGuideText)');%20return;%20}%20while%20(body.hasChildNodes()){%20body.removeChild(body.firstChild);%20}%20body.appendChild(text);%20for(i=0;i<document.styleSheets.length;i++){%20void(document.styleSheets.item(i).disabled=true);%20}%20var%20els=document.getElementsByTagName('*');%20for(i=0;i<els.length;i++){%20void(els[i].style.cssText='');%20if(els[i].className=='floatingad'){%20els[i].parentNode.removeChild(els[i]);%20}%20}%20})();

Tested only with Firefox (Gecko) and Epiphany (Webkit). (I would have liked to just publish it as a link, but WordPress mangles it to something incomprehensible.)

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s