User:KockaAdmiralac/global.js

/** * Loader for my global JavaScript. */ window.KockaJS = { init: function { var js = localStorage.getItem('KockaJS-core'); $(document.body).keydown($.proxy(this.keydown, this)); var isDebug = mw.config.get('debug') || window.location.search.indexOf('clearkockajs') !== -1; if (js && !isDebug) { this.exec(js); } else { this.fetch; }   },    exec: function(js) { var script = document.createElement('script'); script.textContent = js; script.type = 'text/javascript'; document.body.append(script); },   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.'); }   } }; $(window.KockaJS.init.bind(window.KockaJS));