MediaWiki:Gadget-topIcons.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
Tag: Reverted
No edit summary
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
"use strict";

/**
/**
* Moves icons from [[Template:External]] to the top bar, next to "Article" and "Talk" tabs;
* Moves icons from [[Template:External]] to the top bar, next to "Article" and "Talk" tabs;
Line 4: Line 6:
*/
*/
(function ($, mw) {
(function ($, mw) {
let namespace = mw.config.get('wgNamespaceNumber');
var namespace = mw.config.get('wgNamespaceNumber');

// automatically show externals on Special and MediaWiki namespaces
if (namespace === -1 || namespace === 8) {
let page = mw.config.get('wgPageName');

addLink('the RuneScape Wiki', 'rsw', page);
addLink('the RuneScape Classic Wiki', 'classicrsw', page);
addLink('the Meta Weird Gloop Wiki', 'meta', page);
}
else {
// make this work when "Show preview without reloading the page" is checked
mw.hook('wikipage.content').add(function ($content) {
if (!$content.has('.rs-external-header-links').length) return;


// automatically show externals on Special and MediaWiki namespaces
// clear existing externals first so previewing doesn't add an infinite amount
if (namespace === -1 || namespace === 8) {
$('.gadget-external-icon').remove();
var page = mw.config.get('wgPageName');
addLink('the RuneScape Wiki', 'rsw', page);
addLink('the RuneScape Classic Wiki', 'classicrsw', page);
addLink('the Meta Weird Gloop Wiki', 'meta', page);
} else {
// make this work when "Show preview without reloading the page" is checked
mw.hook('wikipage.content').add(function ($content) {
if (!$content.has('.rs-external-header-links').length) return;


// clear existing externals first so previewing doesn't add an infinite amount
// convert whatever [[Template:External]] gives us
$('.rs-header-icon').each(function (index, icon) {
$('.gadget-external-icon').remove();
addLink(icon.dataset.wikiname, icon.dataset.interwiki, icon.dataset.page);
});


// convert whatever [[Template:External]] gives us
// clean up
$('.rs-external-header-links').remove();
$('.rs-header-icon').each(function (index, icon) {
addLink(icon.dataset.wikiname, icon.dataset.interwiki, icon.dataset.page);
});
});
}


// clean up
function addLink(wikiname, interwiki, page) {
$('.rs-external-header-links').remove();
let url = mw.mw.util.getUrl(interwiki + ':' + page);
});
let title = wikiname + ' also has an article on ' + page + '.';
}


function addLink(wikiname, interwiki, page) {
// wikipedia links aren't forwarded - see [[Special:Interwiki]]
if (interwiki === 'wikipedia') {
var url = mw.util.getUrl(interwiki + ':' + page);
var title = wikiname + ' also has an article on ' + page + '.';
url = '//en.wikipedia.org/wiki/' + encodeURIComponent(page);
}


// wikipedia links aren't forwarded - see [[Special:Interwiki]]
// Special and MediaWiki namespaces
if (namespace === -1 || namespace === 8) {
if (interwiki === 'wikipedia') {
url = '//en.wikipedia.org/wiki/' + encodeURIComponent(page);
url += window.location.search; // include url parameters
}
title = 'Open this page on ' + wikiname + '.';
}


// Special and MediaWiki namespaces
let link = mw.mw.util.addPortletLink(
if (namespace === -1 || namespace === 8) {
'p-namespaces',
url += window.location.search; // include url parameters
url,
title = 'Open this page on ' + wikiname + '.';
'', // no text - use background-image instead
}
'gadget-external-' + interwiki, // button id
var link = mw.util.addPortletLink('p-namespaces', url, '',
title // title text shown on hover
// no text - use background-image instead
);
'gadget-external-' + interwiki,
// button id
title // title text shown on hover
);


// for styling all links at once - addPortletLink only sets an id
// for styling all links at once - addPortletLink only sets an id
$(link).addClass('gadget-external-icon');
$(link).addClass('gadget-external-icon');
}
}
})(jQuery, mediaWiki);
})(jQuery, mediaWiki);

Latest revision as of 12:31, 20 October 2024

"use strict";

/**
 * Moves icons from [[Template:External]] to the top bar, next to "Article" and "Talk" tabs;
 * rewritten from scratch for modern MediaWiki
 */
(function ($, mw) {
  var namespace = mw.config.get('wgNamespaceNumber');

  // automatically show externals on Special and MediaWiki namespaces
  if (namespace === -1 || namespace === 8) {
    var page = mw.config.get('wgPageName');
    addLink('the RuneScape Wiki', 'rsw', page);
    addLink('the RuneScape Classic Wiki', 'classicrsw', page);
    addLink('the Meta Weird Gloop Wiki', 'meta', page);
  } else {
    // make this work when "Show preview without reloading the page" is checked
    mw.hook('wikipage.content').add(function ($content) {
      if (!$content.has('.rs-external-header-links').length) return;

      // clear existing externals first so previewing doesn't add an infinite amount
      $('.gadget-external-icon').remove();

      // convert whatever [[Template:External]] gives us
      $('.rs-header-icon').each(function (index, icon) {
        addLink(icon.dataset.wikiname, icon.dataset.interwiki, icon.dataset.page);
      });

      // clean up
      $('.rs-external-header-links').remove();
    });
  }

  function addLink(wikiname, interwiki, page) {
    var url = mw.util.getUrl(interwiki + ':' + page);
    var title = wikiname + ' also has an article on ' + page + '.';

    // wikipedia links aren't forwarded - see [[Special:Interwiki]]
    if (interwiki === 'wikipedia') {
      url = '//en.wikipedia.org/wiki/' + encodeURIComponent(page);
    }

    // Special and MediaWiki namespaces
    if (namespace === -1 || namespace === 8) {
      url += window.location.search; // include url parameters
      title = 'Open this page on ' + wikiname + '.';
    }
    var link = mw.util.addPortletLink('p-namespaces', url, '',
    // no text - use background-image instead
    'gadget-external-' + interwiki,
    // button id
    title // title text shown on hover
    );

    // for styling all links at once - addPortletLink only sets an id
    $(link).addClass('gadget-external-icon');
  }
})(jQuery, mediaWiki);