User:Draugabjörn/common.js

Note: After saving, 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: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.

// Load dependencies for WikimediaUI Dark Mode (remove when loading through ResourceLoader).
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Volker_E._(WMF)/dark-mode.css&action=raw&ctype=text/css', 'text/css');

// User has dark color scheme preference set in operating system.
if (window.matchMedia('( prefers-color-scheme: dark )').matches || mw.storage.get('client-dark-mode-toggle') === '1') {
    document.documentElement.className = 'client-dark-mode';
};
// Test fix for tables on mobile
//if(window.devicePixelRatio > 2){
//    var m = document.createElement("meta");
//    m.name = "viewport";
//    m.content = "width=device-width,initial-scale=1";
//    document.head.appendChild(m);
//
//    var table = document.querySelectorAll("table.wikitable.nearby-systems");
//    for(var i = 0; i < table.length; i = i+1){
//        var w = document.createElement("div");
//        w.class = "table_wrapper";
//        w.style = "overflow-x:scroll";
//        table[i].parentNode.replaceChild(w, table[i]);
//        w.appendChild(table[i]);
//   };
//};

// Fix logos and add dark mode toggle
$(function () {
    $('#p-logo').clone().addClass('mw-no-invert').insertAfter('#p-logo');

    mw.util.addPortletLink('p-personal', '#', 'Dark mode', 'pt-darkmode', '', 'np', '#pt-watchlist');

    $('#pt-darkmode').on('click', function (e) {
        var $html = $('html');
        e.preventDefault();

        $html.toggleClass('client-dark-mode');
        mw.storage.set(
            'client-dark-mode-toggle',
            String(Number($html.hasClass('client-dark-mode'))));
    });

});