User:Jon Harald Søby/warnOnLargeFile.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload

/**
 * Script that triggers a warning popup if you're about to open a file that
 * is above a certain threshold. The default thresholds are dimensions over
 * 10,000 pixels, or file sizes over 100 MB.
 * 
 * You can set your own thresholds by adding the following to your common.js (or global.js):
var warnOnLargeFile_maxdimension = 1000; // number of pixels
var warnOnLargeFile_maxsizeMB = 10; // file size in MB
 *
 * Enable the script by adding the following to your common.js or global.js page:
 *   mw.loader.load( 'https://commons.wikimedia.org/w/index.php?title=User:Jon_Harald_Søby/warnOnLargeFile.js&action=raw&ctype=text/javascript' );
 */
mw.loader.using( [ "oojs", "oojs-ui", "mediawiki.api", "mediawiki.util" ] ).done( function() {
	if ( ( mw.config.get( "wgNamespaceNumber" ) === 6 ) && $( "#file" ).length ) {
		let api = new mw.Api(),
			foreignApi = new mw.ForeignApi( 'https://commons.wikimedia.org/w/api.php' );
		
		let messages = function() {
			let translations = {
				en: {
					dimensions: "This file's height or width is larger than $1 pixels. Are you sure you want to continue?",
					filesize: "This file is larger than $2 MB. Are you sure you want to continue?"
				},
				nb: {
					dimensions: "Denne filas høyde eller bredde er større enn $1 piksler. Er du sikker på at du vil fortsette?",
					filesize: "Denne fila er større enn $2 MB. Er du sikker på at du vil fortsette?"
				},
				nn: {
					dimensions: "Høgda eller breidda til denne fila er større enn $1 pikslar. Er du sikker på at du vil halde fram?",
					filesize: "Denne fila er større enn $2 MB. Er du sikker på at du vil halde fram?"
				}
			},
				chain = mw.language.getFallbackLanguageChain(),
				len = chain.length,
				ret = {},
				i = len - 1;
			while ( i >= 0 ) {
				if ( translations.hasOwnProperty( chain[ i ] ) ) {
		        	$.extend( ret, translations[ chain[ i ] ] );
		    	}
		    	i = i - 1;
	    	}
			return ret;
		}();
		
		let maxdimension = 10000, // px
			maxsizeMB = 100; // MB
		if ( typeof warnOnLargeFile_maxdimension === "number" ) maxdimension = warnOnLargeFile_maxdimension;
		if ( typeof warnOnLargeFile_maxsizeMB === "number" ) maxsizeMB = warnOnLargeFile_maxsizeMB;
		
		let queryparams = {
			action: "query",
			format: "json",
			prop: "imageinfo",
			titles: "File:" + mw.config.get( "wgTitle" ),
			iiprop: "archivename|dimensions|size",
			iilimit: 500
		};
		api.get( queryparams ).then( function( data ) {
			if ( data.query.pages.hasOwnProperty( "-1" ) && data.query.pages["-1"].imagerepository === "shared" ) {
				return foreignApi.get( queryparams );
			} else {
				return data;
			}
		}).done( function( data ) {
			let articleId = Object.keys( data.query.pages )[ 0 ],
				info = data.query.pages[ articleId ].imageinfo,
				$mainImageLink = $( "#file a img" ).first().parent().add( ".mw-filepage-other-resolutions .mw-thumbnail-link:last-of-type" ),
				$archiveImageLinks = $( "table.filehistory a[href~='/archive/']");
			info.forEach( function( version ) {
				let triggerCondition = false,
					$selector;
				if ( version.height >= maxdimension || version.width >= maxdimension ) triggerCondition = "dimensions";
				if ( version.size >= ( maxsizeMB * 1000000 ) ) triggerCondition = "filesize";
				if ( !version.hasOwnProperty( "archivename" ) ) {
					$selector = $mainImageLink.add( "table.filehistory td.filehistory-selected a, table.filehistory td.filehistory-selected + td a" );
				} else {
					$selector = $( "table.filehistory a[href$='/" + mw.util.rawurlencode( version.archivename ) + "']" );
				}
				if ( triggerCondition ) {
					$selector.on( "click", function( e ) {
						e.preventDefault();
						let $this = $( this ),
							message = messages[ triggerCondition ].replace( "$1", maxdimension ).replace( "$2", maxsizeMB );
						OO.ui.confirm( message ).done( function( confirmed ) {
							if ( confirmed ) {
								if ( e.ctrlKey ) {
									window.open( $this.attr( "href" ) );
								} else {
									window.location.href = $this.attr( "href" );
								}
							}
						});
					});
				}
			});
		});
	}
});
// [[Category:User scripts]]