MediaWiki:Common.js

From JCraft Wiki
Jump to navigation Jump to search

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*PLEASE DON'T MESS WITH*/
$(function() {
    var homeTab = document.querySelector('#ca-talk a');
    if (homeTab) {
        homeTab.setAttribute('href', 'https://wiki.jcraft-eoe.com/index.php?title=Main_Page');
        // Ensure the link is physically clickable
        homeTab.style.pointerEvents = 'auto';
        homeTab.style.display = 'inline-block';
    }
});
mw.hook('wikipage.content').add(function($content) {
    $content.find('#toc li').has('ul').each(function() {
        var $li = $(this);
        if ($li.children('.fandom-toc-toggle').length === 0) {
            // Insert a purple clickable arrow before the link
            $li.prepend('<span class="fandom-toc-toggle" style="cursor:pointer; display:inline-block; width: 16px; text-align:center; color:#a862ea; margin-right:4px; font-size:10px; transition: transform 0.2s;">▼</span>');
        }
    });

    $content.find('.fandom-toc-toggle').off('click').on('click', function(e) {
        e.preventDefault();
        e.stopPropagation();
        var $toggle = $(this);
        var $sublist = $toggle.siblings('ul');
        $sublist.slideToggle(200, function() {
            if ($sublist.is(':visible')) {
                $toggle.css('transform', 'rotate(0deg)');
            } else {
                $toggle.css('transform', 'rotate(-90deg)');
            }
        });
    });
});
mw.hook('wikipage.content').add(function($content) {
    var term = /\b(BnB|BNB)\b/g;
    var url = 'https://glossary.infil.net/?t=Bread%20and%20Butter';

    $content.find('p, li, dd').each(function() {
        var walker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT, null, false);
        var nodesToReplace = [];

        while (walker.nextNode()) {
            var node = walker.currentNode;
            if (node.parentNode.tagName === 'A') continue;
            
            if (term.test(node.nodeValue)) {
                nodesToReplace.push(node);
            }
        }

        nodesToReplace.forEach(function(node) {
            var newHtml = node.nodeValue.replace(term, '<a href="' + url + '" class="external text" target="_blank" rel="noreferrer noopener">$1</a>');
            $(node).replaceWith(newHtml);
        });
    });
});