TWP - The Whole Story

By Brent Charbonneau Last update Oct 3, 2007 — Installed 1,883 times. Daily Installs: 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0
// TWP - The Whole Story
// version 0.4.2
// 2007-10-03
// Copyright (c) 2005-2007, Brent Charbonneau
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "TWP - The Whole Story", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          TWP - The Whole Story
// @namespace     http://projects.apathyant.com/
// @description   Loads and appends all remaining story pages on TelevisionWithoutPity.com recap pages
// @include			http://www.televisionwithoutpity.com/articles/content/*
// @include			http://*.televisionwithoutpity.com/articles/content/*
// @include			http://*.televisionwithoutpity.com/ipdynamic/index.php?component=articles&article=*
// @include			http://televisionwithoutpity.com/portal/site/TelevisionWithoutPity/menuitem*
// @include			http://*.televisionwithoutpity.com/portal/site/TelevisionWithoutPity/menuitem*
// ==/UserScript==

function loadStoryPage(showName, page, maxPage, nextHref) {
	//pageHref = document.location.protocol + '//' + document.location.host + (document.location.port ? ':' + document.location.port : '');
	//pageHref += '/articles/content/a' + articleID + '/index-' + (page-1) + '.html';
	pageHref = getStoryPageURL(showName, page, nextHref);
	GM_log('Fetching ' + pageHref);
	
	GM_xmlhttpRequest
	   ({
	   method:'GET',
	   url: pageHref,
	   onload:function(response) {
			var elems, storytext;
			var html = response.responseText;
			/*var parser = new DOMParser();
			GM_log(response.responseText.replace(/(<title>([^<]+)<\/title>)|(<meta[^>]+>)/i,''));
			return;
			var xmlDoc = parser.parseFromString(response.responseText.replace(/<head>([^<]+)*<\/title>/,''), "application/xml");

			/*elems = xmlDoc.getElementsByTagName('span');
			for(var elemCount = 0; elemCount < elems.length; elemCount++)
				if ( elems[elemCount].className=='story_text' ) {
					storytext = elems[elemCount];
					break;
				}
			storytext = xmlDoc.evaluate(
				'//span[@class="story_text"]',
				xmlDoc,
				null,
				XPathResult.FIRST_ORDERED_NODE_TYPE,
				null);
			*/

			// Since the site isn't xhtml, we're going to have to
			// substr out the story, instead of being able to use the DOM to find it
			var spanstart = html.indexOf('>', html.indexOf('style="line-height: 25px;"'))+1;
			var spanend = html.indexOf('</td>', spanstart);
			var span = html.substring(spanstart, spanend);
			storytext = span;
			if (storytext) {
				document.getElementById('story-page-' + page).innerHTML = '<span class="story_text"><hr><div style="text-align: right;">pg ' + page + ' of ' + maxPage + '</div>' + storytext + '</span>';
				if ( page < maxPage ) { 
					loadStoryPage(showName, page+1, maxPage, nextHref);
				}
			}else{
			}
			
		}});
}

function getStoryPageURL(showName, page, nextHref) {
	return 'http://televisionwithoutpity.com' + nextHref.replace(/&currentPage=[0-9]+&/, '&currentPage=' + page + '&');
	/*
	linkurl =window.location;
	linkurl = linkurl.toString();
	iend=linkurl.indexOf('&S');
	term=linkurl.substring(0,iend);
	var url= term+"&ShowName=" + showName;
	url=url+"&currentPage="+page;
	return url;	*/
}


var firstpage = document.evaluate(
	'//font/a[normalize-space(text()) = \'Next\']/../../../../../preceding-sibling::*[1]',
	document,
	null,
	XPathResult.FIRST_ORDERED_NODE_TYPE,
	null);


if ( firstpage ) {
	firstpage = firstpage.singleNodeValue;
}	


if(!firstpage) return;



// Clear up some ads...
var ad;
var adPaths = new Array('//table[@width="170"]', '//td[@width="760"]/../..');
for(var adCount = 0; adCount < adPaths.length; adCount++) {
	ad = document.evaluate(adPaths[adCount], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	if (ad) ad.style.display = 'none';
}

document.styleSheets[0].insertRule('#km_main_div, #km_main_div30829 {display: none}',document.styleSheets[0].cssRules.length);
document.styleSheets[0].insertRule('.story_text, .story_text font { font-family: verdana,arial,helvetica,sans-serif !important; font-size: 13px; line-height: 25px !important; }', document.styleSheets[0].cssRules.length);

document.styleSheets[0].insertRule('#boo iframe { width: 75px !important; }', document.styleSheets[0].cssRules.length);
document.styleSheets[0].insertRule('#boo iframe img { width: 100px !important; }', document.styleSheets[0].cssRules.length);
document.styleSheets[0].insertRule('#boo td#woo { width: 75px !important; }', document.styleSheets[0].cssRules.length);

var centerTable = document.evaluate('//td[@width="78%"]/../../..', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (centerTable) {
//	centerTable.style.width = '865px';
//	centerTable.style.marginLeft = 'auto';
//	centerTable.style.marginRight = 'auto';
	centerTable.id = 'boo';
}

var sideBar = document.evaluate('//table[@id="boo"]//td[@width="22%"]', document, null,XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (sideBar) {
	sideBar.id = 'woo';
	sideBar.style.width = '75px';
}


var pageLinkBox = document.evaluate('//span[@class="pagelink"]/../..', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if(pageLinkBox){
	pageLinkBox.style.display = 'none';
}

var showFaqLink = document.evaluate('//a/font[normalize-space(text()) = \'Show FAQ\']/..', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

if(showFaqLink) {
	var showNameMatches = showFaqLink.href.match(/&ShowName=([^&]+)/i);
	if(showNameMatches.length) var showName = showNameMatches[1];

	if(!showName) return;
}

var anchorTextBeforeNextLink = document.evaluate('//font/a[normalize-space(text()) = \'Next\']/../preceding-sibling::*[1]/a/child::text()', document, null, XPathResult.STRING_TYPE, null);
if(anchorTextBeforeNextLink) {
	totalPages = parseInt(anchorTextBeforeNextLink.stringValue);
	if(!totalPages) return;

}

var nextHref = document.evaluate('//font/a[normalize-space(text()) = \'Next\']/attribute::href', document, null, XPathResult.STRING_TYPE, null);
if(nextHref) {
	nextHref = nextHref.stringValue;
}
if(!nextHref) return;

var startPage;
var currentPageMatches = window.location.href.match(/&currentPage=([0-9]+)/i);
if(currentPageMatches && currentPageMatches.length) { 
	startPage = parseInt(currentPageMatches[1]);
}
if(!startPage) startPage = 1;

var appendedPageElem;


// hide the pager
var pager= document.evaluate(
	'//font/a[normalize-space(text()) = \'Next\']/../../../../..',
	document,
	null,
	XPathResult.FIRST_ORDERED_NODE_TYPE,
	null);

if(pager)
	pager.singleNodeValue.style.display = 'none';

if(startPage && totalPages && startPage < totalPages){
	for(var pageCounter = totalPages; pageCounter > startPage; pageCounter--){
		appendedPageElem = document.createElement('div');
		appendedPageElem.id = 'story-page-' + pageCounter;
		appendedPageElem.innerHTML = '<span style="text-decoration: blink; font-size: 8pt;">Loading page ' + pageCounter  + '...</span>';
		firstpage.parentNode.insertBefore(appendedPageElem, firstpage.nextSibling);
		appendedPageElem = null;
	}
	loadStoryPage(showName, startPage+1, totalPages, nextHref);
}