Advertisement
penguinpal

better AjaxSysop parrot

Nov 25th, 2016
349
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.             AjaxSysop.fn.promotionAlert(user);
  37.         }).fail(function() {
  38.             if (n > 0) {
  39.                 return loop(n-1);
  40.             } else {
  41.                 // error in all attempts to save the group
  42.                 AjaxSysop.fn.error();
  43.             }
  44.         });
  45.     }
  46.     loop(5);
  47. };
  48.  
  49. // close interface
  50. AjaxSysop.fn.close = function() {
  51.     $("section#sysop-promote")
  52.         .hide()
  53.         .find('input[type="text"]').val("");
  54. };
  55.  
  56. // error
  57. AjaxSysop.fn.error = function() {
  58.     alert("There was an error promoting the given user. Please try again later or promote manually.");
  59. };
  60.  
  61. // html
  62. $("body").append(
  63.     '<section id="sysop-promote">\n' +
  64.         '\t<div>\n' +
  65.             '\t\t<h2>Promote an admin</h2>\n' +
  66.             '\t\t<p>\n' +
  67.                 '\t\t\tUser to promote: <input type="text" id="sysop-promote-user" /><br />\n' +
  68.                 '\t\t\tPromotion reason: <input type="text" placeholder="Promoting user via [[Special:Chat]]" id="sysop-promote-reason" /><br />\n' +
  69.                 '\t\t\t<input type="button" class="wikia-button" value="Promote" id="sysop-promote-bt-ok" />&nbsp;' +
  70.                 '\t\t\t<input type="button" class="wikia-button" value="Cancel" id="sysop-promote-bt-cancel" />\n' +
  71.             '\t\t</p>\n' +
  72.         '\t</div>\n' +
  73.     '</section>\n'
  74. );
  75.  
  76. // css
  77. mw.util.addCSS(
  78.     'section#sysop-promote {' +
  79.         '\tdisplay: none;\n' +
  80.         '\twidth: 100%;\n' +
  81.         '\theight: 100%;\n' +
  82.         '\tposition: fixed;\n' +
  83.         '\ttop: 0;\n' +
  84.         '\tleft: 0;\n' +
  85.         '\tbackground: rgba(0,0,0,0.35);\n' +
  86.     '}\n' +
  87.     'section#sysop-promote > div {' +
  88.         '\twidth: 300px;\n' +
  89.         '\theight: 100px;\n' +
  90.         '\tposition: fixed;\n' +
  91.         '\ttop: ' + (($(window).height() - 122) / 2) + 'px;\n' +
  92.         '\tleft: ' + (($(window).width() - 322) / 2) + 'px;\n' +
  93.         '\tpadding: 10px;\n' +
  94.         '\tbackground: white;\n' +
  95.         '\tborder: 1px solid black;\n' +
  96.         '\ttext-align: left;\n' +
  97.         '\tcolor: #333333;\n' +
  98.     '}\n' +
  99.     'section#sysop-promote input[type="text"] {' +
  100.         '\twidth: 100px;\n' +
  101.         '\theight: 20px;\n' +
  102.         '\tline-height: 20px;\n' +
  103.         '\tfont-size: 16px;\n' +
  104.     '}' +
  105.     '.pseudo-inline-alert + .continued {\n' +
  106.         '\tmin-height: 32px;\n' +
  107.         '\tmargin-bottom: 0;\n' +
  108.         '\tpadding-top: 18px;\n' +
  109.         '\ttop: 0;\n' +
  110.     '}\n' +
  111.     '.Chat .pseudo-inline-alert + .continued img, .pseudo-inline-alert + .continued .time {\n' +
  112.         '\tdisplay: inline;\n' +
  113.     '}\n' +
  114.     '.pseudo-inline-alert + .continued .username {\n' +
  115.         '\tdisplay: block;\n' +
  116.     '}\n' +
  117.     '.UserStatsMenu .actions ul li.block .icon {\n' +
  118.         '\tbackground-position: -612px 0px;\n' +
  119.     '}'
  120. );
  121.  
  122. // ok function
  123. $("#sysop-promote-bt-ok").click(function() {
  124.     if ($("#sysop-promote-user").val().length > 0) {
  125.         AjaxSysop.fn.getToken($("#sysop-promote-user").val(), 5);
  126.     }
  127. });
  128.  
  129. // cancel function
  130. $("#sysop-promote-bt-cancel").click(function() {
  131.     AjaxSysop.fn.close();
  132. });
  133.  
  134. // trigger when message is "!sysop"
  135. $('textarea[name="message"]').keydown(function(e) {
  136.     if ($(this).val() == "!sysop" && e.keyCode == 13) {
  137.         $(this).val("");
  138.         $("section#sysop-promote").show();
  139.     }
  140. });
  141.  
  142. // add button
  143.  
  144. AjaxSysop.obs = new MutationObserver(function(mt) {
  145.     if ($("#UserStatsMenu .admin-actions .block").length === 0) {
  146.         var li = $(
  147.             '<li class="block">' +
  148.                 '<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>' +
  149.                 '<span class="label">Give Admin Status</span>' +
  150.             '</li>'
  151.         ).appendTo("#UserStatsMenu .admin-actions");
  152.         $(li).click(function() {
  153.             $("section#sysop-promote").show();
  154.         });
  155.     }
  156. });
  157. AjaxSysop.obs.observe(document.querySelector("#UserStatsMenu"), {
  158.     attributes: true
  159. });
  160.  
  161. // wait for meaningful statuses...
  162. mainRoom.socket.on("updateUser", function(msg) {
  163.     var data = JSON.parse(msg.data),
  164.         attrs = data.attrs,
  165.         state = attrs.statusState,
  166.         ul = mainRoom.viewDiscussion.chatUL,
  167.         li,
  168.         by;
  169.     if (attrs.statusMessage == "@AjaxSysop" && attrs.statusState instanceof Object) {
  170.         li = $('<li class="pseudo-inline-alert" />').text(
  171.             state.by + " promoted " + state.target + " to be an admin on this wiki."
  172.         );
  173.         if ($(ul).children(":last").hasClass("inline-alert")) {
  174.             $(li).addClass("continued");
  175.         }
  176.         by = mainRoom.viewUsers.model.users.findByName(state.by);
  177.         try {
  178.             if (by.attributes.canPromoteModerator === true) {
  179.                 $(ul).append(li);
  180.             } else {
  181.                 console.log("lol " + state.by + " ze nub tried to promote a user without them being able to promote admins");
  182.             }
  183.         } catch(err) {}
  184.     }
  185. });
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement