Jump to content

User:2D/afdvote.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 urlencode(plaintext) { //Unicode chars aren't encoded, which is what I want
    var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";
    var HEX = "0123456789ABCDEF";
    var encoded = "";
    for (var i = 0; i < plaintext.length; i++) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "+";
        }
        else if (SAFECHARS.indexOf(ch) !=  - 1) {
            encoded += ch;
        }
        else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                encoded += ch;
            }
            else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    }
    return encoded;
}

function postr(url, vars,type)
{
    var request =  new XMLHttpRequest();
    request.open("POST", url, true);
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.onreadystatechange = function()
    {
        var done = 4, ok = 200;
        if (request.readyState == done && request.status == ok)
        {
            if (request.responseText)
            {
                httpresponse(request.responseText,type);
            }
        }
    };
    request.send(vars);
}

function httpresponse(response,type)
{
    if(type=="content")
    {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(response,"text/xml");
        content = xmlDoc.getElementsByTagName("rev")[0].childNodes[0].nodeValue;
        if(content.indexOf("If you are seeing this page as a result of an attempt to re-nominate") != -1) // Has already been nominated
        {
            nomnum = prompt("This article has had more than one AfD.  What is the one you want to vote on?  Format: 2nd, 3rd, 4th, etc.","2nd");
            nomstring = " (" + nomnum + " nomination)";
            postr("http://en.wikipedia.org/w/api.php","format=xml&action=query&prop=revisions&titles=" + urlencode("Wikipedia:Articles for deletion/") + urlencode(pagename) + urlencode(nomstring) + "&rvprop=content","content");
            return false;
        }
        postr("http://en.wikipedia.org/w/api.php","action=query&prop=info&intoken=edit&format=xml&titles=WP:AFD","token");
    }
    
    if(type=="token")
    {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(response,"text/xml");
        token = xmlDoc.getElementsByTagName("page")[0].attributes.getNamedItem("edittoken").childNodes[0].nodeValue;

        postr("http://en.wikipedia.org/w/api.php","format=xml&action=edit&title=" + urlencode("Wikipedia:Articles for deletion/" + pagename + nomstring) + "&summary=Voting%20using%20AfDVote&text=" + urlencode(content) + urlencode("\r\n*'''" + vote + "''' - " + reason + " ~~~~") + "&token=" + urlencode(token),"edit");
    }
    if(type=="edit")
    {
        //document.write(response);
    }
}
function vote(element)
{
    var firstp = element.textContent.indexOf("(edit");
    firstp--;
    pagename = element.textContent.slice(0,firstp);
    //pagename.replace(" ","_");
    vote = prompt("What would you like to vote (eg. Delete)"); //Yes, it's ugly, but it works, and it doesn't make the script less effective
    reason = prompt("What is your reason for voting " + vote + "?"); //See above
    var tc = confirm("So, to confirm, you want to vote \"" + vote + "\" because \"" + reason + "\"?");
    if(!tc) return false;
    nomstring = "";
    postr("http://en.wikipedia.org/w/api.php","format=xml&action=query&prop=revisions&titles=" + urlencode("Wikipedia:Articles for deletion/") + urlencode(pagename) + "&rvprop=content","content");
}
function addlinks()
{
    var eles = document.getElementsByClassName("plainlinksneverexpand lx");
    for(var i=0; i < eles.length; i++)
    {
        eles[i].innerHTML += "&nbsp;<tt>(</tt><input type='button' onclick='vote(this.parentNode)' value='vote on this AfD'><tt>)</tt>";
    }
}
function start()
{
    if(mw.config.get('wgPageName').indexOf("Wikipedia:Articles_for_deletion/Log/") != -1)
    {
        addlinks();
    }

}

$(start);
-->