User:Erutuon/scripts/imageSize.js

/*	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.

/* jshint esversion: 6, undef: true, unused: true, varstmt: true */ /* globals $, CleanupButtons, mw */

(function { "use strict";

if ( mw.config.get("wgPageContentModel") === "wikitext"	&& [ "edit", "submit" ].indexOf(mw.config.get("wgAction")) !== -1) { const convertToUpright = function (wikitext) { const oldWikitext = wikitext; let 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) {				if ( !/\|(thumb|frameless)/.test(wholematch) )					return wholematch;				return wholematch.replace( /\|\s*(\d+)px\s*([\|\]])/g, function(match, number, afterNumber) { // Convert string to number. number = Number(number); if ( number < 50 ) return match; count += 1; // Convert to upright value. number = number / 220; // Round to nearest hundredth. number = Math.floor( number * 100 ) / 100; return "|upright=" + number + afterNumber; });			});		if ( wikitext === oldWikitext ) console.log("imageSize.js made no changes."); else if ( count > 0 ) { let agreement = count === 1 ? "" : "s"; const message = "imageSize.js made " + count + " replacement" + agreement + "."; if ( window.imageSizeNotifications ) mw.notify(message); console.log(message); }		CleanupButtons.addSummary(count, "converted image sizes in pixels to "				+ "scaling values, as prescribed by WP:IMGSIZE, using "				+ "JavaScript"); return wikitext; };	mw.loader.load("//en.wiktionary.org/w/index.php?title=User:Erutuon/"		+ "styles/wikitext-cleanup.css&action=raw&ctype=text/css",		"text/css"); $.getScript('//en.wiktionary.org/w/index.php?title=User:Erutuon/scripts/CleanupButtons.js&action=raw&ctype=text/javascript') .done( => {		$( => { const buttons = new CleanupButtons; if (CleanupButtons.evaluateConditions(/\[\[(?:[Ff]ile|[Ii]mage)[^\]]+px/)) { buttons.addButton({					button: { text: 'convert pixel to scaling values' },					func: convertToUpright,				}); }		});	}).fail( => {		console.error("Failed to load CleanupButtons script!");	}); }

});