MediaWiki:Gadgets/mpEditLinks/main.js

From Tensura Wiki
Revision as of 10:55, 25 September 2026 by imported>Merge (merge.py)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.
$( function () {
    if(!document.getElementById('mp-container')) return;
    
    var rootpage = document.getElementById('mp-container').dataset['rootpage']; // Get the rootpage passed from the main page module, so we don't need to make an API call

	/* generate the links here instead of in the wikitext, so that users who can't see them don't have them
	* in the DOM at all, and screenreaders and search engines don't get confused */
    function createLinks(){
        $('.mp-box').each(function(_, el){
        	var params = '?action=edit';
        	
        	// only add the long preload stuff if the box doesn't exist, for prettier urls
        	if ( $(el).hasClass('missing') ){

        		params += '&preload=Template:Main page box/preload&preloadparams[0]=' + el.dataset['boxId'].charAt(0).toUpperCase() + el.dataset['boxId'].slice(1); /* preloadparam is box id with first letter capitalized */
        	}
        	
            $('<a></a>', {
                href: mw.util.getUrl(rootpage + '/' + el.dataset['boxId']) + params,
                text: 'Edit ' + rootpage + '/' + el.dataset['boxId']
            }).appendTo(
                $('<div></div>', {
                    class: 'mp-edit-link'
                }).prependTo(el)
            );
        });
    }

    function toggleLinks(){
        $('.mp-edit-link').each(function(_, el){
            $(el).toggle().css('display none;');
        });
    }

    $('<span></span>', {
        text: 'Toggle edit buttons',
        class: 'mp-edit-toggle',
        on: {click: toggleLinks},
    }).appendTo($('#top'));

    createLinks();

});