// ==UserScript==
// @name           LP_Ubuntu_Bugs_Only
// @namespace      http://outflux.net/greasemonkey/
// @description    (Launchpad) List Ubuntu Bugs Only
// @include        https://*.launchpad.net/*
// @include        https://launchpad.net/*
// @version        0.2
// @date           2007-07-20
// @creator        Kees Cook <kees@ubuntu.com>
// ==/UserScript==

(function () {
  var SCRIPT = {
    name: "LP_Ubuntu_Bugs_Only",
    namespace: "http://outflux.net/greasemonkey/",
    description: '(Launchpad) List Ubuntu Bugs Only',
    source: "http://outflux.net/greasemonkey/",
    identifier: "http://outflux.net/greasemonkey/lp_ubuntu_bugs.user.js",
    version: "0.2",
    date: (new Date(2007, 7 - 1, 20))// update date
    .valueOf()
  };

function xpath(query, context) {
  context = context ? context : document;
  return document.evaluate(query, context, null,
                           XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}


window.addEventListener("load", function(e) {

    // track how many we dropped
    var removed = 0;

    // find all the non-Ubuntu bugs and drop them
    var rows = xpath('//table[@id="buglisting"]//tr/td[position()=4 and contains(.,"(") and not(contains(.,"(Ubuntu"))]/..');

    for (var i = 0; i < rows.snapshotLength; i++) {
        var node = rows.snapshotItem(i);
        node.parentNode.removeChild(node);
        removed += 1;
    }

    if (removed) {
        // find bug number summary
        var counts = xpath('//div[@id="maincontent"]//div[@class="lesser"]//tr[1]/td[1]');
        for (var i = 0; i < counts.snapshotLength; i++) {
            var summary = counts.snapshotItem(i);

            // reduce the final bugs-on-this-page count
            var thiscount = xpath('.//strong[2]', summary).snapshotItem(0).childNodes[0];
            thiscount.nodeValue -= removed;

            // reduce the total bugs found count
            var maxcount = summary.childNodes[4];

            /* This isn't really sane -- let's leave the total count alone

            var regex = new RegExp('([0-9]+)');
            var text = maxcount.nodeValue;

            // find the number
            var match = regex.exec(text);

            // rebuild the count
            maxcount.nodeValue = text.substring(0,match.index) + (match[0]-removed) + text.substring(match.index+match[0].length);
            */

            maxcount.nodeValue += " (filtered out " + removed + " non-Ubuntu bug" + (removed == 1 ? "" : "s") + " from this page)";
        }
    }

}, false);

})();

