User:Volte/cologneblue.js

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes.

  • Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
  • Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
  • Opera, Konqueror and Safari users can just click the Reload button.
  • Chrome: press Ctrl+F5 or Shift+F5
//<nowiki>

function main() {
  addLinks();
  addRevert();
  addHistoryEdit();
  addEditLinks();
  setEditForm();
};

function addEditLinks() {
    var re = RegExp("Editing (.*) - Edit");
    var matches;

    if (matches = re.exec(document.title)) {
        var quickBar = document.getElementById('quickbar');

        if (!quickBar) return;

        var header = document.createElement("h6");
        header.textContent = "Edit Stuff";

        quickBar.appendChild(header);
        addSidebarLink(quickBar, "Wikipedia", "http://en.wikipedia.org/wiki/" + matches[1]);
        addSidebarLink(quickBar, "Google", "http://www.google.com/search?sourceid=mozclient&ie=utf-8&oe=utf-8&q=" + matches[1]);
        var nrv = addSidebarLink(quickBar, "NRV", "#");

        nrv.addEventListener("click", makeNrv, false);
    }
};

function addHistoryEdit() {
    var pageHistory = document.getElementById('pagehistory');
    if (!pageHistory) return;

    var links = document.evaluate("//li[input[@type='radio']]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
    var xp_name1 = document.evaluate("//li[input[@type='radio']][1]/descendant::a[last()]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    if (!xp_name1) return;
  
    var name1 = xp_name1.singleNodeValue;
    var curLink;
    var i;
  
    for (i = 0; i < links.snapshotLength; i++) {
        var n = i + 1;
        curLink = links.snapshotItem(i);
    
        var xp_name2 = document.evaluate("//li[input[@type='radio']][" + n + "]/descendant::input/following::a[2]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);  
        if (!xp_name2) return;
    
        var xp_link = document.evaluate("//li[input[@type='radio']][" + n + "]/descendant::input/following::a[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);  
        if (!xp_link) return;
    
        var name2 = xp_name2.singleNodeValue;
        var link = xp_link.singleNodeValue;
	
        if (i == 0) {
            curLink.innerHTML = '(edit) ' + curLink.innerHTML;
        } else {
            curLink.innerHTML = '(<span class="plainlinks"><a href="' + link.href + '&action=edit&setwatch=0&setsummary=Reverted edit of [[User:' + name1.textContent + '|' + name1.textContent + ']] to previous version by ' + name2.textContent + '">edit</a></span>) ' + curLink.innerHTML;
        }
    } 
};  

function makeNrv() {
    var editarea = document.getElementsByName("wpTextbox1")[0];
    if (editarea) editarea.value += "\n\n{{NRV|~~~~~}}";

    var summary = document.getElementsByName("wpSummary")[0];
    if (summary) summary.value = "+NRV";

    var watch = document.getElementById("wpWatchthis");
    if (watch) watch.checked = false;
};

function addLinks() {
    var quickBar = document.getElementById('quickbar');

    if (!quickBar) return;

    var header = document.createElement("h6");
    header.textContent = "Custom Links";

    quickBar.appendChild(header);

    addSidebarLink(quickBar, "VFH", "http://uncyclopedia.wikia.com/wiki/Uncyclopedia:VFH");
    addSidebarLink(quickBar, "VFP", "http://uncyclopedia.wikia.com/wiki/Uncyclopedia:VFP");
    addSidebarLink(quickBar, "Village Dump", "http://uncyclopedia.wikia.com/wiki/Uncyclopedia:Village_Dump");
    addSidebarLink(quickBar, "Custom JavaScript", "http://uncyclopedia.wikia.com/index.php?title=User:Volte/cologneblue.js&action=edit");
};

function addSidebarLink(quickBar, text, href) {
    var link = document.createElement("a");
    link.textContent = text;
    link.href = href;

    quickBar.appendChild(link);
    quickBar.appendChild(document.createElement("br"));

    return link;
};

function addRevert() {
    if(!queryString("diff")) return;
	
    var xp_link = document.evaluate("//a[starts-with(., 'Revision')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    if (!xp_link) return;
	
    var link = xp_link.singleNodeValue;
    
    var xp_name1 = document.evaluate("/descendant::a[.='Talk'][1]/preceding-sibling::a", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    if (!xp_name1) return;
    
    var xp_name2 = document.evaluate("/descendant::a[.='Talk'][2]/preceding-sibling::a", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    if (!xp_name2) return;
    
    var name1 = xp_name1.singleNodeValue;
    var name2 = xp_name2.singleNodeValue;

    var linkSpan = document.createElement("span");
    var revertLink = document.createElement("a");
    var summaryText = "Reverted edit of [[Special:Contributions/" + name2.textContent + "|" + name2.textContent + "]] to previous one by " + name1.textContent

    linkSpan.innerHTML = "&nbsp;&nbsp;";

    revertLink.innerHTML = "[Auto-Revert]";
    revertLink.href = link.href + "&action=edit&setsubmit=wpSave&setsummary=" + summaryText + "&setwatch=0";

    insertAfter(link.parentNode, linkSpan, link);
    insertAfter(link.parentNode, revertLink, linkSpan);
};

function insertAfter(parent, node, referenceNode) {
    if(referenceNode.nextSibling) {
        return parent.insertBefore(node, referenceNode.nextSibling);
    } else {
        return parent.appendChild(node);
    }
}

function setEditForm() {
	var editform = document.getElementById("editform");
	if (!editform) return;
	
	var summaryText = queryString('setsummary');
	if (summaryText) {
		var summary = document.getElementsByName("wpSummary")[0];
		if (summary) summary.value = summaryText;
	}
	
	var setMinor = queryString('setminor');
	if (setMinor) {
		var minor = document.getElementById("wpMinoredit");
		if (minor) minor.checked = (setMinor == '1');
	}
	
	var setWatch = queryString('setwatch');
	if (setWatch) {
		var watch = document.getElementById("wpWatchthis");
		if (watch) watch.checked = (setWatch == '1');
	}

	var setSubmit = queryString('setsubmit');
	if (setSubmit) {
		var btn = document.getElementById(setSubmit);
	
		if (document.getElementById("editform") && btn) {
			btn.textContent += " (auto clicking...)";
			btn.click();
		}
	}
};

function queryString(p) {
	var re = RegExp('[&?]' + p + '=([^&]*)');
  
	var matches;
  
	if (matches = re.exec(document.location)) {
		try { 
			return decodeURI(matches[1]);
		} catch (e) {
		}
	}
  
	return null;
};

window.addEventListener("load", main, false);

//</nowiki>