User:NguoiDungKhongDinhDanh/script-imageres.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// 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, ".");
	}
});