User:Rutilant/reportSpam.js

// /* Stable July 12, 2019 1.2 $(function {  var config = {    debug: false,    disable_notification: false,    report_page: 'Wikipedia:Administrator intervention against vandalism',    edit_summary: "Added report for a possible spambot: $username (reportSpam)"  };  if (typeof reportSpam_customconfig == "object") {    Object.keys(reportSpam_customconfig).forEach((e) => { config[e] = reportSpam_customconfig[e]; }); }  let allow_init = mw.config.get("wgCanonicalSpecialPageName") === "Recentchanges" || mw.config.get("wgCanonicalSpecialPageName") === "Contributions";  if (allow_init) {    class AIV_report {      constructor(username, token) {        this.username = username;        this.ajax_data = {          title: config.report_page,          action: "edit",          token: token,          summary: config.edit_summary.replace(/\$username/g, username),          appendtext: '\n* – account is evidently a spambot. ' + '',          format: 'json',        };      }

submit_report(clicked) { var $this = this; $.ajax({         type: 'POST',          url: '/w/api.php',          data: this.ajax_data,          success: function (response) {            $(clicked).text("reported");            if (!config.disable_notification) mw.notify('Added report for ' + $this.username);            if (config.debug) console.log(response);          },          error: function (e) {            if (config.debug) console.log(e);            $(clicked).text("editing failed");            if (!config.disable_notification) mw.notify('reportSpam: unable to report the user; see console log.');            return 'Unable to perform the edit.';          },        }); }   }

let main = { get_token: => { return $.get("/w/api.php?action=query&meta=tokens&format=json").then(response => {         try {            return response.query.tokens.csrftoken;          } catch (e) {            if (!config.disable_notification) mw.notify(s_appname + ': unable to parse the token; check console log for more detail.');            if (config.debug) console.log(e);            return false;          }        }); },

escape_regexp: (str) => { return str.replace(/[-\/\\^$*+?.|[\]{}]/g, '\\$&'); },

check_AIV: (username) => { return $.get("/w/api.php?action=parse&page=" + config.report_page + "&format=json&prop=wikitext") .then(response => {           if (config.debug) console.log(response);            let AIV_text = response.parse.wikitext["*"];            let regexp = RegExp("", "ig");            return regexp.test(AIV_text);          }); },

report_user: (username, clicked) => { $(clicked).text("reading AIV..."); main.check_AIV(username).then(user_exists => {         let confirm_text = "Report '" + username + "' to AIV?";          if (user_exists) {            alert("'" + username + "' seems to already have been reported.");            return;          }          if (confirm(confirm_text)) {            $(clicked).text("getting token");            main.get_token.then(token => { if (token) { let report = new AIV_report(username, token); $(clicked).text("writing report"); report.submit_report(clicked); } else { $(clicked).text("failed to get token"); }           });          } else {            $(clicked).text("report spam");          }

});     }    };

let add_links = function { $(".rp-spam-span").remove; $(".mw-changeslist-line").each(function {        let username = $(this).find(".mw-userlink").text;        $(this).find(".mw-usertoollinks").append(" report spam ");      });

$(".mw-contributions-list li").each(function {        let username = $(this).find(".mw-userlink").text || mw.config.get("wgRelevantUserName") /* for individual contrib page */ ;        $(this).append(" [report spam] ");      });

$(".rp-spam").click(function {        main.report_user($(this).data("user"), $(this));      }); };   mw.hook('wikipage.content').add(function  {      add_links;    }); } }); //