User:C.Ezra.M/addPrefixesLink.js

From Bulbapedia, the community-driven Pokémon encyclopedia.
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.
/**
 * A script that adds a "List of subpages" link to the sidebar.
 *
 * You can define these variables:
 * - prefixesLink.nsSpecialName - localized name for the Special namespace
 * - prefixesLink.specialPageName - localized name for "PrefixIndex"
 * - prefixesLink.title = the hover title
 * - prefixesLink.display = link text
 * 
 * @name addPrefixesLink
 * @author Keyacom
 * @summary A script that adds a "List of subpages" link to the sidebar.
 */

if (mw.config.get('wgNamespaceNumber') != -1 && mw.config.get('skin') != "minerva") { // Exclude special pages or the Minerva skin
    if (typeof window.prefixesLink === 'undefined') {
        window.prefixesLink = {};
    }
    const { config: cfg } = mw;
    const list = document.querySelector("#p-tb ul");
    const li = document.createElement("li");
    const link = document.createElement("a");

    let href = `${cfg.get('wgArticlePath')}/${cfg.get('wgPageName')}`;
    if (window.prefixesLink?.nsSpecialName === undefined) {
        window.prefixesLink.nsSpecialName = "Special";
    }
    href = href.replace("$1", window.prefixesLink.nsSpecialName.trim()   ":$1");
    if (window.prefixesLink?.specialPageName === undefined) {
        window.prefixesLink.specialPageName = "PrefixIndex";
    }
    href = href.replace("$1", window.prefixesLink.specialPageName.trim());
    // Assuming leaving defaults (and wgArticlePath with "/wiki/$1"), the href should be "/wiki/Special:PrefixIndex/<pagename>"
    link.href = href;
    if (window.prefixesLink?.title === undefined) {
        window.prefixesLink.title = "See subpages of this page";
    }
    link.title = window.prefixesLink.title.trim();
    if (window.prefixesLink?.display === undefined) {
        window.prefixesLink.display = "List of subpages";
    }
    link.innerHTML = window.prefixesLink.display.trim();
    li.appendChild(link);
    list.appendChild(li);
}