MediaWiki:Common.js

From EQOA Sandstorm Wiki
Revision as of 19:11, 16 May 2026 by RenodorDev (talk | contribs)
Jump to navigationJump 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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
(function () {
    function rawPage(title) {
        return mw.config.get('wgScript') +
            '?title=' + encodeURIComponent(title) +
            '&action=raw&ctype=text/javascript';
    }

    function loadScript(title) {
        return mw.loader.getScript(rawPage(title));
    }

    function applyWikiTheme(theme) {
        var isLight = theme === 'light';

        document.documentElement.classList.toggle('eqoa-theme-light', isLight);
        document.documentElement.classList.toggle('eqoa-theme-dark', !isLight);

        if (document.body) {
            document.body.classList.toggle('eqoa-theme-light', isLight);
            document.body.classList.toggle('eqoa-theme-dark', !isLight);
        }

        try {
            localStorage.setItem('eqoa-wiki-theme', isLight ? 'light' : 'dark');
        } catch (e) {}

        var button = document.getElementById('eqoa-theme-toggle');

        if (button) {
            button.innerHTML = isLight ? '☾' : '☼';
            button.title = isLight ? 'Switch to Dark Theme' : 'Switch to White Theme';
            button.setAttribute(
                'aria-label',
                isLight ? 'Switch to Dark Theme' : 'Switch to White Theme'
            );
        }

        if (window.dispatchEvent) {
            window.dispatchEvent(new CustomEvent('eqoa-wiki-theme-change', {
                detail: {
                    theme: isLight ? 'light' : 'dark'
                }
            }));
        }
    }

    function getSavedWikiTheme() {
        try {
            return localStorage.getItem('eqoa-wiki-theme') === 'light'
                ? 'light'
                : 'dark';
        } catch (e) {
            return 'dark';
        }
    }

    window.EQOA_APPLY_WIKI_THEME = applyWikiTheme;
    window.EQOA_GET_WIKI_THEME = getSavedWikiTheme;

    mw.hook('wikipage.content').add(function () {
        var button = document.getElementById('eqoa-theme-toggle');

        if (!button) {
            button = document.createElement('button');
            button.id = 'eqoa-theme-toggle';
            button.type = 'button';
            button.className = 'eqoa-theme-toggle';

            button.addEventListener('click', function () {
                var isLight = document.body.classList.contains('eqoa-theme-light');
                applyWikiTheme(isLight ? 'dark' : 'light');
            });

            document.body.appendChild(button);
        }

        applyWikiTheme(getSavedWikiTheme());
    });

    mw.loader.using('mediawiki.util').then(function () {
        loadScript('MediaWiki:CharacterBuilderData.js')
            .then(function () {
                return $.when(
                    loadScript('MediaWiki:LZString.js'),
                    loadScript('MediaWiki:QRCode.js'),
                    loadScript('MediaWiki:Html2Canvas.js')
                );
            })
            .then(function () {
                return loadScript('MediaWiki:CharacterBuilder.js');
            })
            .catch(function (error) {
                console.error('Character Builder failed to load:', error);
            });

        loadScript('MediaWiki:ClassMasteryData.js')
            .then(function () {
                return loadScript('MediaWiki:ClassMasteryRawData.js');
            })
            .then(function () {
                return loadScript('MediaWiki:ClassMasteryCalculator.js');
            })
            .catch(function (error) {
                console.error('Class Mastery failed to load:', error);
            });
    });
})();