Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// DESCRIPTION:
// Adds a "speedy" tag to all pages not in the "Special:" or "Media:" namespaces;
// Will tag pages but not delete
//
// TO-DO:
// Add notification capabilities
//------------------------------------------------------------------

importScript('User:Animum/addlimenu.js');

function speedy() {} //For sanity's sake

speedy.zeroPad = function(num) {
    return ("0" + num).slice(-2);
}

speedy.formatResponse = function(obj) {
    try {
        var obj = obj.query.pages;
        for(var index in obj) {
            return obj[index];
        } 
    } catch(e) {
        return obj.query.pages[-1];
    }
}

speedy.capitalize = function(str) {
    return str.substring(0,1).toUpperCase + str.substring(1);
}

speedy.bgColor = function(color) {
    document.getElementById("content").style.backgroundColor = color;
}

speedy.tag = function(template, reason) {
    this.bgColor("#EFE");
    var req = sajax_init_object();
    req.open("GET", wgScriptPath + "/api.php?format=json&action=query&prop=info|revisions&intoken=edit&rvprop=content&titles=" + encodeURIComponent(mw.config.get('wgPageName')), false);
    req.send(null);
    var info = this.formatResponse(eval("(" + req.responseText + ")"));
    var token = info.edittoken;
    var d = new Date();
    var edittime = info.touched.replace(/[^0-9]/g, "");
    var starttime = d.getUTCFullYear() + this.zeroPad(d.getUTCMonth()+1) + this.zeroPad(d.getUTCDate()) + this.zeroPad(d.getUTCHours()) + this.zeroPad(d.getUTCMinutes()) + this.zeroPad(d.getUTCSeconds());
    var content = info.revisions[0]["*"];
    var regexp = new RegExp("{{(" + template + "|" + this.capitalize(template) + ")", "i");

    if(info.missing) { //Bug handler
        jsMsg("This page either has already been deleted or did not exist at the time of this script's execution.");
        return;
    }
    if(content.search(regexp) != -1) { //Bug handler
        jsMsg("This page has already been tagged with \{\{" + template + "\}\}");
        return;
    }
    delete req;

    var postdata = "title=" + encodeURIComponent(mw.config.get('wgPageName'))
                 + "&action=submit"
                 + "&wpTextbox1=" + encodeURIComponent("\{\{" + template + (typeof(reason) != 'undefined' ? "\|" + reason : "") + "\}\}\n\n" + content)
                 + "&wpStarttime=" + starttime
                 + "&wpEdittime=" + edittime
                 + "&wpEditToken=" + encodeURIComponent(token)
                 + "&wpMinoredit=1"
                 + "&wpSummary=" + encodeURIComponent("Tagging with \{\{[[Template:" + template + "\|" + template + "]]\}\}" + (typeof(reason) != "undefined" ? ": " + reason : ""));
    var req = sajax_init_object();
    req.open("POST", wgScript, false);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-length", postdata.length);
    req.setRequestHeader("Connection", "close");
    req.send(postdata);
    if(req.status == 200 && req.readyState == 4) {
        this.bgColor("#EEF");
        window.setTimeout(function() { location.href = location.href; }, 1000); //Reload the webpage.
    }
    delete req;
}

speedy.speedyDeleteTag = function(template) {
    speedy.speedyDeleteTag.submit = function() {
        speedy.tag("speedydelete", document.getElementById("speedyReason").value);
    }
    jsMsg('<table><tr><td>Reason:</td><td><input type="text" id="speedyReason" name="speedyReason" tabindex="1" /></td></tr><tr><td><input type="button" value="Submit" onclick="speedy.speedyDeleteTag.submit()" /></td></tr></table>');
}

$(function() {
    if(wgNamespaceNumber > -1) {
        addlimenu(document.getElementById("p-cactions").getElementsByTagName("ul")[0], "speedy", "ca-speedy", "");
        var speedy = document.getElementById("ca-speedy").getElementsByTagName("ul")[0];
        addlilink(speedy, 'javascript:speedy.speedyDeleteTag("speedydelete")', "speedydelete", "");
        addlilink(speedy, 'javascript:speedy.tag("copyvio")', "copyvio", "");
        addlilink(speedy, 'javascript:speedy.tag("fair use")', "fair use", "");
        addlilink(speedy, 'javascript:speedy.tag("bad name")', "bad name", "");
    }
});