Jump to content

MediaWiki:Common.js

From OnTrackWorld
Revision as of 19:38, 14 September 2026 by CNeiss (talk | contribs) (Pin sidebar open by default for all visitors)

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.
/* OPTIONAL. Append to MediaWiki:Common.js only if you want inline highlight-and-annotate
   on long-form pages (white paper, book chapters). Loads the Hypothesis client on pages
   tagged with the "Annotate" category. Readers need a free Hypothesis account to annotate;
   anyone can read annotations. Remove this block to turn it off. */
( function () {
  if ( mw.config.get( 'wgCategories' ).indexOf( 'Annotate' ) === -1 ) { return; }
  if ( mw.config.get( 'wgAction' ) !== 'view' ) { return; }
  var s = document.createElement( 'script' );
  s.src = 'https://hypothes.is/embed.js';
  s.async = true;
  document.head.appendChild( s );
}() );

/* ---- Pin the sidebar (Main menu) open by default for every visitor -----------------
   Vector 2022 only shows Main menu as a permanent left column once a user pins it
   (client preference, saved per-account/browser). This triggers that same built-in
   pin action automatically on every page load, so it defaults to open for everyone
   instead of only whoever has pinned it themselves. Skipped on narrow/mobile
   viewports, where a permanently-open sidebar would crowd out the page content. */
( function () {
  function log( msg ) { if ( window.console && window.console.log ) { console.log( '[otw-pin] ' + msg ); } }
  function pinMainMenu() {
    log( 'pinMainMenu called, readyState=' + document.readyState + ' width=' + window.innerWidth );
    if ( window.innerWidth < 1000 ) { log( 'bail: narrow' ); return; }
    if ( document.documentElement.classList.contains( 'vector-feature-main-menu-pinned-enabled' ) ) { log( 'bail: already pinned' ); return; }
    var pinnedContainer = document.getElementById( 'vector-main-menu-pinned-container' );
    var realMenu = document.getElementById( 'vector-main-menu' );
    log( 'pinnedContainer=' + ( !!pinnedContainer ) + ' realMenu=' + ( !!realMenu ) +
      ' pinnedChildren=' + ( pinnedContainer ? pinnedContainer.children.length : '-' ) );
    if ( pinnedContainer && realMenu && pinnedContainer.children.length === 0 ) {
      pinnedContainer.appendChild( realMenu );
      document.documentElement.classList.remove( 'vector-feature-main-menu-pinned-disabled' );
      document.documentElement.classList.add( 'vector-feature-main-menu-pinned-enabled' );
      log( 'done, pinnedChildren now=' + pinnedContainer.children.length );
    }
  }
  log( 'script loaded, readyState=' + document.readyState );
  if ( document.readyState === 'loading' ) {
    document.addEventListener( 'DOMContentLoaded', pinMainMenu );
  } else {
    pinMainMenu();
  }
}() );