MediaWiki:Gadget-sister-projects.js

From Path of Exile 2 Wiki
Revision as of 01:53, 14 October 2024 by Vinifera7 (talk | contribs) (This timing should produce more reliable results)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes. The simplest method that will work for most users is to hold down the ⇧ Shift key and click the Reload toolbar button. For details and other methods, see Help:Clear your browser cache.

/* global mw */
/* jshint strict:true, jquery:true, esversion:5, bitwise:true, curly:true, eqeqeq:true, undef:true, latedef:true, trailingcomma:true */

( function () {
'use strict';

/* Wait for mediawiki.util and DOM ready */
$.when( mw.loader.using(['mediawiki.util',]), $.ready ).then( function () {
    var namespace = mw.config.get('wgNamespaceNumber');

    /*
     * Add menu tab
     */
    var addTab = function (project, interwiki, page) {
        var url = mw.util.getUrl(interwiki + ':' + page);
        if ( namespace === -1 || namespace === 8 ) {
            url += window.location.search; // include query string
        }
        var tooltip = 'View this page on ' + project;
        if ( namespace === 0 ) {
            // Article namespace
            tooltip = 'View this article on ' + project;
        }
        var link = mw.util.addPortletLink(
        'p-namespaces',
            url,
            project,
            'ca-sister-' + interwiki,
            tooltip
        );
        link.classList.add('sister-tab');
    };

    if ( namespace === -1 || namespace === 8 ) {
        // Add tabs on Special and MediaWiki pages
        var page = mw.config.get('wgPageName');
        addTab('Path of Exile Wiki', 'poewiki', page);
    } else {
        // Add tabs from Template:Sister
        var sisterData = document.getElementsByClassName('js-sister-data');
        if (sisterData.length) {
            for ( var i = 0; i < sisterData.length; i++ ) {
                var data = sisterData[i].children;
                for ( var j = 0; j < data.length; j++ ) {
                    addTab(
                        data[j].dataset.project,
                        data[j].dataset.interwiki,
                        data[j].dataset.page
                    );
                }
            }
        }
    }
} );

}() );