Marc Ermshaus’ avatar

Marc Ermshaus

Linkblog

Greasemonkey: Auto-"read on one page" for news sites

Published on 24 Sep 2010.

Here's a very basic Greasemonkey script that enables the "read on one page" feature by default for a number of news sites. It currently works with nytimes.com and zeit.de. (Yes, that's two. Yes, that's not a huge number.)

// ==UserScript==
// @name           read on one page
// @namespace      http://ermshaus.org/
// @include        *
// ==/UserScript==

(function()
{
    function getParam(name)
    {
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var tmpURL = window.location.href;
        var results = regex.exec(tmpURL);

        if (results == null) {
            return "";
        }

        return results[1];
    }

    if (window.location.hostname === 'www.zeit.de') {
        if (getParam('page') === '') {
            var snapshot = document.evaluate(
                '//a[@href="?page=all"]',
                document,
                null,
                XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
                null
            );
            if (snapshot.snapshotLength >= 1) {
                window.location.href += '?page=all';
            }
        }
    } else if (window.location.hostname === 'www.nytimes.com') {
        if (getParam('pagewanted') === '') {
            var snapshot = document.evaluate(
                '//a[contains(@href, "pagewanted=all")]',
                document,
                null,
                XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
                null
            );
            if (snapshot.snapshotLength >= 1) {
                window.location.href += '&pagewanted=all';
            }
        }
    }
})();

Install via Greasemonkey auto-detection.

There's a lot of room for improvement. For instance, the redirect should be executed earlier than the time at which Greasemonkey runs ("DOM ready" event). That would need to be done with a real Firefox extension, I guess. Besides that, the XPath expression that is used to detect whether there's a "read on one page" version is quite hacky and will break easily. There certainly are much better scripts around, I didn't check.

Nevertheless, it's much more useful than I thought.

I might update the script and this post at some point in the future, but don't hold your breath.