MediaWiki

Difference between revisions of "Common.js"

From Shadow Era Wiki

(Created page with "Any JavaScript here will be loaded for all users on every page load.: /** * Collapsible tables * * @version 2.0.1 (2013-03-26) * @source https://www.mediawiki.org/wi...")
 
m
 
(143 intermediate revisions by one user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
+
// -----------------------------
 +
// Random Card: This script randomly selects a SE card in the Main Page.
 +
// -----------------------------
 +
document.addEventListener('DOMContentLoaded', function() {
 +
    var cardData = {
 +
        "se": 202, // Number of cards in the 'se' set
 +
        "ex": 152,
 +
        "sf": 202,
 +
        "ll": 202,
 +
        "cp": 61,
 +
        "rr": 200
 +
    };
  
/**
+
    var prefixes = Object.keys(cardData); // Get prefixes of the card data
* Collapsible tables
+
 
*
+
    var randomPrefix = prefixes[Math.floor(Math.random() * prefixes.length)];
* @version 2.0.1 (2013-03-26)
+
   
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
+
    var maxNumber = cardData[randomPrefix];
* @author [[User:R. Koot]]
+
   
* @author [[User:Krinkle]]
+
    var randomIndex = Math.floor(Math.random() * maxNumber) + 1;
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
+
   
* is supported in MediaWiki core.
+
    var formattedNumber = randomIndex.toString().padStart(3, '0');
  */
+
   
+
    var cardImageUrl = 'https://www.shadowera.com/cards/' + randomPrefix + formattedNumber + '.jpg';
var autoCollapse = 2;
+
   
var collapseCaption = 'hide';
+
    var cardImage = document.getElementById('randomCard');
var expandCaption = 'show';
+
    if (cardImage) {
+
        cardImage.src = cardImageUrl; // Set the image source
function collapseTable( tableIndex ) {
+
        cardImage.alt = randomPrefix + formattedNumber + '.jpg';
var Button = document.getElementById( 'collapseButton' + tableIndex );
+
    }
var Table = document.getElementById( 'collapsibleTable' + tableIndex );
+
});
+
// -----------------------------
if ( !Table || !Button ) {
+
// Background Wiki image and Skin:
return false;
+
// This script applies the background image and overlay styles to the wiki body.
}
+
// Changes color in different area and set index to objects.
+
// -----------------------------
var Rows = Table.rows;
+
$(document).ready(function() {
+
    // Background Image for the wiki
if ( Button.firstChild.data == collapseCaption ) {
+
    $('body').css({
for ( var i = 1; i < Rows.length; i++ ) {
+
        'background-image': 'url("https://www.shadowera.com/landing/img/developers.jpg")',
Rows[i].style.display = 'none';
+
        'background-size': 'cover',
}
+
        'background-attachment': 'fixed',
Button.firstChild.data = expandCaption;
+
        'background-repeat': 'no-repeat'
} else {
+
    });
for ( var i = 1; i < Rows.length; i++ ) {
+
 
Rows[i].style.display = Rows[0].style.display;
+
    // Add main Overlay
}
+
    $('body').append('<div class="overlay"></div>');
Button.firstChild.data = collapseCaption;
+
    $('.overlay').css({
}
+
        'position': 'absolute',
 +
        'top': '55px',  
 +
        'left': '1%',
 +
        'width': '98%',
 +
        'background-color': 'rgba(24, 29, 35, 0.9)',
 +
        'z-index': '1',
 +
        'pointer-events': 'none',
 +
        'border-radius': '10px'
 +
    });
 +
 
 +
    // Adjust the height of the main overlay to reach the bottom of the page
 +
    function adjustOverlayHeight() {
 +
        var documentHeight = $(document).height();  
 +
        var overlayStart = $('.overlay').position().top;  
 +
        var overlayHeight = documentHeight - overlayStart;
 +
        $('.overlay').css('height', overlayHeight + 'px');  
 +
    }
 +
 
 +
    adjustOverlayHeight();
 +
 
 +
    $(window).resize(function() {
 +
        adjustOverlayHeight();
 +
    });
 +
 
 +
    // Check if the current page is Common.css or Common.js
 +
    var currentPage = mw.config.get('wgPageName');
 +
    if (currentPage === 'MediaWiki:Common.css' || currentPage === 'MediaWiki:Common.js') {
 +
        // Add the second overlay for specific pages with the same parameters as the main overlay
 +
        $('body').append('<div class="overlay-common"></div>');
 +
        $('.overlay-common').css({
 +
            'position': 'absolute',
 +
            'top': '55px',
 +
            'left': '1%',
 +
            'width': '98%',
 +
            'background-color': 'rgba(48, 57, 66, 0.5)',
 +
            'z-index': '1',
 +
            'pointer-events': 'none',
 +
            'border-radius': '10px'
 +
        });
 +
 
 +
        // Adjust the height of the overlay to reach the bottom of the page
 +
        function adjustOverlayHeightCommon() {
 +
            var documentHeight = $(document).height();
 +
            var overlayStart = $('.overlay-common').position().top;
 +
            var overlayHeight = documentHeight - overlayStart;
 +
            $('.overlay-common').css('height', overlayHeight + 'px');  
 +
        }
 +
 
 +
        adjustOverlayHeightCommon();
 +
 
 +
        $(window).resize(function() {
 +
            adjustOverlayHeightCommon();
 +
        });
 +
    }
 +
});
 +
 
 +
// Apply style to wiki content: This section applies styles to the main content area of the wiki.
 +
$('#mw-content-text').css({
 +
    'position': 'relative',
 +
    'color': 'white',
 +
    'padding': '20px'
 +
});
 +
 
 +
// Apply styles to headings
 +
$('.mw-headline').css({
 +
    'color': 'white'
 +
});
 +
 
 +
// Render comments text in <span> elements with class co1 in light gray color
 +
$('.co1').css({
 +
    'color': '#b3b3b3'
 +
});
 +
 
 +
// Set the title color to white
 +
$('.title').css({
 +
    'color': 'white',
 +
    'position': 'relative',
 +
    'z-index': '7'
 +
});
 +
 
 +
// Make the tagline less bright
 +
$('#tagline').css({
 +
    'color': 'rgba(255, 255, 255, 0.5)'
 +
});
 +
 
 +
// Set the text color of abbr elements to white
 +
$('.minoredit').css({
 +
    'color': 'white',
 +
    'position': 'relative',
 +
    'z-index': '7'
 +
});
 +
 
 +
// Set the text color of <code> elements to white
 +
$('code').css({
 +
    'color': 'white',
 +
    'position': 'relative',
 +
    'z-index': '7'
 +
});
 +
 
 +
// Set the text color of specific <label> element in black
 +
$('#mw-editpage-watch').css({
 +
    'color': 'black'
 +
});
 +
 
 +
// Set the text color of specific <label> element in black
 +
$('#mw-editpage-minoredit').css({
 +
    'color': 'black'
 +
});
 +
 
 +
// Set the text color of specific <label> element in black
 +
$('label[for="wpSummary"]').css({
 +
    'color': 'black'
 +
});
 +
 
 +
// Set the text color of all other <label> elements with the for attribute to white
 +
$('label[for]').not('#mw-editpage-watch, #mw-editpage-minoredit, label[for="wpSummary"]').css({
 +
    'color': 'white'
 +
});
 +
 
 +
// Set the text color of all <p> elements in the #editpage-copywarn div to black
 +
$('#editpage-copywarn p').css({
 +
    'color': 'black'
 +
});
 +
 
 +
// Change the color of elements <span> with the class mw-plusminus-neg
 +
$('.mw-plusminus-neg').css({
 +
    'color': '#e88c18'
 +
});
 +
 
 +
// Change the color of elements <a> with the class mw-userlink
 +
$('.mw-userlink').css({
 +
    'color': '#e88c18'
 +
});
 +
 
 +
// Change the color of elements <a> with the class new
 +
$('a.new[title="User talk:Blopi (page does not exist)"]').css({
 +
    'color': '#e88c18'
 +
});
 +
 
 +
// Target the bold <b> text
 +
$('#editpage-copywarn b').css({
 +
    'color': 'black'
 +
});
 +
 
 +
// Render Subcategories the same color as tagline
 +
$('h2').css({
 +
    'color': 'rgba(255, 255, 255, 0.8)'
 +
});
 +
 
 +
// Set the text color of <span> elements with the class mw-plusminus-pos to light green
 +
$('.mw-plusminus-pos').css({
 +
    'color': '#66ff66'
 +
});
 +
 
 +
// Render Subcategories the same color as tagline
 +
$('h4').css({
 +
    'color': 'rgba(255, 255, 255, 0.8)'
 +
});
 +
 
 +
// Render Subcategories the same color as tagline
 +
$('h5').css({
 +
    'color': 'orange',
 +
    'position': 'relative',
 +
    'z-index': '7'
 +
});
 +
 
 +
// Change the color of table cells
 +
$('table, td, th').css({
 +
    'background-color': '#1c242c',
 +
    'color': 'white',
 +
    'border': '1px solid #3a3e42'
 +
});
 +
 
 +
// Change the color of <h3> to be the same as the tagline
 +
$('h3').css({
 +
    'color': 'rgba(255, 255, 255, 0.5)'
 +
});
 +
 
 +
// Load the collapsible module from MediaWiki to make sections collapsible.
 +
mw.loader.load('mediawiki.collapsible');
 +
 
 +
// This function displays or hides an element with the specified ID.
 +
function toggleContent(id) {
 +
    var element = document.getElementById(id);
 +
    if (element.style.display === 'none' || element.style.display === '') {
 +
        element.style.display = 'table-row-group'; // Show the element
 +
    } else {
 +
        element.style.display = 'none'; // Hide the element
 +
    }
 
}
 
}
+
 
function createClickHandler( tableIndex ) {
+
// -----------------------------
return function ( e ) {
+
// Script for Highlighting Backticks
e.preventDefault();
+
// Applies custom visual styles to text wrapped in backticks without altering the content
collapseTable( tableIndex );
+
// This script waits for the document to fully load, and applies a visual style
}
+
// to any text that is surrounded by backticks (` `), without altering the content in previous.
}
+
// -----------------------------
+
 
function createCollapseButtons() {
+
document.addEventListener('DOMContentLoaded', function () {
var tableIndex = 0;
+
    // Check if we are in edit mode (to avoid changing the editor content)
var NavigationBoxes = {};
+
    if (document.querySelector('#wpTextbox1') !== null) {
var Tables = document.getElementsByTagName( 'table' );
+
        return; // Do nothing in edit mode
+
    }
for ( var i = 0; i < Tables.length; i++ ) {
+
 
if ( $( Tables[i] ).hasClass( 'collapsible' ) ) {
+
    //
/* only add button and increment count if there is a header row to work with */
+
    // Function to highlight text between backticks by wrapping it in a span element
var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
+
    // @param {string} text
if ( !HeaderRow ) {
+
    // @returns {string}
continue;
+
    //
}
+
    function highlightBackticks(text) {
var Header = HeaderRow.getElementsByTagName( 'th' )[0];
+
        const backtickRegex = /`([^`]+)`/g; // Matches text between backticks
if ( !Header ) {
+
        const styledText = text.replace(backtickRegex, function (match, p1) {
continue;
+
            return '<span class="backtick-style">' + p1 + '</span>';  
}
+
        });
+
        return styledText; // Return the modified text
NavigationBoxes[tableIndex] = Tables[i];
+
    }
Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
+
 
+
    //
var Button = document.createElement( 'span' );
+
    // Traverse all text nodes and apply the backtick highlighting
var ButtonLink = document.createElement( 'a' );
+
    // @param {Node} node
var ButtonText = document.createTextNode( collapseCaption );
+
    //
+
    function traverseTextNodes(node) {
Button.style.styleFloat = 'right';
+
        if (node.nodeType === 3 && node.nodeValue.includes('`')) {
Button.style.cssFloat = 'right';
+
            const span = document.createElement('span');  
Button.style.fontWeight = 'normal';
+
            span.innerHTML = highlightBackticks(node.nodeValue);  
Button.style.textAlign = 'right';
+
            node.parentNode.replaceChild(span, node);  
Button.style.width = '6em';
+
        } else if (node.nodeType === 1 && node.childNodes) {
+
            node.childNodes.forEach(traverseTextNodes);  
ButtonLink.style.color = Header.style.color;
+
        }
ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
+
    }
$( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
+
 
ButtonLink.appendChild( ButtonText );
+
    // Apply the changes to the entire body
+
    traverseTextNodes(document.body);
Button.appendChild( document.createTextNode( '[' ) );
+
});
Button.appendChild( ButtonLink );
+
Button.appendChild( document.createTextNode( ']' ) );
+
+
Header.insertBefore( Button, Header.childNodes[0] );
+
tableIndex++;
+
}
+
}
+
+
for ( var i = 0; i < tableIndex; i++ ) {
+
if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||
+
( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) )
+
) {
+
collapseTable( i );
+
}
+
}
+
}
+
+
$( createCollapseButtons );
+

