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.
//Written by [[User:Animum]]
importScript("User:Animum/formatresponse.js");

var warn = {
    "edit": 
        function(reason) {
            if(!reason) {
                document.getElementById("contentSub").innerHTML = "<br /><b>No reason was provided.</b>";
                return; //Abort if no reason was provided.
            }
            var req = sajax_init_object();
            req.open("POST", wgScriptPath + "/api.php", true);
            var postdata = "format=json"
                         + "&action=query"
                         + "&prop=info"
                         + "&intoken=edit" /* Request edit token from the API */
                         + "&titles=" + wgPageName;
            req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            req.setRequestHeader("Content-length", postdata.length);
            req.send(postdata);
            delete postdata;
            req.onreadystatechange = function() {
                if(req.status == 200 && req.readyState == 4) {
                    document.getElementById("content").style.backgroundColor = "#EFE"; //Change the document's background color
                    var info = formatResponse(eval("(" + req.responseText + ")")); //Format the response to make it usable
                    var postdata = "action=edit"
                                 + "&title=" + wgPageName
                                 + "&summary=" + encodeURIComponent("Notifying user of possible username violation.  Reason: " + reason + ".")
                                 + "&minor"
                                 + "&appendtext=" + encodeURIComponent("\n== Hello! ==\n\n\{\{uw-username\|" + reason + "\}\} \~\~\~\~")
                                 + "&token=" + encodeURIComponent(info.edittoken);
                    var req2 = sajax_init_object();
                    req2.open("POST", wgScriptPath + "/api.php", true);
                    req2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    req2.setRequestHeader("Content-length", postdata.length);
                    req2.send(postdata); //Submit the edit to the API
                    req2.onreadystatechange = function() {
                        if(req2.status == 200 && req2.readyState == 4) {
                            document.getElementById("content").style.backgroundColor = "#EEF"; //Change the document's background color
                            window.setTimeout(function() { location.href = location.href; }, 500); //Reload the page
                        }
                    }
                }
            }
        },
    "init":
        function() { 
            document.getElementById("contentSub").innerHTML = '<br /><table style="background-color: transparent; font-size: 10pt"><tr><td>Reason: </td><td><input type="text" id="wpWarnReason" /></td></tr><tr><td><input type="button" value="Submit" id="wpWarnSubmit" /></table>'; //Build the form
            document.getElementById("wpWarnSubmit").onclick = function() {
                var wpWarnReason = document.getElementById("wpWarnReason");
                document.getElementById("wpWarnSubmit").disabled = true;
                wpWarnReason.disabled = true;
                warn.edit(wpWarnReason.value); //Start the edit process
            }
        }
};
 
$(function() {
    if(wgNamespaceNumber == 3 && wgPageName.indexOf("/") == -1) mw.util.addPortletLink("p-cactions", "javascript:warn.init()", "uw-name", "ca-usernamewarning"); //Add the tab
});