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.
*/
/* 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 "
"[[User:Erutuon/scripts/imageSize.js|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!");
});
}
})();