User:Collei/FastDelete.js

/** * Ajax Fast Delete * * @see https://dev.fandom.com/wiki/MediaWiki:FastDelete/code.js * * @author Collei * @author Splarka * @author Uberfuzzy * @author Grunny * * @version 2.7 */ (function ($, mw, window) { //"use strict";

window.dev = window.dev || {};

const conf = mw.config.get([   "wgNamespaceNumber",    "wgPageName",    "wgArticleId",  ]);

// Loading restrictions if (   window.dev.ajaxFastDelete !== undefined ||    !window.fdButtons ||    conf.wgNamespaceNumber === -1 ||    conf.wgArticleId === 0 ||    (conf.wgNamespaceNumber === 2 && $(".noarticletext").length) ||    (conf.wgNamespaceNumber != 2 && !$("#mm-page-delete-page").length) /* ||    !/sysop|staff|helper|wiki-representative|wiki-specialist|moderator|soap/.test( mw.config.get("wgUserGroups").join )*/ ) {    return; }

const self = { version: "2.7", init: function { mw.loader.load(       "https://meta.miraheze.org/wiki/User:Collei/fastdelete.css?action=raw&ctype=text/css",        "text/css"      );

const $title = $("#firstHeading");

// Map config object to get buttons' HTML const deleteBtns = window.fdButtons.map(function (btn) {       return $("", { class: "wds-button", title: "Delete", text: btn.label, "data-summary": btn.summary, "data-id": "fastdelete", });     });

const buttonsWrapper = $(" ", {       id: "FastDeleteBtns",        html: deleteBtns,      });

// Place buttons $title.append(buttonsWrapper);

// Handle click events const btnElements = $('a[data-id="fastdelete"]'); if (btnElements.length) { btnElements.click(function {          const deleteReason = $(this).attr("data-summary");

// Delete immediately if NoConfirm option is set if (!!window.FastDeleteNoConfirm) { self.deletePage(deleteReason); } else { // Otherwise ask for confirmation first const isConfirmed = confirm(             "Are you sure you want to delete this page? Delete reason will be " +                deleteReason +                "."            ); if (isConfirmed) { self.deletePage(deleteReason); }         }        });      }

// Fire the hook mw.hook("fastdelete.init").fire(buttonsWrapper); },   deletePage: function (deleteReason) { new mw.Api .post({         action: "delete",          title: conf.wgPageName,          reason: deleteReason,          bot: true,          token: mw.user.tokens.get("csrfToken"),        }) .done(function (res) {         if (res.error) {            return alert( "API error occurred! Error code: " + res.error.code + " - " +               res.error.info );         }          console.log(conf.wgPageName + "deleted successfully");          location.reload;        }) .fail(function {          return alert("AJAX error occurred!");        }); }, };

// Export the script window.dev.ajaxFastDelete = self;

// Run init function with dependencies mw.loader.using(["mediawiki.util", "mediawiki.api"]).then(function {    self.init;  }); })(jQuery, mediaWiki, window);