// ==UserScript==
// @name          Digg to del.icio.us
// @namespace     http://daveg.outer-rim.org
// @description	  Add "save to del.icio.us" link to digg entry. Inspired by "delicious >> my web" script by Jason Rhyley.
// @include       http://digg.com/*
// @exclude       http://digg.com/users/*
// ==/UserScript==

function gm_xpath(what, where) {
    return document.evaluate(what,where,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);	
}

    allH3 = gm_xpath("//h3", document);
    allDesc = gm_xpath("//p[@class='news-submitted']",document);
    allMeta = gm_xpath("//div[@class='news-details']",document);
    allTag = gm_xpath("//span[@class='tool']",document);

    for (i = 0; i < allH3.snapshotLength; i++) {
	if (allH3.snapshotItem(i).firstChild.nodeType == 3) {
            theURL = allH3.snapshotItem(i).firstChild.nextSibling.href;
       	    theTitle = allH3.snapshotItem(i).firstChild.nextSibling.innerHTML;
            theDesc = allDesc.snapshotItem(i).nextSibling.nextSibling.innerHTML;
            theTag = allTag.snapshotItem(i).firstChild.nextSibling.innerHTML;
        } else {
            theURL = allH3.snapshotItem(i).firstChild.href;
       	    theTitle = allH3.snapshotItem(i).firstChild.innerHTML;
            theDesc = allDesc.snapshotItem(i).nextSibling.innerHTML;
            theTag = allTag.snapshotItem(i).nextSibling.innerHTML;
        }
	meta = allMeta.snapshotItem(i);
	delURL = 'http://del.icio.us/api/posts/add?'
	    + '&url='  + escape(theURL)
	    + '&description=' + escape(theTitle)
	    + '&extended=' + escape(theDesc)
            + '&tags=digg ' + escape(theTag)
            + '&replace=no'

        delLink = '<a class="tool" title="save this to del.icio.us" href="'+delURL+'">save del.icio.us</a>';
	meta.innerHTML = meta.innerHTML + delLink;
    }