Latest revision as of 17:26, 12 October 2024

// -----------------------------
// Random Card: This script randomly selects a SE card in the Main Page.
// -----------------------------
document.addEventListener('DOMContentLoaded', function() {
    var cardData = {
        "se": 202, // Number of cards in the 'se' set
        "ex": 152,
        "sf": 202,
        "ll": 202,
        "cp": 61,
        "rr": 200
    };
 
    var prefixes = Object.keys(cardData); // Get prefixes of the card data
 
    var randomPrefix = prefixes[Math.floor(Math.random() * prefixes.length)];
 
    var maxNumber = cardData[randomPrefix]; 
 
    var randomIndex = Math.floor(Math.random() * maxNumber) + 1;
 
    var formattedNumber = randomIndex.toString().padStart(3, '0'); 
 
    var cardImageUrl = 'https://www.shadowera.com/cards/' + randomPrefix + formattedNumber + '.jpg';
 
    var cardImage = document.getElementById('randomCard'); 
    if (cardImage) {
        cardImage.src = cardImageUrl; // Set the image source
        cardImage.alt = randomPrefix + formattedNumber + '.jpg';
    }
});
// -----------------------------
// Background Wiki image and Skin:
// This script applies the background image and overlay styles to the wiki body.
// Changes color in different area and set index to objects.
// -----------------------------
$(document).ready(function() {
    // Background Image for the wiki
    $('body').css({
        'background-image': 'url("https://www.shadowera.com/landing/img/developers.jpg")',
        'background-size': 'cover',
        'background-attachment': 'fixed',
        'background-repeat': 'no-repeat'
    });
 
    // Add main Overlay
    $('body').append('<div class="overlay"></div>'); 
    $('.overlay').css({
        'position': 'absolute',
        'top': '55px',  
        'left': '1%', 
        'width': '98%',
        'background-color': 'rgba(24, 29, 35, 0.9)', 
        'z-index': '1', 
        'pointer-events': 'none', 
        'border-radius': '10px' 
    });
 
    // Adjust the height of the main overlay to reach the bottom of the page
    function adjustOverlayHeight() {
        var documentHeight = $(document).height(); 
        var overlayStart = $('.overlay').position().top; 
        var overlayHeight = documentHeight - overlayStart;
        $('.overlay').css('height', overlayHeight + 'px'); 
    }
 
    adjustOverlayHeight(); 
 
    $(window).resize(function() {
        adjustOverlayHeight();
    });
 
    // Check if the current page is Common.css or Common.js
    var currentPage = mw.config.get('wgPageName');
    if (currentPage === 'MediaWiki:Common.css' || currentPage === 'MediaWiki:Common.js') {
        // Add the second overlay for specific pages with the same parameters as the main overlay
        $('body').append('<div class="overlay-common"></div>'); 
        $('.overlay-common').css({
            'position': 'absolute', 
            'top': '55px', 
            'left': '1%', 
            'width': '98%', 
            'background-color': 'rgba(48, 57, 66, 0.5)', 
            'z-index': '1',
            'pointer-events': 'none',
            'border-radius': '10px' 
        });
 
        // Adjust the height of the overlay to reach the bottom of the page
        function adjustOverlayHeightCommon() {
            var documentHeight = $(document).height(); 
            var overlayStart = $('.overlay-common').position().top; 
            var overlayHeight = documentHeight - overlayStart;
            $('.overlay-common').css('height', overlayHeight + 'px'); 
        }
 
        adjustOverlayHeightCommon();
 
        $(window).resize(function() {
            adjustOverlayHeightCommon();
        });
    }
});
 
