User:Jon Harald Søby/warnOnLargeFile.js
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.
Documentation for this user script can be added at User:Jon Harald Søby/warnOnLargeFile. |
/**
* 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", "jquery.i18n" ] ).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' ),
translations = {
en: {
// $1 is the number as an integer, $2 is the number formatted as a string
dimensions: "This file's height or width is larger than $2 {{plural:$1|pixel|pixels}}. Are you sure you want to continue?",
filesize: "This file is larger than $2 {{plural:$1|MB|MB}}. Are you sure you want to continue?"
},
de: {
dimensions: "Die Breite oder Höhe dieser Datei ist größer als $2 Pixel. Trotzdem fortfahren?",
filesize: "Diese Datei ist größer als $2 MB. Trotzdem fortfahren?"
},
nb: {
dimensions: "Denne filas høyde eller bredde er større enn $2 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 $2 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?"
}
};
$.i18n().load(translations);
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" )
.add( $( ".fullMedia a" ).first() ),
$archiveImageLinks = $( "table.filehistory a[href~='/archive/']");
info.forEach( function( version ) {
let message = false,
$selector;
if ( version.height >= maxdimension || version.width >= maxdimension ) {
message = $.i18n( "dimensions", maxdimension, mw.language.convertNumber( maxdimension ) );
} else if ( version.size >= ( maxsizeMB * 1000000 ) ) {
message = $.i18n( "filesize", maxsizeMB, mw.language.convertNumber( maxsizeMB ) );
} else {
return;
}
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 ) + "']" );
}
$selector.on( "click", function( e ) {
e.preventDefault();
let $this = $( this );
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]]