Community Central
Community Central

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.
/**
 * Loader for my global JavaScript.
 */
var KockaJS = {
    init: function() {
        var js = localStorage.getItem('KockaJS-core');
        $(document.body).keydown($.proxy(this.keydown, this));
        if (js && !mw.config.get('debug')) {
            this.exec(js);
        } else {
            this.fetch();
        }
    },
    exec: function(js) {
        $('<script>', {
            text: js,
            type: 'text/javascript'
        }).appendTo(document.body);
    },
    fetch: function() {
        $.ajax({
            data: {
                action: 'query',
                cb: Date.now(),
                format: 'json',
                indexpageids: true,
                prop: 'revisions',
                rvprop: 'content',
                titles: 'User:KockaAdmiralac/common.js'
            },
            dataType: 'jsonp',
            error: $.proxy(this.ajaxError, this),
            success: $.proxy(this.fetched, this),
            type: 'GET',
            url: 'https://kocka.fandom.com/api.php'
        });
    },
    fetched: function(d) {
        if (d.error) {
            console.error(
                'MediaWiki API threw an error while fetching JavaScript:',
                d.error
            );
            return;
        }
        var id = Number(d.query.pageids[0]);
        if (id < 0) {
            console.error('Requested JavaScript page does not exist!');
            return;
        }
        var js = d.query.pages[id].revisions[0]['*'];
        localStorage.setItem('KockaJS-core', js);
        this.exec(js);
    },
    ajaxError: function(error) {
        console.error('AJAX error while fetching JavaScript:', error);
    },
    keydown: function(event) {
        if (
            !this.clearing &&
            (event.ctrlKey && event.shiftKey && event.key === 'R') ||
            (event.ctrlKey && event.key === 'F5')
        ) {
            this.clearing = true;
            console.info('Clearing KockaJS cache.');
            for (var key in localStorage) {
                if (key.indexOf('KockaJS-') === 0) {
                    localStorage.removeItem(key);
                }
            }
            console.info('Clearing done.');
        }
    }
};

KockaJS.init();