User:Erutuon/scripts/imageSize.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Erutuon/scripts/imageSize. |
/*
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);
}