User:NguoiDungKhongDinhDanh/script-imageres.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:NguoiDungKhongDinhDanh/script-imageres. |
// Originally by Alex 21
$(document.getElementsByClassName("fileInfo")).ready(function() {
// Filenamespace only
if (mw.config.get("wgNamespaceNumber") !== 6) return;
// Get file information: height and width
var fileInfo = document.getElementsByClassName("fileInfo")[0];
if(!fileInfo) return;
var height_width = fileInfo.innerHTML.match(/([\d\.,]+) ?× ?([\d\.,]+)/);
// With the height and width, calculate the total pixel count
var width = parseInt(height_width[1].replace(/(\.|,)/g,''));
var height = parseInt(height_width[2].replace(/(\.|,)/g,''));
var total_pixels = height*width;
// Colour coding the results
var colour_start = "<b style='color:green'>";
var colour_end = "</b>";
// Add the total pixel count after the initial dimensions
fileInfo.innerHTML = fileInfo.innerHTML.replace(/([\d\.,]+) ?× ?([\d\.,]+)/,
number_format(width) + " × " + number_format(height) + " = " + colour_start + number_format(total_pixels) + colour_end + " ");
// I_hate_those_underscores.
$(".internal").text($(".internal").text().replace(/_/g, " "));
// Add commas to values over 1000.
function number_format(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
});