Jump to content

MediaWiki:Gadget-NoticeHandler.js

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.
//Notice Handler Gadget: Injects notices at requested page positions

(function() {
    'use strict';

    // Process wikitext links
    function processWikitext(text) {
        if (!text) return '';
        // [[Page Name]]
        text = text.replace(/\[\[([^#|\]]+)\]\]/g, function(match, pageName) {
            var pageUrl = '/index.php/' + pageName.replace(/ /g, '_');
            return '<a href="' + pageUrl + '">' + pageName + '</a>';
        });
        // [[Page|text]]
        text = text.replace(/\[\[([^#|\]]+)\|([^\]]+)\]\]/g, function(match, pageName, linkText) {
            var pageUrl = '/index.php/' + pageName.replace(/ /g, '_');
            return '<a href="' + pageUrl + '">' + linkText + '</a>';
        });
        // [[#anchor|text]]
        text = text.replace(/\[\[#([^|\]]+)\|([^\]]+)\]\]/g, function(match, anchor, linkText) {
            var anchorId = anchor.replace(/ /g, '_');
            return '<a href="#' + anchorId + '">' + linkText + '</a>';
        });
        return text;
    }
    
    // Wait for DOM ready
    $(function() {
        var $noticeElements = $('.notice-data');
        
        if ($noticeElements.length === 0) {
            return;
        }

        var $t = $('#mw-content-text');
        if (!$t.length) { $t = $('.mw-body-content, #content, .mw-content').first(); }
        if (!$t.length) {
            console.error('Notice Handler: No content area found for injection');
            return;
        }
        
        console.log(`Notice Handler: Processing ${$noticeElements.length} notices`);
        
        // Convert data attributes to notice objects and process
        for (var i = 0; i < $noticeElements.length; i++) {
            var $elem = $($noticeElements[i]);
            var noticeType = $elem.data('notice-type');
            var position = $elem.data('notice-position');
            var cssClass = $elem.data('notice-css');
            var content;

            if (noticeType === 'campaign-js') {
                var bannerTemplate = $elem.data('banner-template');
                var bannerTitle = $elem.data('banner-title');

                if (!bannerTemplate || !bannerTitle) {
                    console.warn(`Notice Handler: Skipping invalid campaign-js notice ${i}`);
                    continue;
                }

                var finalContent = bannerTemplate.replace(/\$CAMPAIGN_NAME\$/g, bannerTitle);
                content = processWikitext(finalContent);

            } else {
                content = $elem.data('notice-content');
            }
            
            if (!content || !position) {
                console.warn(`Notice Handler: Skipping invalid notice ${i}`);
                continue;
            }
            
            var $notice = $('<div>')
                .addClass(cssClass || 'notice-box')
                .html(content);
            
            if (position === 'top') {
                $notice.prependTo($t);
            } else if (position === 'bottom') {
                $notice.appendTo($t);
            } else {
                console.error('Notice Handler: Unknown position:', position);
                continue;
            }
            console.log(`Notice Handler: Injected ${position} notice`);
        }
    });
    
})();