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 18:18, 19 June 2017 (fixed regex, I think). 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.
*/

var contentModel = mw.config.values.wgPageContentModel;

var count;
if ( !count )
	count = 0;

function convertToUpright()
{
	var wikitext = $("#wpTextbox1").val();
	var oldWikitext = wikitext;
	
	wikitext = wikitext.replace(
		/\[\[(?:[Ff]ile|[Ii]mage)(?:[^\]]+|\[+[^\]]+\]+)+\]\](?=\n)/g,
		function(wholematch)
		{
			return wholematch.replace(
				/\|\s*(\d+)px\s*([\|\]])/,
				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)
		{
			addition = "converted image sizes in pixels to scaling values, as prescribed by [[WP:IMGSIZE]], using [[User:Erutuon/scripts/imageSize.js|JavaScript]]";
			
			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;
			}
		}
	);
}

if ( contentModel === "wikitext" )
{
	var wikitext = $("#wpTextbox1").val();
	
	if ( /\[\[(?:[Ff]ile|[Ii]mage)/.test(wikitext) )
		$("#editform").prepend('<div id="pixel-to-upright-value"><a href="javascript:convertToUpright()">convert pixel to scaling values</a></div>');
	else
	{
		var message = "imageSize.js found no images in wikitext.";
		
		if ( window.imageSizeNotifications )
			mw.notify(message);
		console.log(message);
	}
}
else
{
	var message = "Content model is not wikitext, so imageSize.js will do nothing.";
	
	if ( window.imageSizeNotifications )
			mw.notify(message);
	console.log(message);
}