MediaWiki:FAQ/code.js

importArticle({   type: "style",    article: "MediaWiki:FAQ.css" });

if (!window.hasOwnProperty('dev')) { window.dev = {}; } if (!window.dev.hasOwnProperty('faq')) { window.dev.faq = {}; } if (!window.dev.faq.hasOwnProperty('page')) { window.dev.faq.page = wgFormattedNamespaces[4] + ':FAQ'; } if (wgPageName === window.dev.faq.page) { $.getJSON(mw.util.wikiScript('api'), {       action: 'query',        prop: 'revisions',        rvprop: 'content',        titles: 'MediaWiki:Custom-FAQ.json',        format: 'json',        indexpageids: true,        q: Math.random    }, function(res) {        displayFAQ(JSON.parse(res.query.pages[res.query.pageids[0]].revisions[0]['*']));    });

function displayFAQ(faqs) { mw.util.$content.empty; faqs.forEach(function(faq, idx) {           var el = $(' ', { class: 'faq', id: idx })               .append( $(' ', {                       class: 'question'                    }) .text(faq.question), $(' ', {                       class: 'answer'                    }) .append(                       $('')                        .text(faq.answer)                    ) )               .appendTo(mw.util.$content);            if (faq.hasOwnProperty('related')) {                var answer = el.find('.answer');                el.find('.answer')                    .append( $(' '),                       $(' ', {                            class: 'related'                        }) );               if (faq.related.hasOwnProperty('articles')) {                    faq.related.articles.forEach(function(article) { answer.find('.related') .append(                               $(' ')                                .append( $('', {                                       href: mw.util.getUrl(article)                                    }) .text(article) )                           );                    });                }                if (faq.related.hasOwnProperty('discussions')) {                    faq.related.discussions.forEach(function(discussion) { answer.find('.related') .append(                               $(' ')                                .append( $('', {                                       href: '/d/p/' + discussion.p                                    }) .text(discussion.title) )                           );                    });                }                if (faq.related.hasOwnProperty('external')) {                    faq.related.external.forEach(function(external) { answer.find('.related') .append(                               $(' ')                                .append( $('', {                                       href: external.url                                    }) .text(external.title) )                           );                    });                }            }        });        $('.faq .answer') .hide; $('.faq .question') .click(function {               $(this)                    .next                    .toggle;            }); } }