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",  ]);

// Get user permissions so that we can check for deletion restrictions new mw.Api .get({     action: "query",      meta: "userinfo",      format: "json",      uiprop: "rights",    }) .done(function (res) {     // Check loading restrictions      if ( window.dev.ajaxFastDelete === undefined && window.fdButtons && conf.wgNamespaceNumber !== -1 && conf.wgArticleId !== 0 && res.query.userinfo.rights.includes("delete") ) {       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", style: "margin-left: 3px", // 3px margin from https://dev.fandom.com/wiki/MediaWiki:AjaxDelete/code.js, originally in separate CSS text: btn.label, "data-summary": btn.summary, "data-id": "fastdelete", });           });

const buttonsWrapper = $(" ", {             html: deleteBtns,              id: "fast-delete-buttons",              style: "float: right",            });

// 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) { $("#mw-content-text").prepend(             $(" ", { class: "fastDelete-deleting-notice", text: "Deleting, please wait a moment...", })           );

new mw.Api .post({               action: "delete",                title: conf.wgPageName,                reason:                  deleteReason + " (using FastDelete)",                bot: true,                token: mw.user.tokens.get("csrfToken"),              }) .done(function (res) {               $(".fastDelete-deleting-notice").text( "Deletion done, refreshing the page..." );

if (res.error) { return alert(                   "API error occurred! Error code: " +                      res.error.code +                      " - " +                      res.error.info                  ); }               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;        }); }   })    .fail(function (err) { console.error(err); alert(       "FastDelete encountered an error when checking if you have delete permissions. If this repeatedly occurs, please contact Collei on metawiki for help. Make sure to provide any errors from your browser console."      ); return; }); })(jQuery, mediaWiki, window);