User:Blupo/wikia.js

/*credit goes to andrewds1021*/

(function {    "use strict";    if (window.andrewds1021 && window.andrewds1021.category_css && window.andrewds1021.category_css.has_run) return;    if (!window.andrewds1021) {        window.andrewds1021 = {            category_css: {}        };    } else if (!window.andrewds1021.category_css) {        window.andrewds1021.category_css = {};    }    window.andrewds1021.category_css.has_run = true;    if (!window.andrewds1021.category_css.category_styles) {        window.andrewds1021.category_css.category_styles = {            "Market" : "MediaWiki:Market.css"        };    }    var list = window.andrewds1021.category_css.category_styles;    var list_keys = Object.keys(list); // get categories    if (list_keys.length <= 0) return;    /*    generate API request    */    console.log("CategoryCSS.js: Attempting to generate API request.");    var wgArticleId = mw.config.get('wgArticleId');    var request = { action: 'query', pageids: wgArticleId, prop: 'categories', clprop: 'hidden', cllimit: 1 };   /*    var request = { action: "query", titles: mw.config.get("wgPageName"), prop: "categories", cllimit: 1, clcategories: "Category:"+list_keys.join("|Category:") };   */    /*    send API request, check results, and import CSS */   console.log("CategoryCSS.js: Waiting for \"mediawiki.api\"."); mw.loader.using("mediawiki.api").then(function {        console.log("CategoryCSS.js: \"mediawiki.api\" has been loaded. Attempting to send API request.");        return new mw.Api.get(request);    }).then(function (reply) {        console.log("CategoryCSS.js: An API reply has been received. Attempting to extract data.");        var page = reply.query.pages[wgArticleId];        var categories = [];        if (page.categories) {            page.categories.map(function(category) { categories.push(category.title); });       }        if (categories[0]) {            var key = categories[0];            key = key.substring(key.indexOf(":")+1);            value = list[key] ? list[key] : list[key.replace(/\s/g, "_")];            if (!value) {                console.log("CategoryCSS.js: \"" + categories[0] + "\" could not be matched to an entry in the list. No additional CSS has been imported.");            } else {                console.log("CategoryCSS.js: Attempting to import additional CSS.");                importArticles({ type: "style", article: value });           }        } else {            console.log("CategoryCSS.js: There was an issue retrieving category data for \"" + mw.config.get("wgPageName") + "\". No additional CSS has been imported.");        }    }); });