Jump to content

User:Erutuon/scripts/imageSize.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Erutuon (talk | contribs) at 20:33, 22 September 2017 (defining function only if needed; better scope for variables). 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.
/*
	Automatically converts image sizes in pixels to scaling values (upright=), as prescribed by [[WP:IMGSIZE]].
	Adds an edit summary only if a change was made.
	Posts the number of replacements in the console log.
*/

const contentModel = mw.config.values.wgPageContentModel;

if ( contentModel === "wikitext" )
{
	let convertToUpright = function (wikitext)
	{
		const oldWikitext = wikitext;
		
		var count = 0;
		
		// The following regular expression assumes that a given line of wikitext
		// will contain images and nothing else.
		wikitext = wikitext.replace(
			/\[\[(?:[Ff]ile|[Ii]mage).+/g,
			function(wholematch)
			{
				return wholematch.replace(
					/\|\s*(\d+)px\s*([\|\]])/g,
					function(match, number, afterNumber)
					{
						// Convert string to number.
						number = Number(number);
						// Convert to upright value.
						number = number / 220;
						// Round to nearest hundredth.
						number = Math.floor( number * 100 ) / 100;
						
						count += 1;
						
						return "|upright=" + number + afterNumber;
					}
				);
			}
		);
		
		if ( oldWikitext === wikitext )
			console.log("imageSize.js made no changes.");
		else if ( count > 0 )
		{
			var agreement = "";
			if ( count > 1 )
				agreement = "s";
			
			var message = "imageSize.js made " + count + " replacement" + agreement + ".";
			
			if ( window.imageSizeNotifications )
				mw.notify(message);
			console.log(message);
		}
		
		$("#wpTextbox1").val(wikitext);
				
		$("#wpSummary").val(function(index, summary)
			{
				var addition = "converted image sizes in pixels to scaling values, as prescribed by [[WP:IMGSIZE]], using [[User:Erutuon/scripts/imageSize.js|JavaScript]]";
				
				var afterSectionName = summary.match(/^(?:\/\*[^\*]+\*\/)?\s*(.+)/);
				
				if ( afterSectionName && afterSectionName[1].length > 1 )
					addition = "; " + addition;
				
				if ( count > 0 && ( !afterSectionName || !afterSectionName[1].includes(addition) ) )
					return summary + addition;
				else
					return summary;
			}
		);
	};
	
	var wikitext = $("#wpTextbox1").val();
	
	if ( /\[\[(?:[Ff]ile|[Ii]mage)[^\]]+upright/.test(wikitext) )
	{
		mw.loader.load("//en.wiktionary.org/w/index.php?title=User:Erutuon/styles/wikitext-cleanup.css&action=raw&ctype=text/css", "text/css");
		
		if ( !$("#wikitext-cleanup-button-wrapper").length )
			$("#editform").prepend('<div id="wikitext-cleanup-button-wrapper"></div>');
		$("#wikitext-cleanup-button-wrapper")
			.append('<div id="pixel-to-upright-value" class="wikitext-cleanup-button">convert pixel to scaling values</div>');
		$("#pixel-to-upright-value")
			.click(function() {
				convertToUpright(wikitext);
			});
	}
	else
	{
		let message = "imageSize.js found no images in wikitext.";
		
		if ( window.imageSizeNotifications )
			mw.notify(message);
		console.log(message);
	}
}
else
{
	let message = "Content model is not wikitext, so imageSize.js will do nothing.";
	
	if ( window.imageSizeNotifications )
			mw.notify(message);
	console.log(message);
}