// Apply style to wiki content: This section applies styles to the main content area of the wiki.
$('#mw-content-text').css({
    'position': 'relative',
    'color': 'white', 
    'padding': '20px'
});
 
// Apply styles to headings
$('.mw-headline').css({
    'color': 'white'
});
 
// Render comments text in <span> elements with class co1 in light gray color
$('.co1').css({
    'color': '#b3b3b3'
});
 
// Set the title color to white
$('.title').css({
    'color': 'white',
    'position': 'relative',
    'z-index': '7'
});
 
// Make the tagline less bright
$('#tagline').css({
    'color': 'rgba(255, 255, 255, 0.5)'
});
 
// Set the text color of abbr elements to white
$('.minoredit').css({
    'color': 'white',
    'position': 'relative',
    'z-index': '7'
});
 
// Set the text color of <code> elements to white
$('code').css({
    'color': 'white',
    'position': 'relative',
    'z-index': '7'
});
 
// Set the text color of specific <label> element in black
$('#mw-editpage-watch').css({
    'color': 'black'
});
 
// Set the text color of specific <label> element in black
$('#mw-editpage-minoredit').css({
    'color': 'black'
});
 
// Set the text color of specific <label> element in black
$('label[for="wpSummary"]').css({
    'color': 'black'
});
 
