Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // All credit goes to Penguin-Pal //
- //Promote Admins in chat//
- // type "!sysop" or use the button to trigger
- // define object
- if ( mw.config.get( 'wgUserGroups' ).indexOf( 'bureaucrat' ) > -1 ) {
- AjaxSysop = {};
- // functions
- AjaxSysop.fn = {};
- AjaxSysop.fn.getToken = function(user, n) {
- if (n > 0) {
- $.getJSON("/api.php?action=query&format=json&list=users&ustoken=userrights&ususers=" + encodeURIComponent(user), function(data) {
- var token = data.query.users[0].userrightstoken;
- console.log("Token: " + token);
- AjaxSysop.fn.makeAdmin(user, token);
- }).fail(function() {
- return AjaxSysop.fn.getToken(user, n-1);
- });
- } else {
- // errors in all attempts to get the token
- AjaxSysop.fn.error();
- }
- };
- AjaxSysop.fn.makeAdmin = function(user, token) {
- var reason = $("#sysop-promote-reason").val().length > 0 ? $("#sysop-promote-reason").val() : "Promoting user via [[Special:Chat]]";
- function loop(n) {
- $.ajax({
- type: "POST",
- url: "/api.php?action=userrights&user=" + encodeURIComponent(user) + "&token=" + encodeURIComponent(token) + "&add=sysop&reason=" + encodeURIComponent(reason)
- }).done(function() {
- // success! close interface
- AjaxSysop.fn.close();
- mainRoom.socket.send(new models.ChatEntry({
- roomId: mainRoom.roomId,
- name: mw.config.get("wgUserName"),
- text: "[[:::sysoppromote:::" + user + "|" + user + " was given Administrator rights!]]"
- }).xport());
- }).fail(function() {
- if (n > 0) {
- return loop(n-1);
- } else {
- // error in all attempts to save the group
- AjaxSysop.fn.error();
- }
- });
- }
- loop(5);
- };
- // close interface
- AjaxSysop.fn.close = function() {
- $("section#sysop-promote")
- .hide()
- .find('input[type="text"]').val("");
- };
- // error
- AjaxSysop.fn.error = function() {
- alert("There was an error promoting the given user. Please try again later or promote manually.");
- };
- // html
- $("body").append(
- '<section id="sysop-promote">\n' +
- '\t<div>\n' +
- '\t\t<h2>Promote an admin</h2>\n' +
- '\t\t<p>\n' +
- '\t\t\tUser to promote: <input type="text" id="sysop-promote-user" /><br />\n' +
- '\t\t\tPromotion reason: <input type="text" placeholder="Promoting user via [[Special:Chat]]" id="sysop-promote-reason" /><br />\n' +
- '\t\t\t<input type="button" class="wikia-button" value="Promote" id="sysop-promote-bt-ok" /> ' +
- '\t\t\t<input type="button" class="wikia-button" value="Cancel" id="sysop-promote-bt-cancel" />\n' +
- '\t\t</p>\n' +
- '\t</div>\n' +
- '</section>\n'
- );
- // css
- mw.util.addCSS(
- 'section#sysop-promote {' +
- '\tdisplay: none;\n' +
- '\twidth: 100%;\n' +
- '\theight: 100%;\n' +
- '\tposition: fixed;\n' +
- '\ttop: 0;\n' +
- '\tleft: 0;\n' +
- '\tbackground: rgba(0,0,0,0.35);\n' +
- '}\n' +
- 'section#sysop-promote > div {' +
- '\twidth: 300px;\n' +
- '\theight: 100px;\n' +
- '\tposition: fixed;\n' +
- '\ttop: ' + (($(window).height() - 122) / 2) + 'px;\n' +
- '\tleft: ' + (($(window).width() - 322) / 2) + 'px;\n' +
- '\tpadding: 10px;\n' +
- '\tbackground: white;\n' +
- '\tborder: 1px solid black;\n' +
- '\ttext-align: left;\n' +
- '\tcolor: #333333;\n' +
- '}\n' +
- 'section#sysop-promote input[type="text"] {' +
- '\twidth: 100px;\n' +
- '\theight: 20px;\n' +
- '\tline-height: 20px;\n' +
- '\tfont-size: 16px;\n' +
- '}' +
- '.pseudo-inline-alert + .continued {\n' +
- '\tmin-height: 32px;\n' +
- '\tmargin-bottom: 0;\n' +
- '\tpadding-top: 18px;\n' +
- '\ttop: 0;\n' +
- '}\n' +
- '.Chat .pseudo-inline-alert + .continued img, .pseudo-inline-alert + .continued .time {\n' +
- '\tdisplay: inline;\n' +
- '}\n' +
- '.pseudo-inline-alert + .continued .username {\n' +
- '\tdisplay: block;\n' +
- '}\n' +
- '.UserStatsMenu .actions ul li.block .icon {\n' +
- '\tbackground-position: -612px 0px;\n' +
- '}'
- );
- // ok function
- $("#sysop-promote-bt-ok").click(function() {
- if ($("#sysop-promote-user").val().length > 0) {
- AjaxSysop.fn.getToken($("#sysop-promote-user").val(), 5);
- }
- });
- // cancel function
- $("#sysop-promote-bt-cancel").click(function() {
- AjaxSysop.fn.close();
- });
- // trigger when message is "!sysop"
- $('textarea[name="message"]').keydown(function(e) {
- if ($(this).val() == "!sysop" && e.keyCode == 13) {
- $(this).val("");
- $("section#sysop-promote").show();
- }
- });
- // add button
- AjaxSysop.obs = new MutationObserver(function(mt) {
- if ($("#UserStatsMenu .admin-actions .block").length === 0) {
- var li = $(
- '<li class="block">' +
- '<img src="http://vignette3.wikia.nocookie.net/hypothetical-stars/images/b/b7/Admin_Logo.png/revision/latest/scale-to-width/24?cb=20150424191053"> </span>' +
- '<span class="label">Give Admin Status</span>' +
- '</li>'
- ).appendTo("#UserStatsMenu .admin-actions");
- $(li).click(function() {
- $("section#sysop-promote").show();
- });
- }
- });
- AjaxSysop.obs.observe(document.querySelector("#UserStatsMenu"), {
- attributes: true
- });
- }
- /* observe messages from AjaxSysop by everyone in chat */
- // observe new messages
- var AjaxSysopObs = new MutationObserver(function(mutations) {
- for (var i in mutations) {
- for (var j in mutations[i].addedNodes) {
- var node = mutations[i].addedNodes[j];
- if (node.nodeType == 1) {
- if (
- node.nodeName.toLowerCase() == "li" &&
- !$(node).hasClass("inline-alert")
- ) {
- var sender = mainRoom.model.users.findByName($(node).attr("data-user"));
- $(node).find('a[href^="' + location.origin + '/wiki/:::sysoppromote:::"]').each(function() {
- if (sender) {
- var promotion = $(this).attr("href").split(":::")[2];
- if (sender.attributes.isCanGiveChatMod) {
- $(node).replaceWith('<li class="inline-alert pseudo-inline-alert">' + promotion + ' has been given admin rights by ' + sender.attributes.name + '</li>');
- }
- }
- });
- }
- }
- }
- }
- });
- // start observing
- AjaxSysopObs.observe(document.querySelector("#Chat_" + mainRoom.roomId + " ul"), {
- childList: true
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement