MediaWiki:Gadget-LazyAdminTools-core.js

From Old School Near-Reality Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
  1 // LazyAdminTools: easy delete, rollback, and block buttons for admins
  2 // @author Jr Mime
  3 // @author Iiii_I_I_I
  4 
  5 $(function () {
  6     if (mw.config.get("wgCanonicalSpecialPageName") != "Contributions") return;
  7 
  8     var username = mw.config.get("wgPageName").split("/")[1],
  9         lazyAdminTools = {};
 10 
 11     // Fieldset maker
 12     lazyAdminTools.init = function () {
 13         var blockSection = new OO.ui.ActionFieldLayout(
 14             new OO.ui.ComboBoxInputWidget({
 15                 options: [
 16                     { data: '1 day' },
 17                     { data: '3 days' },
 18                     { data: '1 week' },
 19                     { data: '2 weeks' },
 20                     { data: '1 month' },
 21                     { data: '3 months' },
 22                     { data: 'infinite' }
 23                 ],
 24                 value: '3 days',
 25                 placeholder: 'Block duration'
 26             }),
 27             new OO.ui.ButtonWidget({
 28                 classes: ['LAT-button-block'],
 29                 label: 'Block'
 30             }),
 31             {
 32                 align: 'top',
 33                 classes: ['LAT-section-block'],
 34                 label: 'Block with reason "Spam/vandalism" for:',
 35             }
 36         );
 37         var deleteSection = new OO.ui.ActionFieldLayout(
 38             new OO.ui.TextInputWidget({
 39                 value: 'Removing spam/vandalism',
 40                 placeholder: 'Reason for deletion'
 41             }),
 42             new OO.ui.ButtonWidget({
 43                 classes: ['LAT-button-delete'],
 44                 label: 'Delete'
 45             }),
 46             {
 47                 align: 'top',
 48                 classes: ['LAT-section-delete'],
 49                 label: 'Delete page creations with reason:'
 50             }
 51         );
 52         var rollbackSection = new OO.ui.FieldLayout(
 53             new OO.ui.ButtonWidget({
 54                 classes: ['LAT-button-rollback'],
 55                 label: 'Rollback'
 56             }),
 57             {
 58                 align: 'top',
 59                 classes: ['LAT-section-rollback'],
 60                 label: 'Revert current revisions:'
 61             }
 62         );
 63         var everythingSection = new OO.ui.FieldLayout(
 64             new OO.ui.ButtonWidget({
 65                 classes: ['LAT-button-everything'],
 66                 label: 'You\'re a big guy',
 67                 title: 'For you',
 68                 flags: [ 'primary', 'destructive' ]
 69             }),
 70             {
 71                 align: 'top',
 72                 classes: ['LAT-section-everything'],
 73                 label: 'Perform all three actions:'
 74             }
 75         );
 76 
 77         $('<fieldset/>', { class: 'gadget-LAT' }).append(
 78             $('<legend/>', { text: 'LazyAdminTools' }),
 79             $('<div/>', { class: 'LAT-container' }).append(
 80                 blockSection.$element,
 81                 deleteSection.$element,
 82                 rollbackSection.$element,
 83                 everythingSection.$element
 84             )
 85         ).prependTo('#mw-content-text');
 86 
 87         // On click of any of the buttons, call the functions
 88         $(".LAT-button-rollback").on("click", function () {
 89             lazyAdminTools.rollback();
 90         });
 91 
 92         $(".LAT-button-block").on("click", function () {
 93             lazyAdminTools.block();
 94         });
 95 
 96         $(".LAT-button-delete").on("click", function () {
 97             lazyAdminTools.del();
 98         });
 99 
100         $(".LAT-button-everything").on("click", function () {
101             // Poor way of doing all
102             lazyAdminTools.rollback();
103             lazyAdminTools.block();
104             lazyAdminTools.del();
105         });
106     };
107 
108     // Rollback function: get all rollback links in browser, then strike the edit
109     lazyAdminTools.rollback = function () {
110         $(".mw-rollback-link a").each(function (i) {
111             var obj = $(this),
112                 href = obj.attr("href");
113 
114             setTimeout(function () {
115                 $.get(href);
116                 obj.text("gone!")
117                     .css({
118                         color: "grey",
119                         "text-decoration": "line-through",
120                     })
121                     .removeAttr("href")
122                     .parents()
123                     .eq(1)
124                     .css({
125                         color: "grey",
126                         "text-decoration": "line-through",
127                     })
128                     .children()
129                     .removeAttr("href")
130                     .css({
131                         color: "grey",
132                         "text-decoration": "line-through",
133                     });
134             }, i * 500);
135         });
136     };
137 
138     // Block function, simple API call
139     lazyAdminTools.block = function () {
140         new mw.Api()
141             .postWithEditToken({
142                 format: "json",
143                 action: "block",
144                 user: username,
145                 expiry: $('.LAT-section-block .oo-ui-inputWidget-input').val(),
146                 nocreate: 0,
147                 autoblock: 0,
148                 reason: "Spam/vandalism",
149                 bot: 1
150             })
151             .done(function (d) {
152                 if (!d.error) {
153                     alert("User has been blocked!");
154                 } else {
155                     alert("Failed to block " + username + ": " + d.error.code);
156                 }
157             })
158             .fail(function () {
159                 alert("Failed to block " + username + "!");
160             });
161     };
162 
163     // Delete function, get all titles if the line has a "newpage" attribute, delete
164     lazyAdminTools.del = function () {
165         function apiDelete(page, reason) {
166             new mw.Api()
167                 .postWithEditToken({
168                     format: "json",
169                     action: "delete",
170                     title: page,
171                     reason: reason,
172                     bot: 1
173                 })
174                 .done(function (d) {
175                     if (!d.error) {
176                         // console.log('Deletion of ' + page + ' successful!');
177                     } else {
178                         console.log("Failed to delete " + page + ": " + d.error.code);
179                     }
180                 })
181                 .fail(function () {
182                     console.log("Failed to delete " + page + "!");
183                 });
184         }
185 
186         $("li .newpage ~ a").each(function () {
187             var title = $(this).attr("title");
188 
189             apiDelete(title, $('.LAT-section-delete .oo-ui-inputWidget-input').val());
190             $(this)
191                 .parent()
192                 .css({
193                     color: "grey",
194                     "text-decoration": "line-through",
195                 })
196                 .children()
197                 .removeAttr("href")
198                 .css({
199                     color: "grey",
200                     "text-decoration": "line-through",
201                 });
202         });
203     };
204 
205     // Launch the damn thing
206     lazyAdminTools.init();
207 })