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 04:57, 29 February 2016 (Create.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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($) {
	if(wgNamespaceNumber !== 0) {
		return;
	}
	var portletlink = mw.util.addPortletLink('p-tb', '#', 'Fix line colours');
	$(portletlink).click( function(e) {
		e.preventDefault();
		var loc = window.location.href; var wpTextbox1 = document.getElementById('wpTextbox1'); var i;
		if (loc.indexOf('colour_contrast') > 0) {
			var bc = document.getElementById('bc');
			var fc = document.getElementById('fc');
			var contrastratio = document.getElementById('contrastratio');
			var bsSliderInput = document.getElementById('bs-slider-input');
			var bvSliderInput = document.getElementById('bv-slider-input');

			fc.value = 'FFFFFF'; fc.onchange();
			var __white = contrastratio.value;
			fc.value = '000000'; fc.onchange();
			var __black = contrastratio.value;

			if (__white > __black) { fc.value = 'FFFFFF'; fc.onchange(); }
			if (__black > __white) { fc.value = '000000'; fc.onchange(); }
			
			var counter = 0; var giveprompt = true;
			while (contrastratio.value <= 7) {
				if (counter > 100) {
					alert('Infinite loop - manual adjustment required!');
					giveprompt = false;
					break;
				}
				if (bvSliderInput.value < 100 && bvSliderInput.value > 0) {
					bvSliderInput.value = parseInt(bvSliderInput.value)+(fc.value == 'FFFFFF' ? -1 : 1);
					bvSliderInput.onchange();
				} else {
					bsSliderInput.value = parseInt(bsSliderInput.value)+(fc.value == 'FFFFFF' ? 1 : -1);
					bsSliderInput.onchange();
				}
				counter++;
			}

			if (giveprompt) {
				var contrastingColour = bc.value;
				if (window.prompt("Copy to clipboard: Ctrl+C, Enter", contrastingColour)) window.close();
			}
		} else if (loc.indexOf('Category:') >= 0) {
			for (i = 3; i < 13; i++) {
				window.open("https://en.wikipedia.org/wiki/"+document.getElementsByClassName('mw-content-ltr')[0].getElementsByTagName('li')[i].children[0].title);
			}
		} else if (loc.indexOf('action=edit') < 0) {
			window.location = window.location+"?action=edit";
		} else {
			var colours = [];
			var s = wpTextbox1.value.split("\n");
			for (i = 0; i < s.length; i++) {
				if (s[i].indexOf('LineColor') > 0) {
					var reg = /\|([\s]*)[LineColor]*([\s=]*)\#?([0-9A-Fa-f]{3,6})/g;
					var t = reg.exec(s[i]);
					if (!t) continue;
					var fontColor = t[3];
					if (colours.indexOf(fontColor) >= 0) continue;
					colours[colours.length] = fontColor;
				}
			}
			
			window.open("http://snook.ca/technical/colour_contrast/colour.html#fg=FFFFFF,bg="+colours[0]);
			
			document.addEventListener("keypress", function(e) {
				if (e.keyCode == 13) {
					var thiscolour = colours.shift();
					
					var newcolour = prompt('New colour for '+thiscolour+'?');
					if (newcolour) {
						/* #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 = '';
						
						var reg2 = new RegExp('\\b'+_3code+thiscolour+'\\b','gi');
						wpTextbox1.value = wpTextbox1.value.replace(reg2,newcolour);
					}
					
					if (colours.length === 0) {
						document.getElementById('wpSummary').value =
							"Changed colors per [[WP:COLOR]] and [[Template talk:Infobox television season#Colour]].";
						document.getElementById('wpSave').click();
					} else {
						window.open("http://snook.ca/technical/colour_contrast/colour.html#fg=FFFFFF,bg="+colours[0]);
					}
				}
			});
		}
	});
});