// ==UserScript==
// @name           Confluence: extra page links
// @namespace      http://djce.org.uk/greasemonkey
// @description    Adds extra page links to Confluence wikis
// @include        http://wikis.gateway.bbc.co.uk/confluence/*
// ==/UserScript==

{
	var t = document.getElementById("editPageLink");
	if (t) t = t.getAttribute("href");
	if (t) t = t.match(/pageId=(\d+)/);
	if (t) t = t[1];
	// if (t) alert(t);

	var e = document.getElementById("pagenav");
	var xpathResult;
	if (e) xpathResult = document.evaluate( "./ul", e, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null );
	try {
		if (xpathResult) xpathResult = xpathResult.singleNodeValue;
		// if (xpathResult) alert(xpathResult);
		// if (xpathResult) alert(xpathResult.getAttribute("id"));

		// History
		if (xpathResult)
		{
			var txt1 = document.createTextNode("H");
			var txt2 = document.createTextNode("istory");
			var u = document.createElement("u");
			u.appendChild(txt1);

			var a = document.createElement("a");
			a.setAttribute("href", "/confluence/pages/viewpreviouspageversions.action?pageId="+t);
			a.setAttribute("id", "viewHistoryLink");
			a.setAttribute("accesskey", "h");
			a.appendChild(u);
			a.appendChild(txt2);

			var li = document.createElement("li");
			li.appendChild(a);

			xpathResult.appendChild(li);
		}

		// Remove page
		if (xpathResult)
		{
			var txt1 = document.createTextNode("R");
			var txt2 = document.createTextNode("emove");
			var u = document.createElement("u");
			u.appendChild(txt1);

			var a = document.createElement("a");
			a.setAttribute("href", "/confluence/pages/removepage.action?pageId="+t);
			a.setAttribute("id", "removePageLink");
			a.setAttribute("accesskey", "r");
			a.appendChild(u);
			a.appendChild(txt2);

			var li = document.createElement("li");
			li.appendChild(a);

			xpathResult.appendChild(li);
		}

	} catch (e) {
		alert( 'Error: ' + e );
	}

	// alert("done");
}

if (GM_getValue("expandByDefault", true))
{
	// Default to expanding the "page operations" menu
	var t = document.getElementById("pagenav");
	if (t) t.style.display = "block";
}

