MediaWiki:Common.js

From Galaxypedia
Revision as of 15:18, 5 October 2023 by Smallketchup82 (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* 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
var headers = document.querySelectorAll('.pi-header');

if (headers) {
    headers.forEach(function(header) {
        var 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).ready(function(){
    mw.loader.using('mediawiki.api', function() {
        var api = new mw.Api();
        
        console.log("Searching for donators")

        api.get( {
            action: "query",
            list: "allusers",
            augroup: "donator",
            aulimit: "max",
            format: "json"
        } ).done( function ( data ) {
            var users = data.query.allusers;

            users = users.map(function(user) {
                return user.name;
            })

            var xpath = users.map(function(user) {
                return "contains(text(), '" + user + "')"
            })

            xpath = '//*[' + xpath.join(' or ') + ']';

            var nodes = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

            // Add a style for donors
            for (var i = 0; i < nodes.snapshotLength; i++) {
                var node = nodes.snapshotItem(i);

                // find the person's name in the node, and surround their name (in the innerhtml) with a span and change the color to purple. Make sure that the name only exists in the text, and not in the html (so we don't change the color of the link)
                var name = node.innerHTML.match(/^(.*?)\s/)[1];
                var nameIndex = node.innerHTML.indexOf(name);
                var nameLength = name.length;
                var html = node.innerHTML;
                var text = node.textContent;
                var textIndex = text.indexOf(name);
                var textLength = name.length;
                var startTag = '<span style="color:purple">';
                var endTag = '</span>';
                var htmlStart = html.substring(0, nameIndex);
                var htmlEnd = html.substring(nameIndex + nameLength);
                var textStart = text.substring(0, textIndex);
                var textEnd = text.substring(textIndex + textLength);
                var newHtml = htmlStart + startTag + html.substring(nameIndex, nameIndex + nameLength) + endTag + htmlEnd;

                // replace the node's innerHTML with the new html
                node.innerHTML = newHtml;
                // this entire section was made by copilot cuz im too lazy to do it myself
            }

        } );
    })
} );