MediaWiki:CategoryIcon/code.js

//adds image to text in category page //selector: '#mw-pages ul li a' (function($) {   var ci = window.fng ? window.fng.cicon ? window.fng.cicon : {} : {fng: {cicon: {}}};//cicon    if (ci.f || (mw.config.get('wgNamespaceNumber') !== 14)) return;    //debug    ci.debug = ci.debug !== undefined ? ci.debug : false;    //image extension    ci.ext = ci.ext || '.png';    //image class    ci.cls = ci.cls || 'cicon-image';    ci.cls = $(' ', {text: ci.cls}).text;    //scale    ci.scale = ci.scale === undefined ? '/scale-to-width-down/100' : ci.scale ===  ?  : $(' ', {text: ci.scale}).text;    //image prepend: prep+name+ext    ci.iprepend = ci.iprepend !== undefined ? ci.iprepend : ;    //image append: name+append+ext    ci.iappend = ci.iappend !== undefined ? ci.iappend : ;    //text prepend: prepend+     ci.tprepend = ci.tprepend !== undefined ? ci.tprepend : '';    //text append: +append    ci.tappend = ci.tappend !== undefined ? ci.tappend : ''; ci.getObj = function getObj (data, key) { //traverse through object tree var ret = [], r;       for (var k in data) { if (data[k] instanceof Object) { if (k === key) { ret.push(data[k]); }               r = ci.getObj(data[k], key); if (r) ret = ret.concat(r); } //if obj } //for k in data return ret; }; //getObj ci.getVal = function getVal (data, key) { //traverse through object tree var ret = [], r;       for (var k in data) { if (data[k] instanceof Object) { r = ci.getVal(data[k], key); if (r) ret = ret.concat(r); } else { if (k === key) { ret.push(data[k]); }           } //if obj } //for k in data return ret; }; //getVal ci.f = function { if (ci.debug) console.log('ci', $('#mw-pages ul li').length); $('#mw-pages ul li a').each(function {           var aimage = mw.Title.newFromText(ci.iprepend + $(this).text + ci.iappend + ci.ext, 6);            var api = new mw.Api, $el = $(this);            if (ci.debug) console.log('ci', aimage, $el);            api.get({action: 'query', redirects: '', titles: aimage.getPrefixedDb, prop: 'imageinfo', iiprop: 'url', format: 'json'})           .done(function(data) { var image = {}; if (ci.debug) console.log('ci data', data); var page = {}; page.missing = data.query.pages['-1'] ? true : false; if (page.missing) return; //for(var page in data.query.pages) { //rwrt to obj.find //if(!page.missing) { page.imageinfo = ci.getObj(data.query.pages, 'imageinfo'); page.imageinfo = page.imageinfo.length > 0 ? page.imageinfo[0] : null; if (!page.imageinfo) return; if (ci.debug) console.log('ci ii', page.imageinfo); page.redirects = ci.getVal(ci.getObj(data.query, 'redirects'), 'to'); page.redirects = page.redirects.length > 0 ? page.redirects[0] : null; image.url = page.redirects || page.imageinfo.url || ' '; if (image.url === ' ') return; //add scaling image.url = image.url.indexOf('?') > -1 ? image.url.replace('?', ci.scale + '?') : image.url + ci.scale; //ensure url is safe and probably break it                       //image.url = $(' ', {text: image.url}).text; image.img = $(' ', {src: image.url, class: ci.cls}); //stacking stuff if (ci.tappend) $el.prepend($(' ', {text: ci.tappend,class: 'cicon-append'})); $el.prepend(image.img); if (ci.tprepend) $el.prepend($(' ', {text: ci.tprepend, class: 'cicon-prepend'})); //}//if image exists //}//for in pages });//done api.get       });//each li a    };//f window.fng = $.extend(window.fng, {cicon: ci}); $(ci.f); //^add pagination hook here^ }(jQuery));//'#mw-pages ul li a'