User:Splarka/galleryimagelist.js

/* 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("http://upload.wikimedia.org/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('');
}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.