Jump to content

User:Alpha3031/testing/voteSymbols.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.
// Fork of [[User:Pythoncoder/Scripts/voteSymbols.js]] for me to play with

// Revised from [[User:Ais523/votenames.js]]
// Code for adding icons to !votes in AfDs, RfAs, etc...
// Doesn't affect the page, just shows the icons when this code is installed.
// Please see [[:Image:Votenames.js-enhanced_SfD.png]] and
// [[c:Category:Norro style 1 icons]] for copyright info on the images that are added.

$(function() {
	let ns = mw.config.get('wgNamespaceNumber');
	if (!(ns%2)&&ns!==2&&ns!==4||ns===-1) {
		return; // Avoid all even nses (content) except WP and User
	}
	b = (id, filename, width = 60) => "//upload.wikimedia.org/wikipedia/commons/thumb/"
                                      + id + filename + "/" + width + "px-" + filename + ".png";
	const vs = {
		support: b("9/94/", "Symbol_support_vote.svg"),
		oppose: b("7/7f/", "Symbol_oppose_vote.svg"),
		no: b("7/7f/", "Symbol_oppose_vote.svg"),
		keep: b("d/d0/", "Symbol_keep_vote.svg"),
		delete: b("8/89/", "Symbol_delete_vote.svg"),
		speedy: b("6/6d/", "Symbol_speedy_delete_vote.svg"),
		neutral: b("8/89/", "Symbol_neutral_vote.svg"),
		merge: b("b/b0/", "Symbol_merge_vote.svg"),
		move: b("5/50/", "Symbol_move_vote.svg"),
		redirect: b("0/0c/", "Symbol_redirect_vote.svg"),
		orangexc: b("b/ba/", "Symbol_opinion_vote.svg"),
		note: b("e/e0/", "Symbol_comment_vote.svg"),
		unsupport: b("f/f6/", "Symbol_unsupport_vote.svg"),
		query: b("e/e0/", "Symbol_question.svg"),
		dab: b("2/2a/", "Symbol_dab_class.svg"),
		wait: b("5/54/", "Symbol_wait.svg"),
		rename: b("0/0a/", "Symbol_rename_vote.svg"),
		undelete: b("c/c5/", "Symbol_support2_vote.svg"),
		close: b("3/3b/", "Symbol_no_support_vote.svg"),
		split: b("0/0d/", "Symbol_split_vote.svg"),
		abstain: b("6/61/", "Symbol_abstain_vote.svg"),
		draft: b("0/09/", "Symbol_draft_class.svg"),
		user: b("1/13/", "Symbol_user_class.svg"),
		list: b("d/db/", "Symbol_list_class.svg"),
		reply: b("d/d2/", "Symbol_reply.svg"),
		salt: b("6/66/", "Symbol_create_protect_vote.svg"),
		withdraw: b("9/90/", "Cancelled_process_mini.svg", 90),
		info: b("8/8c/", "Symbol_information_vote.svg"),
		update: b("1/18/", "Symbol_version_future.svg"),
		snow: b("d/d3/", "Bouncing_Snowball.png"),
		blank: b("1/1b/", "Symbol_plain_white.svg"),
		ongoing: b("e/e3/", "Symbol_wait_blue.svg")
	};
	const prefixed = {
		keep: vs,
		speedy: {
			delete: b("6/6d/", "Symbol_speedy_delete_vote.svg"),
			keep: b("b/bc/", "Symbol_speedy_keep_vote.svg"),
			redirect: b("2/22/", "Symbol_speedy_redirect_vote.svg"),
		},
		strong: {
			support: b("8/84/", "Symbol_strong_support_vote.svg"),
			oppose: b("5/5e/", "Symbol_oppose_vote_oversat.svg")
		},
		weak: {
			support: b("8/8c/", "GA_candidate.svg"),
			oppose: b("f/f5/", "BA_candidate.svg")
		},
		partial: {
			support: b("b/b2/", "Symbol_conditional_support.svg")
		},
		no: {
			consensus: b('6/6a/', 'Symbol_na_class.svg')
		}
	};
	let unalias={};
	function a() {
		for (let i=0; i < arguments.length; i++) unalias[arguments[i]] = arguments[0];
	}
	a('no', "don't", 'not', 'without');
	a('support', 'accept', 'favor', 'endorse');
	a('oppose', 'object', 'overturn', 'disallow', 'decline');
	a('keep', 'allow', 'permit'); a('delete', 'delet', 'remove', 'pull');
	a('undelete', 'restore'); a('snow', 'snowball');
	a('neutral'); a('merge', 'upmerge');
	a('move', 'transwiki', 'convert');
	a('redirect', 'retarget', 'repost', 'redir');
	a('orangexc', 'relist', 'subst', 'change', 'recuse');
	// Note that the orange exclamation mark icon is often used for debate-specific !votes
	a('note', 'comment', 'comments'); a('split');
	a('delist', 'demote', 'unsupport');
	a('question', 'query', 'query'); a('user');
	a('dab', 'disambiguate', 'sia'); a('list');
	a('wait', 'hold'); a('rename'); a('close');
	a('abstain', 'unnecessary'); a('reply');
	a('draft', 'incubate'); a('salt');
	a('withdraw', 'withdrawn'); a('consensus');
	a('info', 'information'); a('ongoing');
	a('update', 'answer'); a('blank');

	// prefixes also need to be aliased
	a('speedy'); a('strong'); a('weak');
	a('partial', 'conditional');

	let bstrings=document.body.getElementsByTagName("b");
	let un = (s) =>
		unalias[s?s.replace(/(ed|i?fy|io?ng?)$/g, ""):''];
	let isCompound = (cur, next) =>
		prefixed.hasOwnProperty(cur) && next && prefixed[cur].hasOwnProperty(next);
	let tagify = (src) =>
		'<img src="' + src + '" style="height:1em" /> ';
	for (let string of bstrings) {
		if (string.previousElementSibling &&
			string.previousElementSibling.tagName.toLowerCase() === 'img') continue;
		const words = string.textContent.toLowerCase().replace(/[^a-z']/g, " ").split(" ");
		if (words.length > 8) continue;
		let x="";
		for (let i=0; i<words.length; i++) {
		const name = un(words[i]);
		if (isCompound(name, un(words[i+1]))) {
			while(isCompound(un(words[++i]), un(words[i+1]))) {
				// Skip to last possible compound
			}
			x += tagify(prefixed[un(words[i-1])][un(words[i])]);
			} else if (vs.hasOwnProperty(name)) {
			x += tagify(vs[name]);
			}
		}
		if (x!=="") string.innerHTML=x+string.innerHTML;
	}
});
// [[Category:Wikipedia scripts]]