// ==UserScript==
// @name           Dreamwidth Random Bug Picker
// @namespace      http://afunamatata.com/greasemonkey/
// @description    Pick a random bug from Dreamwidth's bugzilla
// @include        http://bugs.dwscoalition.org/buglist.cgi?*
// @tags           dreamwidth
// ==/UserScript==

var choose = document.createElement("a");
choose.setAttribute("href", "#");
choose.appendChild(document.createTextNode("Spice up your life. Pick a bug at random."));
choose.addEventListener("click", function(e) {
        var bugs = document.evaluate("//tr[contains(@class, 'bz_bugitem')]/td/a", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

        if(bugs.snapshotLength > 0) {
            var bug = bugs.snapshotItem( 
                    Math.floor( Math.random()*bugs.snapshotLength) );
            e.target.setAttribute("href", bug.href);
        }

    }, false);

var insert = document.evaluate("//span[@class='bz_result_count']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
insert.appendChild(document.createTextNode(" "));
insert.appendChild(choose);
