User:BryghtShadow/common.js

From Omega Strikers Wiki
Jump to navigation Jump to search

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.
// Preload "Striker/Quotes" pages when audio has been uploaded.
$(function () {
	if (window.preloadStrikerQuotes) return;

	if (!mw.config.get('wgPageName').endsWith('/Quotes')) return;
	if (!/(edit|submit)/.test(mw.config.get('wgAction'))) return;

	const STRIKER = mw.config.get('wgPageName').split('/')[0];
	const PATTERN = new RegExp("^File:(" + STRIKER + ")(\\D+)(\\d+)\\.mp3$");

	var sections = [
		{ key: "Select", heading: "Selected as a Striker", level: 2 },
		{ key: "Intro", heading: "Starting a Match", level: 2 },
		{ heading: "With Teammate", level: 3, text: '* With <!-- {{striker|Here}} -->\n** Audio Button + Quote Here' },
		{ heading: "Against Opponent", level: 3, text: '* Against<!-- {{striker|Here}} -->\n** Audio Button + Quote Here' },
		{ key: "Goal", heading: "Scoring a Goal", level: 2 },
		{ key: "MVP", heading: "Chosen as MVP", level: 2 },
		{ key: "KnockedOut", heading: "Knocked Out", level: 2 },
		{ key: "Respawn", heading: "Respawning", level: 2 },
		{ heading: "Emotes", level: 2, text: false },
		{ key: "EmotePositive", heading: "Delighted", level: 3 },
		{ key: "EmoteNegative", heading: "Disappointed", level: 3 },
		{ key: "CastPrimary", heading: "Casting Primary Ability", level: 2 },
		{ key: "CastSecondary", heading: "Casting Secondary Ability", level: 2 },
		{ key: "CastSpecial", heading: "Casting Special Ability", level: 2 },
	];

	function parseAllImages(data) {
		if (!data.query) {
			console.error("Something went wrong:", data.query);
			return;
		}
		var files = {};
		for (var i in data.query.allimages) {
			var title = data.query.allimages[i].canonicaltitle;
			var m = title.match(PATTERN);
			if (!m) continue;
			var striker = m[1],
				prefix = m[2],
				suffix = parseInt(m[3]);
			files[prefix] = files[prefix] || {};
			files[prefix][suffix] = title;
		}
		var output = [];
		for (var s in sections) {
			var section = sections[s];
			var h0 = '='.repeat(section.level);
			var heading = h0 + section.heading + h0;
			if (section.level === 2) {
				output.push('');
			}
			output.push(heading);
			if (section.text) {
				output.push(section.text);
			} else if (section.key) {
				var filenames = files[section.key];
				for (var f in filenames) {
					var li = '*<div class="audio-button">[[' + filenames[f] + ']]</div> ""';
					output.push(li);
				}
			}
		}
		output.push('[[Category:Quotes]]');
		var preload = output.join('\n').trim();
		$('#wpTextbox1').val(preload);
	}

	function runQuery() {
		return new mw.Api().get({
			action: 'query',
			list: 'allimages',
			aiprefix: 'Rune',
			aisort: 'name',
			ailimit: 'max',
			aiprop: 'canonicaltitle',
			formatversion: 2,
		});
	}

	window.preloadStrikerQuotes = function () {
		runQuery().then(parseAllImages);
	};
});