// ==UserScript==
// @name          Google Groups Filter
// @description   Hide google groups search results by group name
// @namespace     http://www.gozer.org/mozilla/greasemonkey/
// @include       http://groups.google.com/groups?*
// @include       http://groups.google.com/groups/search?*
// ==/UserScript==

// http://www.mozdev.org/bugs/show_bug.cgi?id=12838

function GM_GoogleGroups_getPref(prefName, defaultValue)
{
  var retval;

  try {
    retval = GM_getValue(prefName, defaultValue);
  } catch (e) {
    retval = defaultValue;
  }

  if (retval == undefined)
    return null;

  return retval;
}

function GM_GoogleGroups_setPref(prefName, value)
{
  try {
    GM_setValue(prefName, value);
  } catch (e) { }
}

// ----------------------------------------------------------------------------

var GM_GoogleGroups_exclude = '\\.((announce|linux\\.advocacy|ports)$|cvs)';

var exclude = GM_GoogleGroups_getPref('exclude');

if (!exclude)
  GM_GoogleGroups_setPref('exclude', (exclude = GM_GoogleGroups_exclude));

var regex;

try {
  regex = new RegExp(exclude, 'i');
} catch (e) {
  GM_log(e);
  return;
}

var tables = document.getElementById('cbdy').getElementsByTagName('table');

for (var i = 0; i < tables.length; i++) {
  var links = tables[i].getElementsByTagName('a');

  if (links.length) {
    var text;

    try {
      text = links[0].getAttribute('href').match(/^\/group\/([^?]+)/)[1];
    } catch (e) {
      text = null;
    }

    if (text && regex.test(text)) {
      tables[i].style.display = 'none';

      var n = tables[i].nextSibling.nextSibling;

      if (n && n.nodeType == n.ELEMENT_NODE && n.localName == 'BR')
        n.style.display = 'none';
    }
  }
}
