MediaWiki:SpecialPages.js

// Credit go to Penguin-Pal // Modified it a bit to make it works with AjaxRC function SpecialPages { /* data */ var data = { pages: {}, // key = title, value = data ids: {}, // key = title, value = pageid specialPages: ["Longpages", "Newpages", "Protectedpages"] },       fn = {};

/* functions */ // json xhr fn.queryJsonUrl = function(url, cb) { var a = new XMLHttpRequest, b;       a.open("GET", url, true); a.onload = function { b = JSON.parse(a.responseText); cb(b); };       a.send; };

// prepare ids of pages fn.preparePageIds = function(list, cb) { if (list.length > 0) { fn.queryJsonUrl(mw.config.get("wgScriptPath") + "/api.php?action=query&format=json&titles=" + encodeURIComponent(list.splice(0, 100).join("|")), function(response) {               var pages = response.query.pages,                    pageid;                for (pageid in pages) {                    data.ids[pages[pageid].title] = pageid;                }                fn.preparePageIds(list, cb);            }); } else { cb; }   };

// prepare data of pages fn.preparePageData = function(ids, cb) { if (ids.length > 0) { fn.queryJsonUrl("/api/v1/Articles/Details?abstract=150&width=128&height=128&ids=" + ids.splice(0, 100).join(","), function(response) {               var page,                    pageid;                for (pageid in response.items) {                    page = response.items[pageid];                    data.pages[page.title] = {                        id: page.id,                        title: page.title,                        desc: page.abstract,                        url: page.url,                        src: page.thumbnail || mw.config.get("wgBlankImgUrl")                    };                }                fn.preparePageData(ids, cb);            }); } else { cb; }   };

// generate markup fn.generateMarkup = function(list) { var nav = $(''), item, pg, columns = 3; list.forEach(function(page, i) {           pg = data.pages[page];            item = $('').attr({ href: pg.url, title: pg.title }).append( $('').attr({                   src: pg.src,                    width: 128,                    height: 128                }), $(' ').text(pg.title), $(' ').text(pg.desc) );           $(' ').attr({ "data-row": i % 3 + 1 }).append(item).appendTo(nav);           if (i % columns == 2) {                $(nav).append(' ');           }        });        return nav; };

// prepare pages fn.preparePages = function(list, cb) { fn.preparePageIds(list.concat, function {           var ids = [];            list.forEach(function(page) { ids.push(data.ids[page]); });           fn.preparePageData(ids, function { var nav = fn.generateMarkup(list); cb(nav); });       });    };

// get selectors for given special page fn.getSpecialPageSelectors = function(specialPage) { var s = { wrapper: "ol.special", items: "ol.special li a"       }; switch (specialPage) { case "Protectedpages": s.wrapper = "#mw-content-text > ul"; s.items = "#mw-content-text > ul a"; break; case "Newpages": s.wrapper = "#mw-content-text > ul"; s.items = "#mw-content-text > ul a.mw-newpages-pagename"; break; case "Longpages": s.items = "ol.special a:last-of-type"; break; }       return s;    };

/* implementations */ var specialPage = mw.config.get("wgCanonicalSpecialPageName"), specialPageContent = [], selectors; if (data.specialPages.indexOf(specialPage) > -1) { selectors = fn.getSpecialPageSelectors(specialPage); $(selectors.items).each(function {           specialPageContent.push($(this).text);        }); fn.preparePages(specialPageContent, function(nav) {           $(selectors.wrapper).html(nav);        }); } }

$(function {   $(SpecialPages);    // ajaxrc support    window.ajaxCallAgain = window.ajaxCallAgain || [];    window.ajaxCallAgain.push(SpecialPages); });