Advertisement
Reno_Rex

Promote Admins In Chat

Nov 16th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // All credit goes to Penguin-Pal //
  2.  
  3. //Promote Admins in chat//
  4. // type "!sysop" or use the button to trigger
  5.  
  6. // define object
  7.  
  8. if ( mw.config.get( 'wgUserGroups' ).indexOf( 'bureaucrat' ) > -1 ) {
  9. AjaxSysop = {};
  10.  
  11. // functions
  12. AjaxSysop.fn = {};
  13. AjaxSysop.fn.getToken = function(user, n) {
  14.     if (n > 0) {
  15.         $.getJSON("/api.php?action=query&format=json&list=users&ustoken=userrights&ususers=" + encodeURIComponent(user), function(data) {
  16.             var token = data.query.users[0].userrightstoken;
  17.             console.log("Token: " + token);
  18.             AjaxSysop.fn.makeAdmin(user, token);
  19.         }).fail(function() {
  20.             return AjaxSysop.fn.getToken(user, n-1);
  21.         });
  22.     } else {
  23.         // errors in all attempts to get the token
  24.         AjaxSysop.fn.error();
  25.     }
  26. };
  27. AjaxSysop.fn.makeAdmin = function(user, token) {
  28.     var reason = $("#sysop-promote-reason").val().length > 0 ? $("#sysop-promote-reason").val() : "Promoting user via [[Special:Chat]]";
  29.     function loop(n) {
  30.         $.ajax({
  31.             type: "POST",
  32.             url: "/api.php?action=userrights&user=" + encodeURIComponent(user) + "&token=" + encodeURIComponent(token) + "&add=sysop&reason=" + encodeURIComponent(reason)
  33.         }).done(function() {
  34.             // success! close interface
  35.             AjaxSysop.fn.close();
  36.             mainRoom.socket.send(new models.ChatEntry({
  37.                 roomId: mainRoom.roomId,
  38.                 name: mw.config.get("wgUserName"),
  39.                 text: "[[:::sysoppromote:::" + user + "|" + user + " was given Administrator rights!]]"
  40.             }).xport());
  41.         }).fail(function() {
  42.             if (n > 0) {
  43.                 return loop(n-1);
  44.             } else {
  45.                 // error in all attempts to save the group
  46.                 AjaxSysop.fn.error();
  47.             }
  48.         });
  49.     }
  50.     loop(5);
  51. };
  52.  
  53. // close interface
  54. AjaxSysop.fn.close = function() {
  55.     $("section#sysop-promote")
  56.         .hide()
  57.         .find('input[type="text"]').val("");
  58. };
  59.  
  60. // error
  61. AjaxSysop.fn.error = function() {
  62.     alert("There was an error promoting the given user. Please try again later or promote manually.");
  63. };
  64.  
  65. // html
  66. $("body").append(
  67.     '<section id="sysop-promote">\n' +
  68.         '\t<div>\n' +
  69.             '\t\t<h2>Promote an admin</h2>\n' +
  70.             '\t\t<p>\n' +
  71.                 '\t\t\tUser to promote: <input type="text" id="sysop-promote-user" /><br />\n' +
  72.                 '\t\t\tPromotion reason: <input type="text" placeholder="Promoting user via [[Special:Chat]]" id="sysop-promote-reason" /><br />\n' +
  73.                 '\t\t\t<input type="button" class="wikia-button" value="Promote" id="sysop-promote-bt-ok" />&nbsp;' +
  74.                 '\t\t\t<input type="button" class="wikia-button" value="Cancel" id="sysop-promote-bt-cancel" />\n' +
  75.             '\t\t</p>\n' +
  76.         '\t</div>\n' +
  77.     '</section>\n'
  78. );
  79.  
  80. // css
  81. mw.util.addCSS(
  82.     'section#sysop-promote {' +
  83.         '\tdisplay: none;\n' +
  84.         '\twidth: 100%;\n' +
  85.         '\theight: 100%;\n' +
  86.         '\tposition: fixed;\n' +
  87.         '\ttop: 0;\n' +
  88.         '\tleft: 0;\n' +
  89.         '\tbackground: rgba(0,0,0,0.35);\n' +
  90.     '}\n' +
  91.     'section#sysop-promote > div {' +
  92.         '\twidth: 300px;\n' +
  93.         '\theight: 100px;\n' +
  94.         '\tposition: fixed;\n' +
  95.         '\ttop: ' + (($(window).height() - 122) / 2) + 'px;\n' +
  96.         '\tleft: ' + (($(window).width() - 322) / 2) + 'px;\n' +
  97.         '\tpadding: 10px;\n' +
  98.         '\tbackground: white;\n' +
  99.         '\tborder: 1px solid black;\n' +
  100.         '\ttext-align: left;\n' +
  101.         '\tcolor: #333333;\n' +
  102.     '}\n' +
  103.     'section#sysop-promote input[type="text"] {' +
  104.         '\twidth: 100px;\n' +
  105.         '\theight: 20px;\n' +
  106.         '\tline-height: 20px;\n' +
  107.         '\tfont-size: 16px;\n' +
  108.     '}' +
  109.     '.pseudo-inline-alert + .continued {\n' +
  110.         '\tmin-height: 32px;\n' +
  111.         '\tmargin-bottom: 0;\n' +
  112.         '\tpadding-top: 18px;\n' +
  113.         '\ttop: 0;\n' +
  114.     '}\n' +
  115.     '.Chat .pseudo-inline-alert + .continued img, .pseudo-inline-alert + .continued .time {\n' +
  116.         '\tdisplay: inline;\n' +
  117.     '}\n' +
  118.     '.pseudo-inline-alert + .continued .username {\n' +
  119.         '\tdisplay: block;\n' +
  120.     '}\n' +
  121.     '.UserStatsMenu .actions ul li.block .icon {\n' +
  122.         '\tbackground-position: -612px 0px;\n' +
  123.     '}'
  124. );
  125.  
  126. // ok function
  127. $("#sysop-promote-bt-ok").click(function() {
  128.     if ($("#sysop-promote-user").val().length > 0) {
  129.         AjaxSysop.fn.getToken($("#sysop-promote-user").val(), 5);
  130.     }
  131. });
  132.  
  133. // cancel function
  134. $("#sysop-promote-bt-cancel").click(function() {
  135.     AjaxSysop.fn.close();
  136. });
  137.  
  138. // trigger when message is "!sysop"
  139. $('textarea[name="message"]').keydown(function(e) {
  140.     if ($(this).val() == "!sysop" && e.keyCode == 13) {
  141.         $(this).val("");
  142.         $("section#sysop-promote").show();
  143.     }
  144. });
  145.  
  146. // add button
  147.  
  148. AjaxSysop.obs = new MutationObserver(function(mt) {
  149.     if ($("#UserStatsMenu .admin-actions .block").length === 0) {
  150.         var li = $(
  151.             '<li class="block">' +
  152.                 '<img src="http://vignette3.wikia.nocookie.net/hypothetical-stars/images/b/b7/Admin_Logo.png/revision/latest/scale-to-width/24?cb=20150424191053">&nbsp;</span>' +
  153.                 '<span class="label">Give Admin Status</span>' +
  154.             '</li>'
  155.         ).appendTo("#UserStatsMenu .admin-actions");
  156.         $(li).click(function() {
  157.             $("section#sysop-promote").show();
  158.         });
  159.     }
  160. });
  161. AjaxSysop.obs.observe(document.querySelector("#UserStatsMenu"), {
  162.     attributes: true
  163. });
  164.  
  165. }
  166.  
  167. /* observe messages from AjaxSysop by everyone in chat */
  168. // observe new messages
  169. var AjaxSysopObs = new MutationObserver(function(mutations) {
  170.     for (var i in mutations) {
  171.         for (var j in mutations[i].addedNodes) {
  172.             var node = mutations[i].addedNodes[j];
  173.             if (node.nodeType == 1) {
  174.                 if (
  175.                     node.nodeName.toLowerCase() == "li" &&
  176.                     !$(node).hasClass("inline-alert")
  177.                 ) {
  178.                     var sender = mainRoom.model.users.findByName($(node).attr("data-user"));
  179.                     $(node).find('a[href^="' + location.origin + '/wiki/:::sysoppromote:::"]').each(function() {
  180.                         if (sender) {
  181.                             var promotion = $(this).attr("href").split(":::")[2];
  182.                             if (sender.attributes.isCanGiveChatMod) {
  183.                                 $(node).replaceWith('<li class="inline-alert pseudo-inline-alert">' + promotion + ' has been given admin rights by ' + sender.attributes.name + '</li>');
  184.                             }
  185.                         }
  186.                     });
  187.                 }
  188.             }
  189.         }
  190.     }
  191. });
  192.  
  193. // start observing
  194. AjaxSysopObs.observe(document.querySelector("#Chat_" + mainRoom.roomId + " ul"), {
  195.     childList: true
  196. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement