|
|
| Line 14: |
Line 14: |
| }); | | }); |
| }); | | }); |
| $(function () {
| | $.getJSON(mw.util.wikiScript('api'), { |
| // Where to display
| | action: 'parse', |
| var $spot = $('<div id="lore-spotlight"><h3>Random Lore Spotlight</h3><div class="content">Loading…</div></div>');
| | page: page.title, |
|
| | prop: 'extensiondata', |
| // Only show on Main Page (or wherever you want)
| | format: 'json' |
| if (mw.config.get('wgPageName') !== 'Main_Page') return;
| | }).done(function (parsed) { |
| $('#mw-content-text').prepend($spot);
| | var hooks = parsed.parse.extensiondata['lore-spotlights'] || []; |
| | | if (hooks.length > 0) { |
| // Get a random page in category Lore
| | var hook = hooks[Math.floor(Math.random() * hooks.length)]; |
| $.getJSON(mw.util.wikiScript('api'), {
| | $spot.find('.content').html(hook); |
| action: 'query',
| | } |
| list: 'random',
| |
| rnnamespace: 0,
| |
| rnlimit: 1,
| |
| rncategory: 'Lore',
| |
| format: 'json'
| |
| }).done(function (data) { | |
| var page = data.query.random[0];
| |
| | |
| // Get the page source (wikitext)
| |
| $.get(mw.util.wikiScript('api'), {
| |
| action: 'parse',
| |
| page: page.title,
| |
| prop: 'wikitext',
| |
| format: 'json'
| |
| }).done(function (parsed) {
| |
| var wikitext = parsed.parse.wikitext['*'];
| |
| | |
| // Regex to find <spotlighthook>…</spotlighthook>
| |
| var hooks = [];
| |
| var regex = /<spotlighthook>(.*?)<\/spotlighthook>/g;
| |
| var match;
| |
| while ((match = regex.exec(wikitext)) !== null) {
| |
| hooks.push(match[1]);
| |
| }
| |
| | |
| if (hooks.length > 0) {
| |
| var hook = hooks[Math.floor(Math.random() * hooks.length)];
| |
| $spot.find('.content').html(hook);
| |
| } else {
| |
| // fallback
| |
| $spot.find('.content').html('<b><a href="/wiki/' + encodeURIComponent(page.title) + '">' + page.title + '</a></b>');
| |
| }
| |
| });
| |
| }); | |
| }); | | }); |