User:2D/afdvote2.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function formatResponse(response) {
    try {
        response = response.query.pages;
        for (var property in response)
            return response[property];
    } catch (e) {
    }
}

function zeroPad(str) 
{
    return ("0" + str).slice(-2);
}

function buildForm()
{
    jsMsg('<div id="voteform"><fieldset style="font-size:10pt">'
            + '<legend>'
                + 'AFD Voting'
            + '</legend>'
            + '<table style="background-color:transparent;color:#000000;font-size:10pt"><br />'
                + '<tr><td>Vote<small>(eg. Delete)</small>: </td><td><input type="text" name="vote" id="vote" size="100" /></td></tr>'
                + '<tr><td>Reason: </td><td><input type="text" name="reason" id="reason" size="100" /></td></tr>'
                + '<tr><td><input type="submit" id="submitData" onclick="vote()" value="Submit" /></td></tr>'
        + '</table>'
    + '</fieldset>');
}
function vote()
{
    document.getElementById("submitData").disabled=true;
    var yourvote = document.getElementById("vote").value;
    var reason = document.getElementById("reason").value;
    if(yourvote == "" || reason == "")
    {
        alert("Please enter a vote and a reason");
        document.getElementById("submitData").disabled=false;
        return;
    }
    var req = new sajax_init_object();
    req.open("GET", wgScriptPath + "/api.php?action=query&prop=info|revisions&format=json&intoken=edit&rvprop=content|timestamp&titles=" + encodeURIComponent(mw.config.get('wgPageName')), false);
    req.send(null);
    var info = formatResponse(eval("(" + req.responseText + ")"));
    var date = new Date();
    var startTime = date.getUTCFullYear() + zeroPad(date.getUTCMonth() + 1) + zeroPad(date.getUTCDate()) + zeroPad(date.getUTCHours()) + zeroPad(date.getUTCMinutes()) + zeroPad(date.getUTCSeconds());
    var editTime = (info.revisions[0] ? info.revisions[0].timestamp.replace(/[^0-9]/g, "") : startTime); 
    var pagecontent = (info.revisions[0]["*"] ? info.revisions[0]["*"] : "");
    var editToken = info.edittoken;
    pagecontent += "\r\n" + "*'''" + yourvote + "''' - " + reason + " ~" + "~" + "~" + "~" + "\r\n";
    pagecontent = encodeURIComponent(pagecontent);
    var postdata = "wpMinoredit=1&wpTextbox1=" + pagecontent + "&wpSummary=" + encodeURIComponent("Voting Using [[User:DavidWS/afdvote2.js|AFDVote]]") + "&wpSave=save&wpEditToken=" + encodeURIComponent(editToken) + "&wpEdittime=" + editTime + "&wpStarttime=" + startTime;
    var req1 = new sajax_init_object();
    req1.open("POST", mw.config.get('wgServer') + wgScript + "?title=" + encodeURIComponent(mw.config.get('wgPageName')) + "&action=submit", false);
    req1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req1.setRequestHeader("Content-Length", postdata.length);
    req1.send(postdata);
    location.reload(true); // Refresh
    return;
}
function start()
{
    if(wgTitle.indexOf("Articles for deletion/") != -1)
    {
        mw.util.addPortletLink("p-cactions","javascript:buildForm()","!vote","ca-vote");
    }
}
addOnloadHook(start);