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. |
var contentModel = mw.config.values.wgPageContentModel;
function convertToUpright()
{
var wikitext = $("#wpTextbox1").val();
var oldWikitext = wikitext;
wikitext = wikitext.replace(
/\[\[(?:[Ff]ile|[Ii]mage)[^\]]+\]\]/g,
function(wholematch)
{
return wholematch.replace(
/\|\s*(\d+)px\s*\|/,
function(match, number)
{
// Convert string to number.
number = Number(number);
// Convert to upright value.
number = number / 220;
// Round to nearest hundredth.
number = Math.floor( number * 100 ) / 100;
return "|upright=" + number + "|";
}
);
}
);
$("#wpTextbox1").val(wikitext);
$("#wpSummary").val(function(index, summary)
{
addition = "converted image sizes in pixels to scaling values, as prescribed by [[WP:IMGSIZE]]";
afterSectionName = summary.match(/^\/\*[^\*]+\*\/\s+(.+)/);
if ( afterSectionName && afterSectionName[1].length > 1 )
addition = "; " + addition;
if ( oldWikitext !== wikitext && ( !afterSectionName || !afterSectionName[1].includes(addition) ) )
return summary + addition;
else
{
console.log("imageSize.js made no changes.");
mw.notify("imageSize.js made no changes.");
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
{
console.log("imageSize.js found no images in wikitext.");
mw.notify("imageSize.js found no images in wikitext.");
}
}
else
{
console.log("Content model is not wikitext, so imageSize.js will do nothing.");
mw.notify("Content model is not wikitext, so imageSize.js will do nothing.");
}