// Minus Sites Version 1.0
// A GreaseMonkey script to weed out Google search results from nuisance
// sites.
//
// Copyright (c) 2007 Nimrod A. Abing
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Minus Sites", and click Uninstall.
//
// ----------------------------------------------------------------------------
//
// ==UserScript==
// @name           Minus Sites
// @namespace      http://abing.gotdns.com/greasemonkey/minus-sites/
// @description    Exclude results from selected sites when searching Google.
// @include        http://www.google.com/*
// @include        http://google.com/*
// @include        http://www.google.com.*/*
// @include        http://google.com.*/*
// ==/UserScript==

// My list of nuisance sites. Add to this list or create your own.
var blocked_sites = [
	'about.com',
	'amazon.com',
	'blogger.com',
	'blogspot.com',
	'digg.com',
	'experts-exchange.com',
	'livejournal.com',
];

window.minusSitesCheckboxes = '<div>Exclude results from the following sites:</div>';
window.minusSitesCheckboxes += '<input type="checkbox" value="about.com" /> ' + 'about.com';

window.minusSitesCheckbox_Clicked = function(event) {
	s = event.target.value;
	t = event.target;
	q = window.minusSitesQuery;
	if (t.checked && -1 == q.value.indexOf(s)) {
		q.value = s + ' ' + q.value;
	}
	else if (!t.checked && -1 != q.value.indexOf(s)) {
		i = q.value.indexOf(s + ' ');
		q.value = q.value.substring(0, i) + q.value.substring(i + s.length + 1, q.value.length);
	}
}

function minusSites () {
	window.minusSitesQuery = q = null;
	felms = window.document.forms[0].elements;
	for (i = 0; i < felms.length; ++i) {
		if ('q' == felms[i].name) {
			window.minusSitesQuery = q = felms[i];
		}
	}
	if (q) {
		mainDiv = document.createElement('div');
		mainDiv.align="left";
		mainDiv.style.padding = "0.5em";
		mainDiv.style.fontSize = '80%';
		d = document.createElement('div');
		d.style.fontWeight = 'bold';
		d.innerHTML = 'Exclude results from:';
		mainDiv.insertBefore(d, mainDiv.nextSibling);
		for (i = 0; i < blocked_sites.length; ++i) {
			ck = document.createElement('input');
			ck.type = 'checkbox';
			ck.value = '-site:'+blocked_sites[i];
			ck.addEventListener('click', window.minusSitesCheckbox_Clicked, false);
			if (-1 != q.value.indexOf(ck.value)) {
				ck.checked = true;
			}
			d = document.createElement('div');
			d.innerHTML = blocked_sites[i];
			d.insertBefore(ck, d.lastChild);
			mainDiv.insertBefore(d, mainDiv.nextSibling);
		}
		q.parentNode.insertBefore(mainDiv, q.nextSibling);
		q.parentNode.nextSibling.style.verticalAlign = 'top';
	}
}

minusSites();