// Set the text color of all other <label> elements with the for attribute to white
$('label[for]').not('#mw-editpage-watch, #mw-editpage-minoredit, label[for="wpSummary"]').css({
    'color': 'white'
});
 
// Set the text color of all <p> elements in the #editpage-copywarn div to black
$('#editpage-copywarn p').css({
    'color': 'black'
});
 
// Change the color of elements <span> with the class mw-plusminus-neg
$('.mw-plusminus-neg').css({
    'color': '#e88c18'
});
 
// Change the color of elements <a> with the class mw-userlink
$('.mw-userlink').css({
    'color': '#e88c18'
});
 
// Change the color of elements <a> with the class new
$('a.new[title="User talk:Blopi (page does not exist)"]').css({
    'color': '#e88c18'
});
 
// Target the bold <b> text
$('#editpage-copywarn b').css({
    'color': 'black'
});
 
// Render Subcategories the same color as tagline
$('h2').css({
    'color': 'rgba(255, 255, 255, 0.8)'
});
 
// Set the text color of <span> elements with the class mw-plusminus-pos to light green
$('.mw-plusminus-pos').css({
    'color': '#66ff66'
});
 
// Render Subcategories the same color as tagline
$('h4').css({
    'color': 'rgba(255, 255, 255, 0.8)'
});
 
