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 JS
// Version 2.0.0

window.dev = window.dev || {};
window.caburum = {
	shouldRunLocal: !mw.util.getParamValue('uselocaljs') && !mw.util.getParamValue('usecabjs'), // return true if the local script should run
	isUserAccount: ['Caburum', 'Mini Caburum'].includes(mw.config.get('wgUserName')),
	isBotAccount: ['CaburumBot', 'Among Us Wiki Bot'].includes(mw.config.get('wgUserName')),
	init: function() {
		if (!(window.caburum.isUserAccount || window.caburum.isBotAccount)) return console.error('You\'re not Cab!'); // Prevent users other than me from easily loading this

		var shouldRunGlobal = !mw.util.getParamValue('useglobaljs') && !mw.util.getParamValue('usecabjs'); // return true if core JS should run

		if (!shouldRunGlobal) return this.log('Did not execute global.js from Community Central, disabled');

		var coreJS = localStorage.getItem('Caburum-coreJS');
		var isDebug = mw.config.get('debug') || mw.util.getParamValue('clearcabjs'); // return true if coreJS should be refreshed
		$(document.body).keydown($.proxy(this.clearJSCacheKeydown, this));

		if (coreJS && !isDebug) {
			this.execScript(coreJS);
		} else {
			this.updateCoreJS();
		}

		if (isDebug) {
			this.clearJSCache();
			this.updateCoreJS();
		}

		$('<li>', {
			append: $('<a>', {
				text: 'Clear Cab JS',
				href: new mw.Uri().extend({
						clearcabjs: 1
					}).toString(),
			}),
			prependTo: $('#WikiaBar .toolbar .mytools .tools-menu')
		});

		this.log('Executed global.js from Community Central');
	},
	log: function() { // Formatted console.log
		console.log.apply(null, Array.prototype.concat('%c[CABURUM]', 'color: #00D6D6; background-color: #202124;', Array.prototype.slice.call(arguments)));
	},
	execScript: function(script) { // Add code to a <script> tag to execute it
		var element = document.createElement('script');
		element.textContent = script;
		element.type = 'text/javascript';
		document.body.append(element);
	},
	clearJSCache: function() {
		this.log('Clearing Caburum cache');
		for (var key in localStorage) {
			if (key.indexOf('Caburum-') === 0) {
				localStorage.removeItem(key);
			}
		}
		this.log('Clearing done');
	},
	updateCoreJS: function() { // Update coreJS from [[w:c:caburum:User:Caburum/global.js]]
		$.ajax({
			data: {
				action: 'query',
				cb: Date.now(),
				format: 'json',
				prop: 'revisions',
				rvprop: 'content',
				rvslots: 'main',
				titles: 'User:Caburum/core.js'
			},
			dataType: 'jsonp',
			type: 'GET',
			url: 'https://caburum.fandom.com/api.php'
		})
		.done(function(d) {
			if (d.error) return console.error('MediaWiki API threw an error while fetching JavaScript:', d.error);
			if (d.query.pages[-1]) return console.error('Requested JavaScript page does not exist.');
			var js = Object.values(d.query.pages)[0].revisions[0].slots.main['*'];
			localStorage.setItem('Caburum-coreJS', js);
			window.caburum.execScript(js);
		})
		.fail(function(e) {
			console.error('AJAX error while fetching JavaScript:', e);
		});
	},
	clearJSCacheKeydown: function(event) {
		if (
			!this.clearingJSCache &&
			(event.ctrlKey && event.shiftKey && event.key === 'R') ||
			(event.ctrlKey && event.key === 'F5')
		) {
			this.clearingJSCache = true;
			this.clearJSCache();
		}
	},
};

mw.loader.using('mediawiki.util', function() {
	if (mw.config.get('isGamepedia')) {
		mw.loader.getScript('https://dev.fandom.com/wiki/MediaWiki:ArticlesAsResources.js?action=raw&ctype=text%2Fjavascript').then(function() {
			$(window.caburum.init.bind(window.caburum));
		});
	} else {
		$(window.caburum.init.bind(window.caburum));
	}
});