Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var badges = (function() {
- var groups = ["bureaucrat", "sysop", "junioradmin", "rollback", "chatmoderator", "autopatrolled"], // order determines which group is dominant
- badges = {
- "bureaucrat": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
- "sysop": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
- "junioradmin": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
- "rollback": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
- "chatmoderator": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png",
- "autopatrolled": "http://www.famfamfam.com/lab/icons/silk/icons/browser_internetexplorer.png"
- },
- fn = {},
- css = document.createElement("style");
- css.type = "text/css";
- document.head.appendChild(css);
- /* functions */
- // get list of users in a given group
- fn.getUsersInGroup = function(group, offset, collection, cb) {
- offset = offset || "";
- collection = Array.isArray(collection) ? collection : [];
- var a = new XMLHttpRequest(),
- b,
- c,
- uid;
- a.open("GET", mw.config.get("wgScriptPath") + "/api.php?action=query&format=json&list=allusers&augroup=" + group + "&aulimit=500&aufrom=" + offset, true);
- a.onload = function() {
- b = JSON.parse(a.responseText);
- // add users to list
- for (uid in b.query.allusers) {
- c = b.query.allusers[uid];
- if (c.id > 0) {
- collection.push(c.name);
- }
- }
- // check if there are queries left for given right
- if (b.hasOwnProperty("continue")) {
- fn.getUsersInGroup(group, b.continue.aufrom, collection, cb);
- } else {
- cb(collection);
- }
- }
- a.onerror = function() {
- console.error("'fn.getUsersInGroup' failed (group='" + group + "', offset='" + offset + ")");
- }
- a.send();
- }
- // get object with users as the keys and corresponding dominant groups as values
- fn.getUpdatedGroupList = function(list, users, cb) {
- if (list.length > 0) {
- var group = list.shift();
- fn.getUsersInGroup(group, "", [], function(usersInGroup) {
- usersInGroup.forEach(function(user) {
- if (!users.hasOwnProperty(user)) {
- users[user] = group;
- }
- });
- fn.getUpdatedGroupList(list, users, cb);
- });
- } else {
- cb(users);
- }
- }
- // construct and update css
- fn.constructCss = function(users) {
- var groups = {},
- prop,
- styleText = [".badge svg {\n\tdisplay: none;\n}"];
- for (prop in users) {
- if (!groups.hasOwnProperty(users[prop])) {
- groups[users[prop]] = [];
- }
- if (prop == mw.config.get("wgUserName")) {
- styleText.push("#Rail .User[data-user] .badge {\n\tbackground-repeat: no-repeat;\n}");
- }
- groups[users[prop]].push('#Rail .User[data-user="' + prop + '"] .badge' + (prop == mw.config.get("wgUserName") ? ",\n#ChatHeader .User .badge," : ""));
- }
- for (prop in groups) {
- styleText.push(groups[prop].join(",\n") + " {\n\tbackground: url('" + badges[prop] + "');\n}");
- }
- styleText.push("#Rail .User[data-user] .badge {\n\tbackground-repeat: no-repeat;\n}");
- css.textContent = styleText.join("\n");
- }
- // refresh
- fn.refresh = function() {
- fn.getUpdatedGroupList(groups.concat(), {}, function(groupsmembers) {
- fn.constructCss(groupsmembers);
- });
- }
- /* implementations */
- fn.refresh();
- /* returned values */
- return {
- f5: fn.refresh,
- changeGroups: function(newGroups) {
- groups = newGroups;
- fn.refresh();
- },
- setBadge: function(group, url) {
- badges[group] = url;
- fn.refresh();
- }
- };
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement