ArsTechnica - Multi-page viewer

By Christopher Campbell Last update Jan 18, 2006 — Installed 1,039 times.
// ArsTechnica - Multi-page viewer
// version 0.1
// 2006-01-18
// Copyright (c) 2006, Christopher Campbell
// Based on ArsTechnica - Single Page View by Julien Couvreur
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
// --------------------------------------------------------------------
//
// ==UserScript==
// @name            ArsTechnica - Multi-page viewer
// @namespace	http://userscripts.org
// @description    Combines the multi-page articles on ArsTechnica.com into single-page views.
// @include         http://arstechnica.com/*
// @include         http://*arstechnica.com*
// ==/UserScript==

// make a url with page parameter incremented
function makeNextUrl(baseUrl, pageNumber) {
    var nextUrl = baseUrl + "/" + pageNumber;
    return nextUrl;
}

var xpathNext = "//a[@id='_ctl4_NextPageLink']";

function getNextPage(href, pageNumber) {   
    var loadingHref = makeNextUrl(href, pageNumber);
	GM_xmlhttpRequest({
		method: 'GET',
		url: loadingHref,
		onload: function(responseDetails) {
		    var iframeOnload = function() {
	            var iframeDoc = iframe.contentDocument;
	            iframeDoc.getElementsByTagName("body")[0].innerHTML = responseDetails.responseText;
	            
	            // stop condition
	            var getMore = false;
	            if (getNode(iframeDoc, xpathNext)) {
	                getMore = true;
	            }

	            injectContent(iframeDoc);
	            
	            if (getMore) {
	                getNextPage(href, pageNumber+1);    
	            }
			};
        
			var iframe = createIframe(iframeOnload);
		}
	});
}



function injectContent(iframeDoc) 
{
    cleanIFrame(iframeDoc);
    
    // Take the interesting part out of the iframe
    // and stick it right after the interesting part in the main document
    var xpath = "//form[last()]";
    var dest = document.evaluate(xpath, document, null,
                                XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    if (dest.snapshotLength > 0) 
    {    
        var source = document.evaluate(xpath, iframeDoc, null,
                                    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
        if (source.snapshotLength > 0) 
        {
            var destNode = dest.snapshotItem(dest.snapshotLength - 1);
            destNode.parentNode.insertBefore(source.snapshotItem(1), destNode.nextSibling);
        }
    }
}

function getNode(iframeDoc, xpath) {
    var source = document.evaluate(xpath, iframeDoc, null,
                                    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    if (source.snapshotLength > 0) {
        return source.snapshotItem(0);
    }
}

function cleanIFrame(iframeDoc) 
{
    var node1 = getNode(iframeDoc, "//h1");
    if (node1) {
        node1.parentNode.removeChild(node1);
    }
    
    var node2 = getNode(iframeDoc, "//div[@class = 'ArticlePageSelector']");
    if (node2) {
        node2.parentNode.removeChild(node2);
    }
    
    var node3 = getNode(iframeDoc, "//p[@class = 'PageAuthors']");
    if (node3) {
        node3.parentNode.removeChild(node3);
    }
    
    var node4 = getNode(iframeDoc, "//p[@class = 'PostDate']");
    if (node4) {
        node4.parentNode.removeChild(node4);
    }
}

function createIframe(onload) {
    var iframe = document.createElement("iframe");
    iframe.addEventListener("load", onload, false);
    iframe.src = "about:blank";
    iframe.style.width = '000px';
    iframe.style.height = '00px';
    iframe.style.border = '0px';    
    document.getElementsByTagName('body')[0].appendChild(iframe);
    return iframe;
}


// ******************************* Main logic ******************************* //


// if this is the first page of an article...
if (document.location.href.match(/\.ars$/) != null) 
{
	// and if there is a next pages...
    if (getNode(document, xpathNext)) {
        // load and inject following page
		getNextPage(document.location.href, 2);
    }
}