// ==UserScript==
// @name        Recent Blog List
// @namespace   http://blogs.sun.com/richb
// @description Script to improve blogs.sun.com list of recent posts display
// @include     http://blogs.sun.com/main/page/recentposts
// ==/UserScript==

// Version: 0.1 - 16th October 2007.
// Author:  Rich Burridge - Sun Microsystems Inc.

// Total number of recent blog entries to display.
var maxBlogEntries = 500;

// Each x element is the HTML built from .../?page=x
var pageArray = new Array();

if (typeof GM_xmlhttpRequest != 'function') {
    // do nothing
} else {
    if (window == top) {
        // Construct a new web page just containing the list of recent
        // blog entries by iterating of all the blogs.sun.com recent 
        // posts web pages that make up this list.
        var newHTML = '<div id="the_list" style="margin:0 auto 0 auto; ' +
                    'text-align:left; position:absolute; ' +
                    'left:15px; top:15px; margin: 5px; padding: 5px; ">\n'

        // There are 25 entries per page. Calculate the number of pages
        // we need to display.
        noOfPages = maxBlogEntries / 25;

        // Add in the header/footer for the new web page.
	pageArray[0] = newHTML;
	pageArray[noOfPages+2] = '<p>\n</div>\n';

        for (var j = 0; j <= noOfPages; j++) {
            var pageUrl = "http://blogs.sun.com/main/page/recentposts" +
                          "?page=" +  j.toString();

            // Passing in 'j' to use as the array index for the page data.
            // We don't know what order the callbacks will finish, or even 
            // start, so this keeps everything in order.
            process_url(j, pageUrl);
        }
    }
}

function process_url(index, pageUrl) {
    GM_xmlhttpRequest({
        method:'GET',
        url:pageUrl,
        onload:function(responseDetails){
            thisPage = document.createElement("div");
            thisPage.innerHTML = responseDetails.responseText

            // Find out the recent list entries on the page.
            var allDivs = document.evaluate(
            ".//div[@class='blockbody']",
            thisPage,
            null,
            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
            null);

            var newHTML = "";

            for (var k = 0; k < allDivs.snapshotLength; k++) {
                thisDiv = allDivs.snapshotItem(k);

                newHTML = newHTML + thisDiv.innerHTML
            }

            // The page is rebuilt as new data arrives (each time the 
            // callback runs).
            pageArray[index+1] = newHTML;
            document.body.innerHTML = pageArray.join('');
        }
    });
}

// ChangeLog
// 16th Oct 2007 - 0.1 - richb - Initial version posted to blog.
