User:Ilmari Karonen/ifdthumbnails.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:Ilmari Karonen/ifdthumbnails. |
// Add thumbnails to IfD pages; see the talk page for documentation.
if (/^(Wikipedia:((Files|Images_and_media)_for_deletion|Possibly_unfree_(files|images))|Commons:Deletion_requests)(\/|$)/.test(mw.config.get('wgPageName'))) {
var hTag = (mw.config.get('wgDBname') == "commonswiki" ? "h3" : "h4");
mw.util.addCSS(
"a.ifd-thumbnail { display: block; float: right; }\n" +
"a.ifd-thumbnail img { border: 1px solid #ccc; margin: 0 0 0.5em 1em; }\n" +
hTag + ", div.delh { clear: right; }\n"
);
var createThumbnails = function() {
// compile a list of images being discussed and save the heading node for each
var headings = document.getElementsByTagName(hTag);
var images = [];
var imageLocations = {};
for (var i = 0; i < headings.length; i++) {
var links = headings[i].getElementsByTagName("a");
for (var j = 0; j < links.length; j++) {
if (!/^(Image|File):/i.test(links[j].title) || /(^|\s)new(\s|$)/.test(links[j].className))
continue;
images.push(links[j].title);
imageLocations[links[j].title] = headings[i];
break;
}
}
if (!images.length) return; // nothing to do :-/
headings = null; // free DOM nodeList, we're done with it
// make an API request to find URLs for each image
var showThumbnails = function (result) {
// parse JSON output
if (result.error)
return; // alert("API error " + result.error.code + ": " + result.error.info);
if (!result.query || !result.query.pages)
return; // alert("Unexpected API result:\n" + ajax_result.responseText);
// hopefully we don't get any normalization, but just in case...
if (result.query.normalized) {
var norm = result.query.normalized;
for (var i = 0; i < norm.length; i++) {
imageLocations[norm[i].to] = imageLocations[norm[i].from];
}
}
// now loop over the result and insert thumbs
var pages = result.query.pages;
for (var id in pages) {
var title = pages[id].title;
if (!title) continue; // should not happen
if (pages[id].imagerepository && pages[id].imagerepository == "shared")
continue; // don't show thumbnails for Commons images (which may show up on IfD after a local copy has been deleted)
var info = pages[id].imageinfo;
if (!info || !info.length) continue; // can happen if the image has been deleted
info = info[0];
if (!info.thumburl || !info.thumbwidth || !info.thumbheight || !info.descriptionurl) continue; // should not happen
var heading = imageLocations[title];
if (!heading) continue; // should not happen
var img = document.createElement("img");
img.src = info.thumburl;
img.width = info.thumbwidth;
img.height = info.thumbheight;
img.alt = title;
var link = document.createElement("a");
link.href = info.descriptionurl;
link.title = title;
link.className = 'ifd-thumbnail';
link.appendChild(img);
heading.parentNode.insertBefore(link, heading.nextSibling);
}
};
var queryPrefix = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/api.php?format=json&action=query&maxage=3600&prop=imageinfo&iiprop=url&iiurlwidth=80&iiurlheight=80&titles=";
var batchSize = 50;
for (var i = 0; i < images.length; i += batchSize)
$.getJSON(queryPrefix + encodeURIComponent(images.slice(i,i+batchSize).join("|"))).then(showThumbnails);
};
$(createThumbnails);
}