// Render Subcategories the same color as tagline
$('h5').css({
    'color': 'orange',
    'position': 'relative',
    'z-index': '7'
});
 
// Change the color of table cells
$('table, td, th').css({
    'background-color': '#1c242c',
    'color': 'white',
    'border': '1px solid #3a3e42'
});
 
// Change the color of <h3> to be the same as the tagline
$('h3').css({
    'color': 'rgba(255, 255, 255, 0.5)'
});
 
// Load the collapsible module from MediaWiki to make sections collapsible.
mw.loader.load('mediawiki.collapsible');
 
// This function displays or hides an element with the specified ID.
function toggleContent(id) {
    var element = document.getElementById(id);
    if (element.style.display === 'none' || element.style.display === '') {
        element.style.display = 'table-row-group'; // Show the element
    } else {
        element.style.display = 'none'; // Hide the element
    }
}
 
// -----------------------------
// Script for Highlighting Backticks
// Applies custom visual styles to text wrapped in backticks without altering the content
// This script waits for the document to fully load, and applies a visual style 
// to any text that is surrounded by backticks (` `), without altering the content in previous.
// -----------------------------
 
document.addEventListener('DOMContentLoaded', function () {
    // Check if we are in edit mode (to avoid changing the editor content)
    if (document.querySelector('#wpTextbox1') !== null) {
        return; // Do nothing in edit mode
    }
 
    //
    // Function to highlight text between backticks by wrapping it in a span element
    // @param {string} text
    // @returns {string}
    //
    function highlightBackticks(text) {
        const backtickRegex = /`([^`]+)`/g; // Matches text between backticks
        const styledText = text.replace(backtickRegex, function (match, p1) {
            return '<span class="backtick-style">' + p1 + '</span>'; 
        });
        return styledText; // Return the modified text
    }
 
    //
    // Traverse all text nodes and apply the backtick highlighting
    // @param {Node} node
    //
    function traverseTextNodes(node) {
        if (node.nodeType === 3 && node.nodeValue.includes('`')) { 
            const span = document.createElement('span'); 
            span.innerHTML = highlightBackticks(node.nodeValue); 
            node.parentNode.replaceChild(span, node); 
        } else if (node.nodeType === 1 && node.childNodes) {
            node.childNodes.forEach(traverseTextNodes); 
        }
    }
 
    // Apply the changes to the entire body
    traverseTextNodes(document.body);
});