MediaWiki:Common.js: Difference between revisions

From Galaxypedia
No edit summary
(comment out the donator stuff cuz experimental)
 
(18 intermediate revisions by the same user not shown)
Line 4: Line 4:


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


// Sitenotice
// Sitenotice
Line 30: Line 30:


// Experimental donator user group checker
// Experimental donator user group checker
document.addEventListener('DOMContentLoaded', function() {
/*
$(document).ready(function(){
     mw.loader.using('mediawiki.api', function() {
     mw.loader.using('mediawiki.api', function() {
         var api = new mw.Api();
         var api = new mw.Api();
Line 36: Line 37:
         console.log("Searching for donators")
         console.log("Searching for donators")


         // Get all user links on the page. Filter out non-user links (e.g. "talk" links)
         api.get( {
        var userelements = Array.from(document.getElementsByTagName("a")).filter(function (el) {
            action: "query",
            return el.href.includes("User:");
            list: "allusers",
        });
            augroup: "donator",
       
            aulimit: "max",
        console.log("Userelements: " + userelements.join(", "))
            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 ') + ']';


        if (userelements.length != 0) {
             var nodes = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
             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;
            });
           
            console.log("Users: " + users.join(", "));


             api.get( {
             // Add a style for donors
                action: "query",
            for (var i = 0; i < nodes.snapshotLength; i++) {
                list: "users",
                 var node = nodes.snapshotItem(i);
                usprop: "groups",
                 var nodeText = node.textContent;
                ususers: users.join("|"),
                 format: "json"
            } ).done( function ( data ) {
                 var users = data.query.users;


                 for (var i = 0; i < users.length; ++i) {
                // Iterate through each user's name
                     if (users[i].groups.includes("donator")) {
                 for (var j = 0; j < users.length; j++) {
                         console.log(users[i].name + "is a donator");
                     var userName = users[j];
           
                    // Create a regular expression to match the name in the text
                    var regex = new RegExp('\\b' + userName + '\\b', 'g');
           
                    // Check if the user's name is present in the text
                    if (nodeText.match(regex)) {
                         // Create a new HTML string with the name wrapped in a <span> element
                        var highlightedText = nodeText.replace(regex, '<span style="color: #c846ff;">$&</span>');
           
                        // Update the node's HTML with the highlighted text
                        node.innerHTML = highlightedText;
                     }
                     }
                 }
                 }
                // this entire section was made by copilot because im too lazy to do it myself
            }


            } );
        } );
        }
     })
     } );
} );*/
}, false);

Latest revision as of 13:39, 6 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
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);
                var nodeText = node.textContent;

                // Iterate through each user's name
                for (var j = 0; j < users.length; j++) {
                    var userName = users[j];
            
                    // Create a regular expression to match the name in the text
                    var regex = new RegExp('\\b' + userName + '\\b', 'g');
            
                    // Check if the user's name is present in the text
                    if (nodeText.match(regex)) {
                        // Create a new HTML string with the name wrapped in a <span> element
                        var highlightedText = nodeText.replace(regex, '<span style="color: #c846ff;">$&</span>');
            
                        // Update the node's HTML with the highlighted text
                        node.innerHTML = highlightedText;
                    }
                }
                // this entire section was made by copilot because im too lazy to do it myself
            }

        } );
    })
} );*/