Jump to content

User:Alex 21/script-linecolour.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alex 21 (talk | contribs) at 08:44, 22 December 2016 (Comments throughout code). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
$(function($) {
	var portletlink = mw.util.addPortletLink('p-tv', '#', 'Line colours');
	$(portletlink).click( function(e) {
		e.preventDefault();
		// Default parameters and determine if we need to go to the editing page
		var loc = window.location.href; var wpTextbox1 = document.getElementById('wpTextbox1'); var i;
		if (loc.indexOf('action=edit') < 0 && loc.indexOf('action=submit') < 0) {
			alert("Go to the edit page to use this script.");
		} else {
			// Gather colours from text of textarea
			var colours = [];
			var s = wpTextbox1.value.split("\n");
			for (i = 0; i < s.length; i++) {
				if (s[i].indexOf('LineColor') > 0) {
					// LineColor regex
					var reg = /\|\s*LineColor[\s\=\#]*([^\s]*)/g;
					var t = reg.exec(s[i]);
					if (!t) continue;
					// Add colour to saved colours if its not already added
					var fontColor = t[1];
					if (colours.indexOf(fontColor) >= 0) continue;
					colours[colours.length] = fontColor;
				}
			}
			
			for (i = 0; i < colours.length; i++) {
				// Gather colour, and individually adjust to AAA compliancy
				var thiscolour = colours[i];
				var newcolour = colourCompliance(thiscolour,false);
				
				// Update if the original colour has been changed to be compliant
				if (thiscolour != newcolour) {
					// 3-digits codes (e.g. #FF3344 = #F34)
					var _3code;
					if (thiscolour[0] == thiscolour[1] && thiscolour[2] == thiscolour[3] && thiscolour[4] == thiscolour[5])
						_3code = thiscolour[0]+thiscolour[2]+thiscolour[4]+'|';
					else _3code = '';
					
					// Replace colours
					var reg2 = new RegExp('\\b'+_3code+thiscolour+'\\b','gi');
					wpTextbox1.value = wpTextbox1.value.replace(reg2,newcolour);
				}
			}
			
			// Done
			document.getElementById('wpSummary').value += "Adjusted color contrasts via [[User:AlexTheWhovian/script-linecolour|script]] per [[WP:COLOR]] and [[Template talk:Infobox television season/Archive 3]].";
		}
	});
});