User:Collei/AjaxDelete.js

/** * AjaxDelete * * @author Collei * * @version 1.0 */

// https://sweetalert.js.org/ /* prettier-ignore */ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e:"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.swal=e:t.swal=e}(this,function{return function(t){function e(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:o})},e.n=function(t){var n=t&&t.__esModule?function{return t.default}:function{return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=8)}});

(function ($, mw, window) { /*            Configuration options example:

window.AjaxDeleteReasons = [ 'Housekeeping', 'Unused', 'Vandalism', 'Broken redirect' ];

*/

window.dev = window.dev || {}; window.AjaxDeleteReasons = window.AjaxDeleteReasons || [];

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

// We first need to check if the user is on a SocialProfile page and store that in a variable that we can use, because SocialProfile pages cannot be deleted. // We're going to do this by checking if we're in the user namespace, and then if we are, we're going to check if the user page has the elements indicative of SocialProfile. const isSocialProfilePage = conf.wgNamespaceNumber == 2 && $("#profile-right").length == 1 && $("#profile-image").length == 1;

if (       window.dev.AjaxDelete === undefined && // if it is not undefined, then the script has been run already        window.fdButtons && // if fdButtons is not defined then the script has not been configured        conf.wgNamespaceNumber !== -1 && // namespace number -1 indicates a special page        conf.wgArticleId !== 0 && // article ID 0 indicates a non-existent page        res.query.userinfo.rights.includes("delete") && // user needs to have delete permissions for this script to run        !isSocialProfilePage      ) { const self = { init: function { console.log("a"); mw.util.addCSS(`                                   /* Based on the #filetoc Vector CSS */                        #AjaxDelete-deleting-notice {                            text-align: center;                            border: 1px solid #a2a9b1;                            background-color: #f8f9fa;                            padding: 5px;                            font-size: 95%;                            margin-bottom: 0.5em;                            margin-left: 0;                            margin-right: 0;                        }                        /*                        https://github.com/Wikia/app/blob/dev/skins/oasis/css/core/page.scss                        License: https://github.com/Wikia/app/blob/dev/COPYING                        */                        .wds-button {                            -webkit-box-align: center;                            -webkit-box-pack: center; align-items: center; -webkit-appearance: none; background: #ffc200; border: 1px solid #8e7650; border-radius: 3px; color: white; cursor: default; display: -webkit-inline-box; display: inline-flex; font-family: inherit; font-size: 12px; font-weight: 600; justify-content: center; letter-spacing: 0.15px; line-height: 16px; margin: 0; min-height: 18px; outline: none; padding: 7px 18px; text-decoration: none; text-transform: uppercase; vertical-align: top; font-family: sans-serif; }                   `);

$("#mm-page-delete-page > a").attr("href", "");

$("#mm-page-delete-page > a").on("click", function (e) {             console.log("uwu");

e.preventDefault;

var dropDown = new OO.ui.DropdownWidget({               label: "Select deletion reason",                id: "AjaxDelete-deletion-dropdown",                menu: {                  items: [                    new OO.ui.MenuOptionWidget({ data: "choose-other-reason-selected", label: "Input another reason", }),                 ],                },              });

window.AjaxDeleteReasons.forEach((element) => {               dropDown.menu.items.push( new OO.ui.MenuOptionWidget({                   data: element,                    label: element,                  }) );             });

itemSelected = function (selected) { swal.close;

if (selected.data == "choose-other-reason-selected") { self.chooseOtherDeleteReason; } else { self.deletePage(selected.label); }             };

console.log(dropDown); console.log(dropDown.$element);

swal({               content: dropDown.$element[0],              });

dropDown.getMenu.on("select", itemSelected);

return; });         },          chooseOtherDeleteReason: function  {            //$("#AjaxDelete-deletion-dropdown").remove;

OO.ui             .prompt("Enter delete reason", {                textInput: { placeholder: "Delete reason" },              }) .done(function (result) {               // null result would indicate the user canceled                if (result !== null) {                  self.deletePage(result);                }              }); },         deletePage: function (deleteReason) { $("#mw-content-text").prepend(             $(" ", { id: "AjaxDelete-deleting-notice", text: "Deleting, please wait a moment...", })           );

new mw.Api .post({               action: "delete",                title: conf.wgPageName,                reason:                  deleteReason + " (using AjaxDelete)",                bot: true,                token: mw.user.tokens.get("csrfToken"),              }) .done(function (res) {               if (res.error) {                  $("#AjaxDelete-deleting-notice").remove;

OO.ui.alert(                   "API error occurred! Error code: " +                      res.error.code +                      " - " +                      res.error.info                  );

return; }

$("#AjaxDelete-deleting-notice").text(                 "Deletion done, refreshing the page..."                );

location.reload; })             .fail(function  { $("#AjaxDelete-deleting-notice").remove;

swal(                 "AJAX error occurred. This could be a temporary issue. Try deleting the page again. If this still persists, contact Collei on metawiki, and make sure to provide your browser logs.",                  "error"                );

return; });         },        };

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

// Run init function with dependencies mw.loader .using(["mediawiki.util", "mediawiki.api", "oojs-ui-core"]) .then(function {            self.init;          }); }   })    .fail(function (err) { console.error(err);

swal(       "AjaxDelete 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.",        "error"      );

return; }); })(jQuery, mediaWiki, window);