Welcome to Wargaming.net Wiki!
Variants

MediaWiki:Common.js

Revision as of 21:08, 27 May 2026 by Jack (talk | contribs) (Fix light mode: make .b-game-info transparent so content blends with page background texture)
Jump to: navigation, 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.
/* Any JavaScript here will be loaded for all users on every page load. */

/* ============================================================
 Light / Dark Mode Toggle
 Adds a fixed pill (top-right) that switches between the
 current dark theme and the live-wiki light theme.
 Styled to match the Wargaming sign-in page mode-switcher.
 Preference is remembered via localStorage.
============================================================ */
( function () {
'use strict';

var STORAGE_KEY = 'wows-theme-mode';
var STYLE_ID    = 'wows-light-mode-styles';

var LIGHT_CSS = [
  'html, body, .b-main {',
  '  background-color: transparent !important;',
  '  background-image: url("https://wiki.wargaming.net/skins/globalwiki/images/bg.jpg?2026-01-22T09:18:20Z") !important;',
  '  background-repeat: repeat !important;',
  '  color: rgb(0, 0, 0) !important;',
  '}',
  '#firstHeading { color: rgb(56, 56, 56) !important; }',
  '.b-game-info > b, .b-game-info .b-heading2 { color: rgb(56, 56, 56) !important; }',
  '.mw-parser-output, .mw-body-content, #mw-content-text { color: rgb(0, 0, 0) !important; }',
  '#mw-panel, #mw-panel a, .b-sidebar, .b-sidebar a, .b-left-menu_item, .b-portlet-link { color: rgb(67, 99, 115) !important; }',
  '#mw-head, #mw-head a, .b-header a { color: rgb(56, 56, 56) !important; }',
  '.b-tabs-wrp { border-color: initial !important; }',
  'table, .wikitable, .b-infobox { background-color: rgba(255,255,255,0.8) !important; color: rgb(0,0,0) !important; }',
  'th { background-color: rgba(200,220,235,0.9) !important; color: rgb(0,0,0) !important; }',
  'td { color: rgb(0,0,0) !important; }',
  '.mw-body, #content { background-color: transparent !important; }',
  '.b-sidebar_title, #footer, .b-footer { color: rgb(56, 56, 56) !important; }',
  '.b-heading1, .b-heading2, .b-heading3, h1, h2, h3, h4 { color: rgb(56, 56, 56) !important; }',
  '.b-search-input, #searchInput { background-color: rgb(245,245,245) !important; color: rgb(0,0,0) !important; }',
  '.b-game-info { background-color: transparent !important; color: rgb(0,0,0) !important; border-color: transparent !important; }',
  '.b-description.gw-frame-1, .mw-parser-output .gw-frame-1 { background-color: rgba(255,255,255,0.33) !important; border-color: rgba(0,0,0,0.12) !important; box-shadow: none !important; color: rgb(0,0,0) !important; }',
  '.b-description.gw-frame-1 a, .mw-parser-output .gw-frame-1 a { color: rgb(2,93,177) !important; }'
].join( '\n' );

function applyLight() {
  if ( document.getElementById( STYLE_ID ) ) { return; }
  var s = document.createElement( 'style' );
  s.id          = STYLE_ID;
  s.textContent = LIGHT_CSS;
  document.head.appendChild( s );
}

function applyDark() {
  var s = document.getElementById( STYLE_ID );
  if ( s ) { s.remove(); }
}

var DAY_SVG   = 'https://wargaming.net/id/static/2026.4.0/wgnet/img/mode-switcher/day.svg';
var NIGHT_SVG = 'https://wargaming.net/id/static/2026.4.0/wgnet/img/mode-switcher/night.svg';

function buildButton() {
  var isDark = ( localStorage.getItem( STORAGE_KEY ) !== 'light' );

  if ( !isDark ) { applyLight(); }

  var wrap = document.createElement( 'div' );
  wrap.id = 'wows-theme-toggle';
  wrap.style.cssText = [
    'position:fixed', 'top:14px', 'right:16px', 'z-index:9999',
    'background:rgb(30,30,30)', 'border:1px solid rgb(52,52,52)',
    'border-radius:8px', 'padding:3px', 'display:flex', 'gap:4px',
    'align-items:center'
  ].join( ';' );

  var BTN_BASE = [
    'width:30px', 'height:24px', 'border:none', 'cursor:pointer', 'padding:0',
    'border-radius:5px', 'background-color:transparent',
    'background-repeat:no-repeat', 'background-position:center',
    'background-size:20px', 'transition:opacity 0.2s'
  ].join( ';' );

  var btnLight = document.createElement( 'button' );
  btnLight.title = 'Light mode';
  btnLight.style.cssText = BTN_BASE + ';background-image:url("' + DAY_SVG + '")';
  btnLight.style.opacity = isDark ? '0.5' : '1';

  var btnDark = document.createElement( 'button' );
  btnDark.title = 'Dark mode';
  btnDark.style.cssText = BTN_BASE + ';background-image:url("' + NIGHT_SVG + '")';
  btnDark.style.opacity = isDark ? '1' : '0.5';

  function updateOpacity() {
    btnLight.style.opacity = isDark ? '0.5' : '1';
    btnDark.style.opacity  = isDark ? '1'   : '0.5';
  }

  btnLight.addEventListener( 'click', function () {
    if ( !isDark ) { return; }
    isDark = false; applyLight();
    localStorage.setItem( STORAGE_KEY, 'light' ); updateOpacity();
  } );

  btnDark.addEventListener( 'click', function () {
    if ( isDark ) { return; }
    isDark = true; applyDark();
    localStorage.setItem( STORAGE_KEY, 'dark' ); updateOpacity();
  } );

  wrap.appendChild( btnLight );
  wrap.appendChild( btnDark );
  document.body.appendChild( wrap );
}

if ( document.readyState === 'loading' ) {
  document.addEventListener( 'DOMContentLoaded', buildButton );
} else {
  buildButton();
}

}() );