MediaWiki:Common.js/User:Fumple/PatrolTool.js

function error(data){ $("#funpl-patroltool").css("display", "none"); $("#funpl-patroltool-error").css("display", ""); $("#funpl-patroltool-error-text").text(data); console.error(data); throw new Error(data); }

try{ //try catch all the lines!

var namespaceselect = null; var continuebutton = null; var cblayout = null; var finalchanges = []; var currentchange = -1;

new mw.Api.get({   "action": "query",    "format": "json",    "meta": "userinfo",    "uiprop": "rights" }).done((data) => {    console.log(data);    if (data.query.userinfo.rights.includes("patrol")) {        var url = new URL(window.location);        if(!url.searchParams.has("funpl-patroltool-debug"))            loadFilters;    }    else if (data.query.userinfo.id != 0) {        $("#funpl-patroltool").css("display", "none");        $("#funpl-patroltool-error").css("display", "");        $("#funpl-patroltool-error-text").html("You can't use this tool as you don't have the correct rights");    }    else {        $("#funpl-patroltool").css("display", "none");        $("#funpl-patroltool-error").css("display", "");        $("#funpl-patroltool-error-text").html("You can't use this tool as you don't have the correct rights You're also logged out, maybe you want to log in"); } }).catch(error);

async function loadFilters{ await mw.loader.load("oojs-ui-widgets"); $("#funpl-patroltool-header").text("Loading namespaces..."); new mw.Api.get({       "action": "query",        "format": "json",        "meta": "siteinfo",        "siprop": "namespaces"    }).done((data) => {        console.log("namespaces", data);        namespaceselect = new OO.ui.MenuTagMultiselectWidget({ "options":[{ "data":"*", "label":"All namespaces" }]       });        var layout = new OO.ui.FieldLayout(namespaceselect, { "align": 'top', "label": 'Namespaces', "id":"funpl-patroltool-nss" });       continuebutton = new OO.ui.ButtonWidget({ label: 'Continue', icon: 'arrowNext' });       cblayout = new OO.ui.FieldLayout(continuebutton, { "align": 'top', "label": '', "id":"funpl-patroltool-cb", "notices":["Pro tip: Want to save your filters? Bookmark this page after you click continue"] });       var url = new URL(window.location);        if(url.searchParams.has("namespaces"))            var urlns = url.searchParams.get("namespaces").split('|');        else            var urlns = [];        var selected = [];        if(urlns.includes("*"))            selected = selected.concat({ "data":"*", "label":"All namespaces" });       for(var nsid in data.query.namespaces){            namespaceselect.addOptions([{ "data":nsid, "label":data.query.namespaces[nsid]["*"] != "" ? data.query.namespaces[nsid]["*"] : "(Main)" }])           if(urlns.includes(nsid))                selected = selected.concat({ "data":nsid, "label":data.query.namespaces[nsid]["*"] != "" ? data.query.namespaces[nsid]["*"] : "(Main)" });       }        namespaceselect.setValue(selected);        $("#funpl-patroltool-header").text("Select filters");        $("#funpl-patroltool-loading").css("display", "none");        $("#funpl-patroltool").append(layout.$element);        $("#funpl-patroltool").append(cblayout.$element);        continuebutton.on("click", function{ continuebutton.disabled = true; continuebutton.updateDisabled; namespaceselect.disabled = true; namespaceselect.updateDisabled; $("#funpl-patroltool-header").text("Loading changes..."); cblayout.setNotices(["Loading... This might take a second"]); $("#funpl-patroltool-loading").css("display", ""); loadChanges; })   }).catch(error); }

async function loadChanges(rccontinue){ return new Promise(function(k,no){       var namespace = namespaceselect.getValue.includes("*") ? "*" : namespaceselect.getValue.join('|');        if(namespace == "") namespace = "*"        var url = new URL(window.location);        url.searchParams.set("namespaces", namespace);        window.history.pushState("", "", url.toString);

var options = { "action": "query", "format": "json", "list": "recentchanges", "rcnamespace": namespace, "rcprop": "title|timestamp|ids", "rcshow": "unpatrolled", "rcdir": "newer", "rclimit": "max" };       if(rccontinue != null) options.rccontinue = rccontinue; new mw.Api.get(options).done(async function (data) {           console.log("rcdata", data);            var changes = data.query.recentchanges;            if(data.continue != null){                var cchanges = await loadChanges(data.continue.rccontinue);                changes = changes.concat(cchanges);            }            if(rccontinue != null){                k(changes);                return;            }            console.log(changes.length, changes);            finalchanges = changes;            $("#funpl-patroltool-header").text("Ready!");            cblayout.setNotices(["Found "+changes.length+" changes", "Press continue to load the first edit"]);            $("#funpl-patroltool-loading").css("display", "none");            //continuebutton.disabled = false;            //continuebutton.updateDisabled;            continuebutton.off("click");            continuebutton.on("click", function{ continuebutton.disabled = true; continuebutton.updateDisabled; cblayout.setNotices(["Loading..."]); $("#funpl-patroltool-loading").css("display", ""); loadNextChange; })       }).catch(error).catch(no); }); } function loadNextChange{   loadChange(currentchange+1); } function loadChange(i){    currentchange = i; //and this is the place where we load the changes. how exciting }

} catch(err){ error(err); }