Jump to content

User:NKohli (WMF)/nightpedia2.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
// This script was originally created by User:MusikAnimal and lives at https://en.wikipedia.org/wiki/User:MusikAnimal/nightpedia.js. 
// This copy is only for testing purposes and not for wide usage. Please use the original script for your personal use. 

$(function() {
    var nighttime = 'on' === mw.storage.get('nightpedia'),
        cssLoaded = nighttime ? lightsOff() : false;

    mw.util.addPortletLink('p-personal', '#', nighttime ? 'Daypedia' : 'Nightpedia', 'pt-nightpedia', '', 'np', '#pt-watchlist');

    $('#pt-nightpedia').on('click', function(e) {
        e.preventDefault();
        nighttime = !nighttime;
        if (nighttime) {
            lightsOff();
        } else {
            lightsOn();
        }
    });

    function lightsOn() {
        $('html').removeClass('nighttime');
        $('#pt-nightpedia a').text('Nightpedia');
        mw.storage.set('nightpedia', 'off');
    }
    function lightsOff() {
        if(!cssLoaded) {
            $('head').append(mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:MusikAnimal/nightpedia.css&action=raw&ctype=text/css', 'text/css'));
        }
        $('html').addClass('nighttime');
        $('#pt-nightpedia a').text('Daypedia');
        mw.storage.set('nightpedia', 'on');
        return true;
    }
});
//</nowiki>