Difference between revisions of "MediaWiki:Common.js"

Jump to navigation Jump to search
m (move arcanum searcher to widget)
m (move Arcanum stuff to Gadgets)
ModifySidebar( 'add', 'toolbox', 'Search Arcanum for '+ title, 'https://wob.coppermind.net/adv_search/?query='+ title );
});
 
/* 2018-08-22 auto-generate Cite:Arcanum pages for {{wob ref}} */
$(function () {
// determine if this page is editing a Cite: Arcanum page
const edit_box = document.querySelector('#editform textarea');
// check that there is an edit box
if (!edit_box || mw.config.get('wgCanonicalNamespace') !== 'Cite') return -1;
// check that it's a page creation
if (mw.config.get('wgArticleId') != 0) return -2;
// check that it's an Arcanum page
const page_name = mw.config.get('wgPageName');
if (!page_name.startsWith('Cite:Arcanum-')) return -3;
// work out the api url
const entry = page_name.split('-')[1];
const url = 'https://wob.coppermind.net/api/entry/'+ entry +'/';
// start fetching from Arcanum
fetch(url)
.then(function(resp) { return resp.json() })
.then(function(json) {
if (!json || !json.event) { return; }
// the <arcanum-entry-info> tag
const tag = '<arcanum-entry-info date="'+ (json.date || json.event_date) +'" event="'+ json.event +'">'+ json.event_name +'</arcanum-entry-info>';
// the rest of the page
const page = tag +"<noinclude>\n\n== Users ==\n{{Special:WhatLinksHere/{{FULLPAGENAME}}|}}\n[[category: generated citations]]\n";
// only fill in the edit box if it is currently blank
if (edit_box.value == '') {
edit_box.value = page;
// preview the page
document.querySelector('[name="wpPreview"]').click()
}
})
})
 
/* 2018-09-16 display Arcanum entry on Cite:Arcanum-X page */
function show_arcanum_entry() {
// check that it's an Arcanum page
const page_name = mw.config.get('wgPageName');
if (!page_name.startsWith('Cite:Arcanum-')) return;
// work out the api url
const entry = page_name.split('-')[1];
const url = 'https://wob.coppermind.net/api/entry/'+ entry +'/';
// start fetching from Arcanum
console.log('fetching '+ url);
fetch(url)
.then(function(resp) { return resp.json() })
.then(function(json) {
// debugging:
console.log('Entry '+ entry, json);
// create containing element
const entry_el = document.createElement('article');
entry_el.classList.add('arcanum-entry');
// add lines
json.lines.forEach(function(line) {
// create speaker element
const speaker = document.createElement('h4');
speaker.innerText = line.speaker;
if (speaker.innerText.includes('href') && speaker.innerText.includes('twitter.com')) {
// speaker is a link to twitter, so show it as such
speaker.innerHTML = speaker.innerText
}
// append the speaker and html from the line
entry_el.appendChild(speaker);
entry_el.innerHTML += line.text;
});
// place container on page
const users_el = document.getElementById('Users').parentElement
users_el.parentElement.insertBefore(entry_el, users_el);
});
};
// 2018-11-27 split the above into two so the function can be used elsewhere
//$(function () { show_arcanum_entry() });
mw.hook('wikipage.content').add(show_arcanum_entry)