User:Fbstj/gadget/history-test.js

From The Coppermind
< User:Fbstj
Revision as of 12:04, 22 June 2018 by Fbstj (talk | contribs) (try again)
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* Add edit link to templates */
$(function () {

function add_editsection_link(box) {
 if (!box.id) return;
 // FIXME: what does this do?
 const id = box.id.replace(/\.(\d+)/, '%$1')
 // generate an edit link
 const el = make_editsection_link('/edit/Template:'+ id)
 // get the title element
 const title = box.querySelector('.title')
 title.appendChild(el)
}

document.querySelectorAll('.infobox,.navbar').forEach(add_editsection_link)

})

// determine if this page is a Cite: Arcanum page
function check_for_arcanum() {

const edit_box = document.querySelector('.editButtons');
// 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;

const entry = page_name.split('-')[1];
return 'https://wob.coppermind.net/api/entry/'+ entry +'/';
};
function build_from_json(json) {
console.log(json);
const tag = '<cite-info date="'+ (json.date || json.event_date) +'" event="'+ json.event +'">'+ json.event_name +'</cite-info>';
const page = tag +"<noinclude>\n\n== Users ==\n{{Special:WhatLinksHere/{{FULLPAGENAME}}|}}\n";
document.querySelector('#editform textarea').value = page;
};
/* auto-fill [[Cite: Arcanum-@entry]] pages*/
$(function () {
const url = check_for_arcanum()
fetch(url).then(function(resp) { return resp.json() }).then(build_from_json)
})