MediaWiki:Common.js: Difference between revisions

From Galaxypedia
No edit summary
No edit summary
Line 30: Line 30:


// Experimental donator user group checker
// Experimental donator user group checker
window.onload = function() {
document.addEventListener('DOMContentLoaded', function() {
     mw.loader.using('mediawiki.api', function() {
     mw.loader.using('mediawiki.api', function() {
         var api = new mw.Api();
         var api = new mw.Api();
Line 62: Line 62:
                 var users = data.query.users;
                 var users = data.query.users;


                 for (user in users) {
                 for (var i = 0; i < users.length; ++i) {
                     if (users[user].groups.includes("donator")) {
                     if (users[i].groups.includes("donator")) {
                         console.log(users[user].name + "is a donator");
                         console.log(users[i].name + "is a donator");
                     }
                     }
                 }
                 }
Line 71: Line 71:
         }
         }
     } );
     } );
}
}, false);

Revision as of 14:06, 5 October 2023

/* Any JavaScript here will be loaded for all users on every page load. */

//Note that while loading content, appending "/wiki" or "/w" before the "/index..." statement may cause 404 errors or mime type mismatch errors (403)

//Import the Navbox.js scripts for the navbox price/name sorting functionality
mw.loader.load( '/index.php?title=MediaWiki:Navbox.js&action=raw&ctype=text/javascript' );

// Sitenotice
var sitenotice = document.querySelector('#siteNotice .mw-dismissable-notice');
if (!sitenotice) sitenotice = document.querySelector('#siteNotice');
if (sitenotice) {
	sitenotice.classList.add('sitenoticestyling');
}

// Add extra info to infobox's cost header to notify users to take it with a grain of salt
const headers = document.querySelectorAll('.pi-header');

if (headers) {
    headers.forEach(function(header) {
        const paragraph = header.querySelector('p');

        if (paragraph.textContent.includes('Cost')) {
            header.style.textDecoration = 'underline dashed';
            header.style.cursor = "help";
            header.title = "The Cost section is meant to be taken with a grain of salt. Price fluctuates based on the economy, and the stats listed are meant to serve as a rough estimate.\nNote: KetchupBot does not automatically update this section.";
        	console.log("Added styling to Cost header on infobox");
        }
    })
}

// Experimental donator user group checker
document.addEventListener('DOMContentLoaded', function() {
    mw.loader.using('mediawiki.api', function() {
        var api = new mw.Api();

        // Get all user links on the page. Filter out non-user links (e.g. "talk" links)
        var userelements = Array.from(document.getElementsByTagName("a")).filter(function (el) {
            return el.href.includes("User:");
        });

        if (userelements.length != 0) {
            var users = userelements.map(function (el) {
                var match = el.href.match(new RegExp("/(?<=\/wiki\/User:).*/gis"))
                if (match) {
                    return match[0];
                } else {
                    return null;
                }
            }).filter(function (el) {
                return el != null;
            });
            
            print("Users: " + users.join(", "));

            api.get( {
                action: "query",
                list: "users",
                usprop: "groups",
                ususers: users.join("|"),
                format: "json"
            } ).done( function ( data ) {
                var users = data.query.users;

                for (var i = 0; i < users.length; ++i) {
                    if (users[i].groups.includes("donator")) {
                        console.log(users[i].name + "is a donator");
                    }
                }

            } );
        }
    } );
}, false);