MediaWiki:Gadget-ChooseResolution.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.
This user script seems to have a documentation page at MediaWiki:Gadget-ChooseResolution. |
- Report page listing warnings and errors.
// <source lang="javascript">
/*
Quick links to choose file sizes.
Author: [[User:Lupo]], July 2009
License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0)
Choose whichever license of these you like best :-)
*/
// Global: getElementsByClassName, importScript (wiki.js)
if (mw.config.get('wgNamespaceNumber') == 6 && (mw.config.get('wgAction') == 'view' || mw.config.get('wgAction') == 'purge')) {
importScript ('MediaWiki:LAPI.js');
importScript ('MediaWiki:Tooltips.js');
$(document).ready(function ()
{
var license_categories = [
{re: /^cc-[^ /]*/i , match: 0}
,{re: /(^|\b)(gfdl(-\S+)?)/i, match: 0}
,{re: /^pd-\S+/i , match: 'Public domain'}
,{re: /public domain/i , match: 'Public domain'}
];
var skin = mw.config.get('skin');
var tooltip_styles = // The style for all our tooltips
{ border : '1px solid #8888aa'
, backgroundColor : '#ffffe0'
, padding : '0.3em'
, fontSize : ((skin && (skin == 'monobook' || skin == 'modern')) ? '127%' : '100%')
// Scale up to default text size
};
var install_attempts = 0;
var max_install_attempts = 5;
var close_img_url =
'//upload.wikimedia.org/wikipedia/commons/thumb/3/35/Button_normal.svg/14px-Button_normal.svg.png';
function install ()
{
if (mw.config.get('wgTitle').search (/\.(jpe?g|png|gif)$/i) < 0) return; // Only for images
// SVG not included, they have their own size-links below the image
if (typeof (LAPI) == 'undefined' || typeof (Tooltip) == 'undefined') {
if (install_attempts++ <= max_install_attempts) window.setTimeout (install, 500);
return;
}
var chooser_div = LAPI.$ ('chooseresolution_main');
if (chooser_div) return; // Already installed
var file_div = LAPI.$ ('file');
if (!file_div) return; // No file
img = LAPI.WP.getPreviewImage ();
if (!img) return;
thumb = {width: img.offsetWidth, height: img.offsetHeight};
full_img = LAPI.WP.fullImageSizeFromPage ();
// If we miss either the thumbnail or the full image size, don't do anything.
if ( thumb.width <= 0 || thumb.height <= 0
|| full_img.width <= 0 || full_img.height <= 0
|| full_img.width < thumb.width
|| full_img.height < thumb.height)
return;
// Now collect preview sizes
var sizes = [];
if (thumb.width < 0.9 * thumb.height)
// Vertical image
sizes = [[null, 480], [null, 600], [null, 800], [null, 1024], [null, 2048]];
else
sizes = [[400, null], [640, null], [800, null], [1024, null], [1280, null], [2048, null]];
Array.forEach (sizes,
function (elem) {
if (elem[0] === null)
elem[0] = Math.floor ((elem[1] * full_img.width) / full_img.height);
else
elem[1] = Math.floor ((elem[0] * full_img.height) / full_img.width);
if (elem[0] <= thumb.width || elem[0] >= full_img.width) {
elem[0] = null; elem[1] = null;
}
}
);
sizes = Array.filter (sizes, function (elem) {return elem[0] !== null;});
// Now we have the sizes we want to display.
if (!sizes || sizes.length == 0) return;
// HTML we want to show:
// <div style="text-align:center">
// <a href="Image description page"><img src="URL" style="border:none;"/></a><br/>
// Author: author, License: <a href="Link to template at Commons">license</a>
// </div>
var author = LAPI.$ ('fileinfotpl_aut');
if (author) {
author = author.parentNode.getElementsByTagName ('td');
if (author && author.length > 0) {
author = author[0];
var name = getElementsByClassName (author, '*', 'creator-name');
if (name && name.length > 0) {
name = name[0];
var realname = getElementsByClassName (name, '*', 'fn');
if (realname && realname.length > 0)
author = LAPI.DOM.getInnerText (realname[0]);
else
author = LAPI.DOM.getInnerText (name);
} else
author = LAPI.DOM.getInnerText (author);
} else
author = null;
}
if (author) author = author.trim ();
// Find license categories
var category_holder = LAPI.$ ('catlinks');
var licenses = [];
var pd_done = false;
if (category_holder) category_holder = category_holder.getElementsByTagName ('span');
if (category_holder) {
Array.forEach (
category_holder
, function (cat) {
var a = cat.getElementsByTagName ('a');
if (!a || a.length == 0) return;
a = LAPI.WP.pageFromLink (a[0]);
if (!a) return;
a = a.substring (a.indexOf (':') + 1); // Strip off namespace prefix
Array.forEach (license_categories,
function (test) {
var m = test.re.exec (a);
if (m) {
if (typeof (test.match) == 'string')
m = test.match;
else
m = m[test.match];
if (!Array.contains (licenses, m)) licenses[licenses.length] = m.trim ();
}
}
);
}
);
}
if (licenses.length == 0)
licenses = 'see <a href="' + mw.config.get('wgServer') + wgArticlePath.replace ('$1', encodeURI (mw.config.get('wgPageName'))) + '">image source</a>';
else {
licenses = Array.map(licenses,
function (elem) {
// Construct a link to a Commons template for all cc or gfdl licenses
if (elem.search (/^(cc-|gfdl)/i) == 0)
return '<a href="//commons.wikimedia.org/wiki/Template:' + encodeURI (elem.split (' ').shift ()) + '">' + elem + '</a>';
else
return elem;
}
).join (', ');
}
// Determine where the image comes from.
var thumb_url = img.parentNode.getAttribute ('href', 2);
var project = null;
var project_url = null;
if (thumb_url.contains ('/commons/')) {
project = 'Wikimedia Commons';
project_url = '//commons.wikimedia.org/';
} else {
project_url = mw.config.get('wgServer');
project = mw.config.get('wgServer').split ('.')[1].capitalizeFirst ();
}
var html =
'<div>'
+ '<a href="' + mw.config.get('wgServer') + wgArticlePath.replace ('$1', encodeURI (mw.config.get('wgPageName'))) + '">'
+ '<img width="\x01" height="\x02" src="\x03" />'
+ '</a><br />'
+ 'From <a href="' + project_url + '">' + project + '</a>; '
+ (author ? 'Author: ' + author + '; ' : "")
+ 'Licensing: ' + licenses + '.'
+ '</div>';
// Now add links: <a href="URL">size</a> (<a href="#">html</a>)
chooser_div = LAPI.make ('div', {id: 'chooseresolution_main'}, {marginBottom: '6px'});
function add (width, height, is_full)
{
if (chooser_div.firstChild) chooser_div.appendChild (document.createTextNode ('\xa0| '));
var url = null;
if (is_full)
url = img.parentNode.getAttribute ('href', 2);
else {
url = img.getAttribute ('src', 2);
var i = url.lastIndexOf ('/');
url = url.substring (0, i)
+ url.substring (i).replace (/^\/\d+px-/, '/' + width + 'px-');
}
chooser_div.appendChild (LAPI.DOM.makeLink (url, "" + width + '×' + height));
chooser_div.appendChild (document.createTextNode ('\xa0('));
var popup_lk = LAPI.DOM.makeLink ('#', 'HTML');
chooser_div.appendChild (popup_lk);
chooser_div.appendChild (document.createTextNode (')'));
var popup_content = LAPI.make ('div');
popup_content.appendChild (LAPI.make ('input', {size: 40}, {width: '400px'}));
popup_content.firstChild.value =
html.replace ('\x01', width)
.replace ('\x02', height)
.replace ('\x03', url);
var tooltip = new Tooltip (
popup_lk, popup_content
, { activate : Tooltip.CLICK
,deactivate : Tooltip.CLICK_ELEM | Tooltip.ESCAPE
,close_button : LAPI.DOM.makeImage (close_img_url, 14, 14)
,mode : Tooltip.FIXED
,open_delay : 0
,hide_delay : 0
}
, tooltip_styles
);
}
Array.forEach (sizes, function (elem) {add (elem[0], elem[1]);});
add (full_img.width, full_img.height, true);
LAPI.DOM.insertNode (chooser_div, file_div);
}
install ();
});
} // end if on image page
// </source>