User:Awesome Aasim/editrequest.js/stable.js

/* FUNCTION DEFINITIONS */ // AER.pages = []; AER.requestPages = ["Category:Wikipedia_semi-protected_edit_requests", "Category:Wikipedia_extended-confirmed-protected_edit_requests", "Category:Wikipedia_template-protected_edit_requests", "Category:Wikipedia_fully-protected_edit_requests", "Category:Wikipedia_interface-protected_edit_requests"]; AER.pageCounter = 0; AER.currentPage = ""; AER.currentTalkPage = ""; AER.wikitext = ""; AER.disableEditor = function { $("#edit-editarea").prop("disabled", true); $("#edit-editsummary").prop("disabled", true); $("#edit-cancel").prop("disabled", true); $("#edit-previewbutton").prop("disabled", true); $("#edit-submit").prop("disabled", true); }; AER.enableEditor = function { $("#edit-editarea").prop("disabled", false); $("#edit-editsummary").prop("disabled", false); $("#edit-cancel").prop("disabled", false); $("#edit-previewbutton").prop("disabled", false); $("#edit-submit").prop("disabled", false); }; AER.editForm = function(d, p, _then) { $(".editpage").prop("disabled", true); $(d).removeAttr('style'); var revert = $(d).html; $(d).html(" Loading editor... "); $.get(mw.config.get('wgScriptPath') + '/api.php', {       "action": "query",        "format": "json",        "meta": "tokens",        "type": "csrf"    }).done(function (r) {    	$.get(mw.config.get('wgScriptPath') + '/api.php', { "action": "parse", "format": "json", "prop": "wikitext", "page": AER.currentPage, "type": "csrf" }).done(function(wt) { if (wt.error) { if (wt.error.code == "missingtitle") { AER.wikitext = ""; } else { alert("Error while loading editor: " + wt.error); location.reload; }   		} else { AER.wikitext = wt.parse.wikitext["*"]; $(d).text(""); $(' ').appendTo(d); $(' Full editor ').appendTo("#AEReditform"); $('').val(AER.wikitext).appendTo("#AEReditform").css({ "height": "500px", "overflow": "auto" }); $(' ').appendTo("#AEReditform"); $(' ').html('Edit summary:  Processing edit requests with AER tool: ').append($('').val("")).appendTo("#AEReditform"); $(' ').appendTo("#AEReditform"); $(' ').appendTo("#AEReditform"); $('Cancel ').click(function (_e) {		           $(".editpage").prop("disabled", false);		            $(d).css({ "width": "100%", "height": "500px", "overflow": "auto" });		            $(d).html(revert);		        }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-destructive").appendTo("#AEReditform"); $('Preview ').click(function (_e) {		           AER.disableEditor;		            $.get(mw.config.get('wgScriptPath') + '/api.php', { "action": 'parse', "prop": 'text', "pst": 'true', // PST = pre-save transform; this makes substitution work properly "text": $("#edit-editarea").val, "summary": "Processing edit requests with AER tool" + (($("#edit-editsummary").val.length > 0) ? ": " + $("#edit-editsummary").val : ""), "format": "json", "title": p		           }).done(function (r) { console.log(r); if (r.error) { console.error(r.error); } else { $("#edit-preview").html(r.parse.text["*"]); $("#edit-summary").html(r.parse.parsesummary["*"]); }		               AER.enableEditor; }).fail(function (_r) { $("#edit-preview").html("Could not parse preview. Please check your edit carefully.  If in doubt, use the full editor. "); AER.enableEditor; });		       }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").appendTo("#AEReditform"); $('Save ').click(function (_e) {		           AER.disableEditor;		            $.post(mw.config.get("wgScriptPath") + '/api.php', { "action": "edit", "format": "json", "title": p,		               "text": $("#edit-editarea").val, "summary": "Processing edit requests with AER tool" + (($("#edit-editsummary").val.length > 0) ? ": " + $("#edit-editsummary").val : ""), "token": r.query.tokens.csrftoken }).done(function (h) { if (h.error) { $("   ").text(h.error.info).appendTo(d); } else if (h.edit.nochange == "") { $(".editpage").prop("disabled", false); $(d).css({ "width": "100%", "height": "500px", "overflow": "auto" }); $(d).html(revert); } else { $(d).html(" Edit saved! "); _then; }		           }).fail(function (_h) { });		       }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-progressive").appendTo("#AEReditform"); $(' ').appendTo("#AEReditform"); }   	}).fail(function(wt) { });

}).fail(function (r) { throw "Unknown error"; }); }; AER.shuffle = function(arra1) {   var ctr = arra1.length, temp, index;

// While there are elements in the array while (ctr > 0) { // Pick a random index index = Math.floor(Math.random * ctr); // Decrease ctr by 1 ctr--; // And swap the last element with it       temp = arra1[ctr]; arra1[ctr] = arra1[index]; arra1[index] = temp; }   return arra1; } // from w3 AER.run = function(f) { AER.disableButtons; AER.disableEditor; $("#editrequest-page").css({ "width": "100%", "height": "500px", "overflow": "auto" }); if (f) { $("#editrequest-info").html(" Loading next request... "); $("#editrequest-info-2").html(" Loading next request... "); }   try { if (AER.pages[AER.pageCounter][0]) { AER.currentTalkPage = AER.pages[AER.pageCounter][0]; AER.currentSection = AER.pages[AER.pageCounter][1]; AER.currentPage = AER.pages[AER.pageCounter][0].replace(/(T|(\b|_)t)alk:/g, ':'); if (AER.currentPage[0] == ":") { AER.currentPage = AER.currentPage.substring(1, AER.currentPage.length); } else { AER.currentPage = AER.currentPage.replace(" :", ":"); }           console.log(AER.currentPage); $.get(mw.config.get("wgScriptPath") + "/api.php", {               "action": "query",                "format": "json",                "prop": "revisions",                "titles": AER.currentTalkPage,                "formatversion": "2"            }).done(function (a) {                if (a.error) {

} else { currentRevID = a.query.pages[0].revisions[0].revid; parse(AER.currentTalkPage, "#editrequest-talk", function (d) {                       parse(AER.currentPage, "#editrequest-page", function (r) { $("#editrequest-talk").html(d); if (AER.AERSettings.eph) { // need rewrite to allow for quick acceptance and decline }                           if (document.getElementById(AER.pages[AER.pageCounter][1])) { $("#editrequest-page").html(r); AER.enableButtons; $("#editrequest-info").html(' Can you answer the edit request below? '); $("#editrequest-info-2").html(""); $(".editrequest-title").text(AER.currentPage).append(" (" + (AER.pageCounter + 1) + "/" + AER.pages.length + ")"); AER.pageCounter++; $("#editrequest-page a").each(function {                               	if (!($(this).attr("href") ? $(this).attr("href") : "").includes("#")) {                                		$(this).attr("target", "_blank");                                	}                                }); $("#editrequest-talk a").each(function {                               	if (!($(this).attr("href") ? $(this).attr("href") : "").includes("#")) {                                		$(this).attr("target", "_blank");                                	}                                }); if (AER.AERSettings.autojump) { location.href = "#" + AER.currentSection; } else { location.href = "#"; }                           } else { AER.pageCounter++; AER.run; }                       });                    });                }            }).fail(function (a) {

});       } else {            throw "The end";        }    } catch (Error) {        $("#editrequest-info").html(' You\'ve reached the end! Reload &bull; <a href="/wiki/WP:RFED">Other edit requests</a> &bull; <a href="/wiki/Wikipedia:Backlog">More backlog</a> ');       $("#editrequest-info-2").html('');        AER.disableButtons;        $("#editrequest-talk").html("");        $("#editrequest-page").html("");    } } function parse(page, where, fn) {    var result = "";    $.get(mw.config.get('wgScriptPath') + '/api.php', { 'action': 'parse', 'page': page, 'prop': 'text', 'format': 'json' }).done(function (r) { if (r.error) { if (r.error.code == "missingtitle") { fn(' This page does not exist. '); } else { throw r.error; }       } else { result = r.parse.text["*"]; //console.log(result); fn(result); }   }).fail(function (r) { $("#editrequest-info").html(' Failed to load: there is no Internet connection.  '); $("#editrequest-talk").html(" Error has occurred. Please reload. "); $("#editrequest-page").html(" Error has occurred. Please reload. "); throw "Connection failed"; }); } AER.disableButtons = function {   $(".advance").prop("disabled", true);    $(".editpage").prop("disabled", true);    $(".history").prop("disabled", true);    $(".edittalk").prop("disabled", true); } AER.enableButtons = function {    $(".advance").prop("disabled", false);    $(".editpage").prop("disabled", false);    $(".history").prop("disabled", false);    $(".edittalk").prop("disabled", false); } AER.preferencesInit = function {    $(".firstHeading").html("AER Preferences");    $("#section_0").html("AER Preferences");    $("title").text("AER Preferences - Wikipedia");    $("#mw-content-text").text("");    $("<form id=\"editprefs-settings\"> ")        .append('<input type="checkbox" id="editprefs-autojump"><label for="editprefs-autojump">Automatically jump to edit request upon loading talk page. ')       .append('<input type="checkbox" id="editprefs-quickeditor"><label for="editprefs-quickeditor">Enable the quick editor to answer edit requests quickly. ')       .append('<input type="checkbox" id="editprefs-sidebarlink"><label for="editprefs-sidebarlink">Show a link to "answer edit requests" on the sidebar. ')       .append('<input disabled type="checkbox" id="editprefs-eph"><label for="editprefs-eph">Enable the <a target="_blank" href="/wiki/WP:EPH">Edit Protected Helper</a> script. This option is currently disabled because the script does not work as intended. ')       .append('<input type="checkbox" id="editprefs-sticky"><label for="editprefs-sticky">Always show the AER buttons at the top of the page. ')       .append('<input type="checkbox" id="editprefs-beta"><label for="editprefs-beta">Enable the beta version of AER. Note: This can get really unstable. ')       .append($('<button type="submit" id="editprefs-submit">Save preferences ').addClass("mw-ui-button").addClass("mw-ui-progressive").css({ "margin-right": "5px", "margin-left": "1px" }))        .append($('<button id="editprefs-reset">Reset preferences ').addClass("mw-ui-button").addClass("mw-ui-destructive").css({ "margin-right": "5px", "margin-left": "1px" }))        .appendTo("#mw-content-text");    for (var setting in AER.AERSettings) {        $("#editprefs-" + setting).attr("checked", AER.AERSettings[setting] ? true : false);   }    $("#editprefs-reset").click(function (e) { e.preventDefault; if (confirm("Are you sure you want to reset all of your preferences? All of your settings will be lost.")) { $("#editprefs-submit").prop("disabled", true); $("#editprefs-reset").prop("disabled", true); $.get(mw.config.get('wgScriptPath') + '/api.php', {               "action": "query",                "format": "json",                "meta": "tokens",                "type": "csrf"            }).done(function (x) {                if (x.error) {                    throw x.error;                } else {                    $.get(mw.config.get('wgScriptPath') + '/api.php', { 'action': 'parse', 'page': "User:Awesome Aasim/editrequest.js/defaultpreferences.json", 'prop': 'wikitext', 'format': 'json' }).done(function (z) { AER.AERSettings = JSON.parse(z.parse.wikitext["*"]); $.post(mw.config.get("wgScriptPath") + '/api.php', {                           "action": "edit",                            "format": "json",                            "title": "User:" + mw.config.get("wgUserName") + "/AERpreferences.json",                            "text": z.parse.wikitext["*"],                            "summary": "Resetting preferences for AER tool",                            "token": x.query.tokens.csrftoken                        }).done(function (y) {                            if (y.error) {                                mw.notify($(" Could not save your preferences because: " + y.error.info + " "));                            } else {								mw.notify($(" Your preferences have been reset. Reload the AER tool to apply them. "));                            }                        }).fail(function (y) { alert("Network settings may have changed. Please reload the page."); });                   }).fail(function (z) {                        alert("Network settings may have changed.  Please reload the page.");                    }) }           }).fail(function (x) { alert("Network settings may have changed. Please reload the page."); })       }    })    $("#editprefs-submit").click(function (e) {        $("#editprefs-submit").prop("disabled", true);        $("#editprefs-reset").prop("disabled", true);        e.preventDefault;        AER.set("autojump");        AER.set("quickeditor");        AER.set("sidebarlink");        AER.set("eph");        AER.set("beta");        AER.set("sticky");        $.get(mw.config.get('wgScriptPath') + '/api.php', { "action": "query", "format": "json", "meta": "tokens", "type": "csrf" }).done(function (x) { if (x.error) { throw x.error; } else { $.post(mw.config.get("wgScriptPath") + '/api.php', {                   "action": "edit",                    "format": "json",                    "title": "User:" + mw.config.get("wgUserName") + "/AERpreferences.json",                    "text": JSON.stringify(AER.AERSettings),                    "summary": "Setting preferences for AER tool",                    "token": x.query.tokens.csrftoken                }).done(function (y) {                    if (y.error) {						mw.notify($(" Could not save your preferences because: " + y.error.info + " "));                    } else {						mw.notify($(" Your preferences have been set. Reload the AER tool to apply them. "));				        $("#editprefs-submit").prop("disabled", false);				        $("#editprefs-reset").prop("disabled", false);                    }                }).fail(function (y) { mw.notify($(" Lost Internet connection. Please try again. ")); $("#editprefs-submit").prop("disabled", false); $("#editprefs-reset").prop("disabled", false); });           }        }).fail(function (x) {			mw.notify($(" Lost Internet connection.  Please try again. "));	        $("#editprefs-submit").prop("disabled", false);	        $("#editprefs-reset").prop("disabled", false);        }) }) } AER.set = function(setting) {   AER.AERSettings[setting] = $("#editprefs-" + setting).is(":checked") ? true : false; } AER.init = function {    $("#mw-content-text").text("");    $(' ').append($(' ')).append($(' ')).appendTo("#mw-content-text")    $(' ').appendTo("#mw-content-text");    $(' ').appendTo("#mw-content-text");    $(' ').append($(' ')).append($(' ')).appendTo("#mw-content-text");    $('<p id="editrequest-preferences"><a href="/wiki/Special:BlankPage/EditProtectedPrefs">Edit settings</a> ').appendTo("#mw-content-text");    if (mw.config.get("wgUserGroups").includes("autoconfirmed") || mw.config.get("wgUserGroups").includes("confirmed") || mw.config.get("wgUserGroups").includes("extendedconfirmed") || mw.config.get("wgUserGroups").includes("templateeditor") || mw.config.get("wgUserGroups").includes("sysop") || mw.config.get("wgUserGroups").includes("interface-admin")) { if (AER.AERSettings.sticky) { $(".editrequest-tools").css({ "position": "sticky", "top": "47.5px", "background-color": "white", "z-index": 99 }); }       //$(".editrequest-tools").css({ "position": "sticky", "top": "47.5px", "background-color": "white", "z-index": 99 });

$("#editrequest-talk").css({ "width": "100%", "height": "500px", "overflow": "auto" }); $("#editrequest-page").css({ "width": "100%", "height": "900px", "overflow": "auto" }); $(" ").append($(' ').text("Edit").click(function (e) { if (AER.AERSettings.quickeditor) { location.href = "#editrequest-page"; AER.editForm("#editrequest-page", AER.currentPage, function {                    $("#editrequest-page").css({ "width": "100%", "height": "500px", "overflow": "auto" });                }); } else { window.open("https://en.wikipedia.org/wiki/" + AER.currentPage + "?action=edit"); }       }).addClass("mw-ui-button").addClass("mw-ui-progressive").css({ "margin-right": "5px", "margin-left": "1px" })).append($(' ').text("Open talk").click(function (e) { window.open("https://en.wikipedia.org/wiki/" + AER.currentTalkPage + "#" + AER.currentSection); }).addClass("mw-ui-button").css({ "margin-right": "5px", "margin-left": "1px" })).append($(' ').text("View history").click(function (e) { e.preventDefault; window.open("https://en.wikipedia.org/wiki/" + AER.currentPage + "?action=history"); }).addClass("mw-ui-button").css({ "margin-right": "5px", "margin-left": "1px" })).append($(' ').text("\u2192").click(function (e) { e.preventDefault; AER.run("f"); }).addClass("mw-ui-button").css({ "margin-right": "5px", "margin-left": "1px" })).appendTo(".editrequest-buttons");

$(' <a href="https://en.wikipedia.org/wiki/User:Awesome_Aasim/editrequest" target="_blank" class="mw-helplink">Help</a> ').appendTo(".mw-indicators"); $("#mw-indicator-mw-helplink a").attr('style', 'background-image: url(/w/resources/src/mediawiki.helplink/images/helpNotice.png?15b31);background-image: linear-gradient(transparent,transparent),url(/w/resources/src/mediawiki.helplink/images/helpNotice.svg?46d34);background-position: left center;background-repeat: no-repeat;display: inline-block;height: 20px;padding-left: 25px;line-height: 20px;'); AER.disableButtons; $("#editrequest-page").before(" Page content "); $(".firstHeading").html("Answer edit requests: <span class=\"editrequest-title\">Loading... "); $("#section_0").html("Answer edit requests: <span class=\"editrequest-title\">Loading... "); $("title").text("Answer edit requests - Wikipedia"); $('#editrequest-info').html(" Loading... "); try { $.get(mw.config.get("wgScriptPath") + "/api.php", {               "action": "query",                "format": "json",                "list": "categorymembers",                "cmtitle": AER.requestPages[0],                "cmprop": "title",                "cmlimit": "max"            }).done(function (r) {                if (r.error) {                    throw r.error;                } else {                    var result = r.query.categorymembers;                    for (var j in result) {                        AER.pages.push([result[j].title, "editsemiprotected"]);                    }                    if (mw.config.get("wgUserGroups").includes("extendedconfirmed") || mw.config.get("wgUserGroups").includes("templateeditor") || mw.config.get("wgUserGroups").includes("sysop") || mw.config.get("wgUserGroups").includes("interface-admin")) {                        $.get(mw.config.get("wgScriptPath") + "/api.php", { "action": "query", "format": "json", "list": "categorymembers", "cmtitle": AER.requestPages[1], "cmprop": "title", "cmlimit": "max" }).done(function (r) { if (r.error) { throw r.error; } else { var result = r.query.categorymembers; for (var j in result) { AER.pages.push([result[j].title, "editextendedprotected"]); }                               if (mw.config.get("wgUserGroups").includes("templateeditor") || mw.config.get("wgUserGroups").includes("sysop") || mw.config.get("wgUserGroups").includes("interface-admin")) { $.get(mw.config.get("wgScriptPath") + "/api.php", {                                       "action": "query",                                        "format": "json",                                        "list": "categorymembers",                                        "cmtitle": AER.requestPages[2],                                        "cmprop": "title",                                        "cmlimit": "max"                                    }).done(function (r) {                                        if (r.error) {                                            throw r.error;                                        } else {                                            var result = r.query.categorymembers;                                            for (var j in result) {                                                AER.pages.push([result[j].title, "edittemplateprotected"]); }                                           if (mw.config.get("wgUserGroups").includes("sysop") || mw.config.get("wgUserGroups").includes("interface-admin")) { $.get(mw.config.get("wgScriptPath") + "/api.php", {                                                   "action": "query",                                                    "format": "json",                                                    "list": "categorymembers",                                                    "cmtitle": AER.requestPages[3],                                                    "cmprop": "title",                                                    "cmlimit": "max"                                                }).done(function (r) {                                                    if (r.error) {                                                        throw r.error;                                                    } else {                                                        var result = r.query.categorymembers;                                                        for (var j in result) { AER.pages.push([result[j].title, "editprotected"]); }                                                       if (mw.config.get("wgUserGroups").includes("interface-admin")) { $.get(mw.config.get("wgScriptPath") + "/api.php", {                                                               "action": "query",                                                                "format": "json",                                                                "list": "categorymembers",                                                                "cmtitle": AER.requestPages[4],                                                                "cmprop": "title",                                                                "cmlimit": "max"                                                            }).done(function (r) {                                                                if (r.error) {                                                                    throw r.error;                                                                } else { var result = r.query.categorymembers; for (var j in result) { AER.pages.push([result[j].title, "editinterfaceprotected"]); }                                                               }                                                                AER.pages = AER.shuffle(AER.pages); AER.run; }).fail(function (r) { throw "Unknown error"; });                                                       } else {                                                            AER.pages = AER.shuffle(AER.pages);                                                            AER.run;                                                        }                                                    }                                                }).fail(function (r) {                                                    throw "Unknown error";                                                }); } else { AER.pages = AER.shuffle(AER.pages); AER.run; }                                       }                                    }).fail(function (r) { throw "Unknown error"; });                               } else {                                    AER.pages = AER.shuffle(AER.pages);                                    AER.run;                                }                            }                        }).fail(function (r) {                            throw "Unknown error";                        }); } else { AER.pages = AER.shuffle(AER.pages); AER.run; }               }            }).fail(function (r) { throw "Unknown error"; });       } catch (Error) {            $("#editrequest-info").html(' Reload. We have hit an error. If you are still having trouble, leave a message <a href="/wiki/User_talk:Awesome_Aasim/editrequest">here</a>. ');           console.error(Error);        }    } else {        $(".firstHeading").text("Permission error");        $("#section_0").text("Permission error");        $("title").text("Permission error - Wikipedia");        $('#mw-content-text').html(' Access to this tool is restricted to <a href="/wiki/Wikipedia:Autoconfirmed">autoconfirmed</a> and <a href="/wiki/Wikipedia:Confirmed">confirmed</a> users. <p id="mw-returnto">Return to <a href="/wiki/Main_Page">Main Page</a>. ');   } } if (mw.config.get("wgPageName").toLowerCase == "Special:BlankPage/EditProtected".toLowerCase) {    $(document).ready(function { AER.init; }); } else if (mw.config.get("wgPageName") == "Category:Wikipedia_edit_requests" || AER.requestPages.includes(mw.config.get("wgPageName"))) {   $(document).ready(function  { $("#siteNotice").append(' Consider using <a href="/wiki/Special:BlankPage/EditProtected">the edit protected tool</a> to review edit requests! '); }) } else if (mw.config.get("wgPageName").toLowerCase == "Special:BlankPage/EditProtectedPrefs".toLowerCase) {   $(document).ready(function { AER.preferencesInit; }); } //