User:Splarka/galleryimagelist.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:Splarka/galleryimagelist. |
/* Load image thumbnails on Special:ImageList, version [0.0.2c]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/galleryimagelist.js
Notes:
* Uses the API to generate thumbnails of all images listed.
* Shows them under the matching title (batched means they get disarranged, so matching may not be perfect).
* If they are smaller than the chosen width, they get shown at their natural size.
* Maximum height defined as 1.5x maximum width, to prevent large vertical images.
* Currently limited to 50 thumbnails per page.
Todo:
* Test crude title matching.
* Use href of image instead of getText() (have to match against server and image namespace translations)?
** DON'T USE TITLE, subject to change.
* Queue up queries for pages with more than 50, dammit.
*/
var galleryImageListWidth = 100;
if(wgCanonicalSpecialPageName && (wgCanonicalSpecialPageName.toLowerCase() == 'imagelist' || wgCanonicalSpecialPageName.toLowerCase() == 'filelist' || wgCanonicalSpecialPageName.toLowerCase() == 'listfiles')) addOnloadHook(function() {
mw.util.addPortletLink('p-cactions','javascript:galleryImageList()','show thumbnails','ca-galil');
})
function galleryImageList() {
appendCSS('#ca-galil a {visibility:hidden;}');
var table = getElementsByClassName(document,'table','TablePager')[0];
var ti = getElementsByClassName(table,'td','TablePager_col_img_name');
var imgs = [];
for(var i=0;i<ti.length;i++) {
var imgname = getText(ti[i].getElementsByTagName('a')[0]).replace(/_/ig,' ');
imgs.push('Image:' + encodeURIComponent(imgname));
}
var width = window.galleryImageListWidthOverride || window.galleryImageListWidth
var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?maxage=300&smaxage=300&format=json&callback=galleryImageListCB&action=query&prop=imageinfo&iiprop=url&iiurlwidth=' + width + '&iiurlheight=' + parseInt(width * 1.5) + '&titles=' + imgs.join('|')
mw.loader.load(url);
}
function galleryImageListCB(obj) {
if(!obj['query'] || !obj['query']['pages']) return
var table = getElementsByClassName(document,'table','TablePager')[0];
var ti = getElementsByClassName(table,'td','TablePager_col_img_name');
var thumbs = obj['query']['pages'];
for(var i in thumbs) {
var title = thumbs[i]['title'];
for(var j=0;j<ti.length;j++) {
var imgtitle = getText(ti[j].getElementsByTagName('a')[0]).replace(/_/ig,' ');
if(title.indexOf(imgtitle) != -1) {
var img = document.createElement('img');
img.style.width = thumbs[i]['imageinfo'][0]['thumbwidth'] + 'px';
img.style.height = thumbs[i]['imageinfo'][0]['thumbheight'] + 'px';
img.style.border = '1px solid #999999';
img.style.background = 'url("/media/wikipedia/commons/5/5d/Checker-16x16.png") repeat';
img.setAttribute('title',title);
img.setAttribute('src',thumbs[i]['imageinfo'][0]['thumburl'])
ti[j].appendChild(document.createElement('br'));
ti[j].appendChild(img);
continue;
}
}
}
}
function getText(obj) {
if(obj.nodeType == 3) return obj.nodeValue;
var txt = [];
var i = 0;
while(obj.childNodes[i]) {
txt[txt.length] = getText(obj.childNodes[i]);
i++;
}
return txt.join('');
}