Digg Top 10 Direct

By Matthew Botos Last update Aug 30, 2007 — Installed 886 times. Daily Installs: 0, 2, 1, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 3, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1
// ==UserScript==
// @name           Digg Top 10 Direct
// @description    Link Top 10 directly to articles
// @include        http://digg.com/*
// ==/UserScript==
//
// Copyright (c) 2007, Matthew Botos (http://matthew.botos.com)
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// ================
//
// This is a Greasemonkey user script: http://greasemonkey.mozdev.org/
// To use it, install Greasemonkey, restart Firefox, revisit this script, and click on install.
//
// ================

var appKey = escape('http://userscripts.org/scripts/show/9004')
var debug = false;
var links = new Array();

var allDivs = document.evaluate(
    "//div[@id='topten-list']",
    document,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null);
var topTen = allDivs.snapshotItem(0);

// loop through news item divs
for (var i = 0; i < topTen.childNodes.length; i += 1) {
    var child = topTen.childNodes[i];
		if (child.tagName == 'DIV') {
			link = child.childNodes[0].childNodes[0];
			if (debug) GM_log('Digg Link: ' + link);
			var storyName = link.href.match(/.*\/(.*)/)[1];
			if (debug) GM_log('Top Ten Story: ' + storyName);
			links[storyName] = link;
			apiLink = 'http://services.digg.com/story/' + storyName + '?appkey=' + appKey + '&type=json';
			if(debug) GM_log('API Link: ' + apiLink)

			// get the page and substitute the direct story link
			GM_xmlhttpRequest({
				method: 'GET',
				url: apiLink,

				onload: function(responseDetails) {
					var response = eval( '(' + responseDetails.responseText + ')' );
					var storyUrl = response.stories[0].link;
					var storyName = response.stories[0].href.match(/.*\/(.*)/)[1];
					if (debug) GM_log('Story URL: ' + storyUrl);
					if (debug) GM_log('Story Name: ' + storyName);
					links[storyName].href = storyUrl;
				}
			});

    }
}