Advertisement
penguinpal

paste for hypercane's hypoverse by the parrot potato 2

Jan 17th, 2017
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var badges = (function() {
  2.     var groups = ["bureaucrat", "sysop", "junioradmin", "rollback", "chatmoderator", "autopatrolled"], // order determines which group is dominant
  3.         badges = {
  4.             "bureaucrat": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
  5.             "sysop": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
  6.             "junioradmin": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
  7.             "rollback": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
  8.             "chatmoderator": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
  9.             "autopatrolled": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png"
  10.         },
  11.         fn = {},
  12.         css = document.createElement("style");
  13.     css.type = "text/css";
  14.     document.head.appendChild(css);
  15.  
  16.     /* functions */
  17.     // get list of users in a given group
  18.     fn.getUsersInGroup = function(group, offset, collection, cb) {
  19.         offset = offset || "";
  20.         collection = Array.isArray(collection) ? collection : [];
  21.         var a = new XMLHttpRequest(),
  22.             b,
  23.             c,
  24.             uid;
  25.         a.open("GET", mw.config.get("wgScriptPath") + "/api.php?action=query&format=json&list=allusers&augroup=" + group + "&aulimit=500&aufrom=" + offset, true);
  26.         a.onload = function() {
  27.             b = JSON.parse(a.responseText);
  28.             // add users to list
  29.             for (uid in b.query.allusers) {
  30.                 c = b.query.allusers[uid];
  31.                 if (c.id > 0) {
  32.                     collection.push(c.name);
  33.                 }
  34.             }
  35.             // check if there are queries left for given right
  36.             if (b.hasOwnProperty("continue")) {
  37.                 fn.getUsersInGroup(group, b.continue.aufrom, collection, cb);
  38.             } else {
  39.                 cb(collection);
  40.             }
  41.         }
  42.         a.onerror = function() {
  43.             console.error("'fn.getUsersInGroup' failed (group='" + group + "', offset='" + offset + ")");
  44.         }
  45.         a.send();
  46.     }
  47.     // get object with users as the keys and corresponding dominant groups as values
  48.     fn.getUpdatedGroupList = function(list, users, cb) {
  49.         if (list.length > 0) {
  50.             var group = list.shift();
  51.             fn.getUsersInGroup(group, "", [], function(usersInGroup) {
  52.                 usersInGroup.forEach(function(user) {
  53.                     if (!users.hasOwnProperty(user)) {
  54.                         users[user] = group;
  55.                     }
  56.                 });
  57.                 fn.getUpdatedGroupList(list, users, cb);
  58.             });
  59.         } else {
  60.             cb(users);
  61.         }
  62.     }
  63.     // construct and update css
  64.     fn.constructCss = function(users) {
  65.         var groups = {},
  66.             prop,
  67.             styleText = [".badge svg {\n\tdisplay: none;\n}"];
  68.         for (prop in users) {
  69.             if (!groups.hasOwnProperty(users[prop])) {
  70.                 groups[users[prop]] = [];
  71.             }
  72.             if (prop == mw.config.get("wgUserName")) {
  73.                 styleText.push("#Rail .User[data-user] .badge {\n\tbackground-repeat: no-repeat;\n}");
  74.             }
  75.             groups[users[prop]].push('#Rail .User[data-user="' + prop + '"] .badge' + (prop == mw.config.get("wgUserName") ? ",\n#ChatHeader .User .badge," : ""));
  76.         }
  77.         for (prop in groups) {
  78.             styleText.push(groups[prop].join(",\n") + " {\n\tbackground: url('" + badges[prop] + "');\n}");
  79.         }
  80.         styleText.push("#Rail .User[data-user] .badge {\n\tbackground-repeat: no-repeat;\n}");
  81.         css.textContent = styleText.join("\n");
  82.     }
  83.     // refresh
  84.     fn.refresh = function() {
  85.         fn.getUpdatedGroupList(groups.concat(), {}, function(groupsmembers) {
  86.             fn.constructCss(groupsmembers);
  87.         });
  88.     }
  89.  
  90.     /* implementations */
  91.     fn.refresh();
  92.  
  93.     /* returned values */
  94.     return {
  95.         f5: fn.refresh,
  96.         changeGroups: function(newGroups) {
  97.             groups = newGroups;
  98.             fn.refresh();
  99.         },
  100.         setBadge: function(group, url) {
  101.             badges[group] = url;
  102.             fn.refresh();
  103.         }
  104.     };
  105. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement