1 document.addEventListener('DOMContentLoaded', () => {
2 document.querySelectorAll('.multiinput > input[id]').forEach(el => {
4 if (e.target.value == '') return;
5 let add = e.target.cloneNode(true);
7 add.oninput = e.target.oninput;
8 e.target.parentNode.appendChild(add);
9 e.target.oninput = undefined;
10 e.target.removeAttribute('id');
14 let wpinput = document.getElementById('wptitle');
16 let wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
17 wpbutton.type = 'button';
18 wpbutton.append('Download');
19 wpbutton.onclick = () => {
20 let wptitle = wpinput.value || document.getElementById('form').value;
21 let wplang = document.getElementById('lang').value;
22 if (wplang == 'la') wplang = 'en'; // most likely presence of scientific names
23 let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
24 let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text|langlinks&page='+wptitle;
25 fetch(wppage).then(res => res.json()).then(json => {
26 if (json.error) throw `error returned: ${json.error.info}`;
27 wpinput.value = json.parse.title;
29 // translations from language links
30 let wplangs = json.parse.langlinks;
31 if (wplangs) wplangs.forEach(wptrans => {
32 let transrow = document.getElementById('trans-' + wptrans.lang);
33 if (!transrow || transrow.value) return;
34 transrow.value = wptrans['*'].replace(/([^,(]*).*/, (link, short) => {
35 return short.toLocaleLowerCase(wptrans.lang).trimEnd() + ' [' + link + ']';
39 // copy first paragraph to story
40 let wptext = json.parse.text['*'];
41 let storyinput = document.getElementById('story');
42 if (storyinput && wptext) {
43 storyinput.innerHTML = wptext
44 .replace(/<h2.*/s, '') // prefix
45 .replace(/<table.*?<\/table>/sg, '') // ignore infobox
46 .match(/<p>(.*?)<\/p>/s)[0] // first paragraph
47 .replace(/<[^>]*>/g, '') // strip html tags
50 // list images in article html
51 let imginput = document.getElementById('source');
52 if (!imginput || imginput.value) return;
53 let wpimages = wptext.match(/<img\s[^>]+>/g);
54 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
55 wpselect.className = 'popup';
56 wpimages.forEach(img => {
57 let selectitem = wpselect.appendChild(document.createElement('li'));
58 selectitem.insertAdjacentHTML('beforeend', img);
59 selectitem.onclick = e => {
60 let imgsrc = e.target.src
61 .replace(/^(?=\/\/)/, 'https:')
62 .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
63 imginput.value = imgsrc;
68 }).catch(error => alert(error));
71 wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
72 wpbutton.type = 'button';
73 wpbutton.append('Visit');
74 wpbutton.onclick = () => {
75 let wptitle = wpinput.value || document.getElementById('form').value;
76 let wplang = document.getElementById('lang').value;
78 wplang == 'la' ? `https://species.wikimedia.org/wiki/${wptitle}` :
79 `https://${wplang}.wikipedia.org/wiki/${wptitle}`;
80 window.open(wpurl, 'sheet-wikipedia').focus();
85 let imgpreview = document.getElementById('sourcepreview');
87 let imginput = document.getElementById('source');
88 imginput.parentNode.parentNode.append(imgpreview); // separate row
89 let previewbutton = imginput.parentNode.appendChild(document.createElement('button'));
90 previewbutton.type = 'button';
91 previewbutton.append('View');
92 previewbutton.onclick = () => {
93 previewbutton.childNodes[0].nodeValue = imgpreview.hidden ? 'Hide' : 'View';
94 imgpreview.hidden = !imgpreview.hidden;
98 let translist = document.getElementById('trans');
100 let langoptions = Array.prototype.filter.call(document.getElementById('lang').options, opt => {
101 if (document.getElementById('trans-' + opt.value)) return;
102 if (document.getElementById('lang').value == opt.value) return;
105 if (!langoptions.length) return;
107 let transadd = translist.appendChild(document.createElement('li'));
108 let transselect = transadd.appendChild(document.createElement('select'));
109 transselect.appendChild(document.createElement('option'));
110 for (let langoption of langoptions) {
111 let transoption = document.createElement('option');
112 transoption.value = langoption.value;
113 transoption.append(langoption.label);
114 transselect.appendChild(transoption);
116 transselect.onchange = e => {
117 let inputlang = e.target.selectedOptions[0];
118 let transadded = translist.insertBefore(document.createElement('li'), transadd);
119 let translabel = transadded.appendChild(document.createElement('label'));
120 translabel.append(inputlang.label.replace(/ (.+)/, ' ')); //TODO title = $1
121 let transinput = transadded.appendChild(document.createElement('input'));
122 transinput.name = 'trans-'+inputlang.value;
123 translabel.setAttribute('for', transinput.id = transinput.name);
125 if (e.target.length <= 1) e.target.remove();