// ==UserScript==
// @name           Google Search Compactor
// @namespace      http://www.bronco.co.uk
// @description    Removes the snippets and adds numbers to the main Google search
// @include        http://www.google.com/search*
// @include        http://www.google.co.uk/search*
// ==/UserScript==

function evaluate(query, root) {
	if (!root) root = document
	return document.evaluate(query, root, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
}

container = document.getElementById("res").getElementsByTagName("div")[0]
list = document.createElement("ol")

start = evaluate("//table[@class='t bt']/tbody/tr/td[@align='right']/font/b").snapshotItem(0).firstChild.nodeValue
list.setAttribute("start", start)

snippets = document.evaluate("//div[@class='g']/table//td/font[@size='-1']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
for (i = 0; i < snippets.snapshotLength; i++) {
	snippet = snippets.snapshotItem(i)
	link = document.evaluate(".//span[@class='a']", snippet, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0)

	if (link) newlink = link.cloneNode(true)
	while (snippet.hasChildNodes())
		snippet.removeChild(snippet.firstChild)
	if (link) {
		newlink.innerHTML = newlink.innerHTML.substring(0, newlink.innerHTML.length-8)
		snippet.appendChild(newlink)
	}
}

while (container.hasChildNodes()) {
	node = container.firstChild
	container.removeChild(node)
	if (node.nodeName == "DIV") {
		item = document.createElement("li")
		item.appendChild(node)
		list.appendChild(item)
	}
}
container.appendChild(list)
