1 document.addEventListener('DOMContentLoaded', () => {
2 document.querySelectorAll('.multiinput > input[id]').forEach(el => {
4 if (e.target.value == '') return;
5 // insert another empty input element option
6 let add = e.target.cloneNode(true);
8 add.oninput = e.target.oninput;
9 e.target.parentNode.appendChild(add);
10 e.target.oninput = undefined;
11 e.target.removeAttribute('id');
15 let wpinput = document.getElementById('wptitle');
17 let wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
18 wpbutton.type = 'button';
19 wpbutton.append('Download');
20 wpbutton.onclick = () => {
21 let wptitle = wpinput.value || document.getElementById('form').value;
22 let wplang = document.getElementById('lang').value;
23 if (wplang == 'la') wplang = 'en'; // most likely presence of scientific names
24 let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
25 let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text|langlinks&page='+wptitle;
26 fetch(wppage).then(res => res.json()).then(json => {
27 if (json.error) throw `error returned: ${json.error.info}`;
28 wpinput.value = json.parse.title;
30 let wptext = json.parse.text['*'];
31 let transrow = document.getElementById('trans-la');
32 if (transrow && !transrow.value && wptext) {
33 const binom = wptext.match(/ class="binomial">.*?<i>(.*?)<\/i>/);
34 transrow.value = binom[1]
37 // translations from language links
38 let wplangs = json.parse.langlinks;
39 if (wplangs) wplangs.forEach(wptrans => {
40 let transrow = document.getElementById('trans-' + wptrans.lang);
41 if (!transrow || transrow.value) return;
42 transrow.value = wptrans['*'].replace(/([^,(]*).*/, (link, short) => {
43 return short.toLocaleLowerCase(wptrans.lang).trimEnd() + ' [' + link + ']';
47 // copy first paragraph to story
48 let storyinput = document.getElementById('story');
49 if (storyinput && !storyinput.value && wptext) {
50 storyinput.value = wptext
51 .replace(/<h2.*/s, '') // prefix
52 .replace(/<table.*?<\/table>/sg, '') // ignore infobox
53 .match(/<p>(.*?)<\/p>/s)[0] // first paragraph
54 .replace(/<[^>]*>/g, '') // strip html tags
57 // list images in article html
58 let imginput = document.getElementById('source');
59 if (!imginput || imginput.value) return;
60 let wpimages = wptext.match(/<img\s[^>]+>/g);
61 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
62 wpselect.className = 'popup';
63 wpimages.forEach(img => {
64 let selectitem = wpselect.appendChild(document.createElement('li'));
65 selectitem.insertAdjacentHTML('beforeend', img);
66 selectitem.onclick = e => {
67 let imgsrc = e.target.src
68 .replace(/^(?=\/\/)/, 'https:')
69 .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
70 imginput.value = imgsrc;
75 }).catch(error => alert(error));
78 wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
79 wpbutton.type = 'button';
80 wpbutton.append('Visit');
81 wpbutton.onclick = () => {
82 let wptitle = wpinput.value || document.getElementById('form').value;
83 let wplang = document.getElementById('lang').value;
85 wplang == 'la' ? `https://species.wikimedia.org/wiki/${wptitle}` :
86 `https://${wplang}.wikipedia.org/wiki/${wptitle}`;
87 window.open(wpurl, 'sheet-wikipedia').focus();
92 let imgpreview = document.getElementById('sourcepreview');
94 let imginput = document.getElementById('source');
95 imginput.parentNode.parentNode.append(imgpreview); // separate row
96 let previewbutton = imginput.parentNode.appendChild(document.createElement('button'));
97 previewbutton.type = 'button';
98 previewbutton.append('View');
99 previewbutton.onclick = () => {
100 previewbutton.childNodes[0].nodeValue = imgpreview.hidden ? 'Hide' : 'View';
101 imgpreview.hidden = !imgpreview.hidden;
105 let thumbpreview = document.getElementById('convertpreview');
106 if (thumbpreview && imgpreview) {
107 thumbpreview.onclick = e => {
108 let imgselect = imgpreview; /* TODO clone */
109 imgselect.hidden = false;
110 imgselect.classList.add('popup');
111 imgselect.onmousemove = e => {
112 let border = imgselect.getBoundingClientRect();
114 Math.round(1000 * (e.clientX - border.x) / border.width),
115 Math.round(1000 * (e.clientY - border.y) / border.height)
119 imgselect.onclick = e => {
120 let imgoption = document.getElementById('convert');
121 imgoption.value += (imgoption.value && '-') + imgselect.onmousemove(e);
122 imgselect.hidden = true;
123 imgselect.classList.remove('popup');
128 let translist = document.getElementById('trans');
130 let langoptions = Array.prototype.filter.call(document.getElementById('lang').options, opt => {
131 if (document.getElementById('trans-' + opt.value)) return;
132 if (document.getElementById('lang').value == opt.value) return;
135 if (!langoptions.length) return;
137 let transadd = translist.appendChild(document.createElement('li'));
138 let transselect = transadd.appendChild(document.createElement('select'));
139 transselect.appendChild(document.createElement('option'));
140 for (let langoption of langoptions) {
141 let transoption = document.createElement('option');
142 transoption.value = langoption.value;
143 transoption.append(langoption.label);
144 transselect.appendChild(transoption);
146 transselect.onchange = e => {
147 let inputlang = e.target.selectedOptions[0];
148 let transadded = translist.insertBefore(document.createElement('li'), transadd);
149 let translabel = transadded.appendChild(document.createElement('label'));
150 translabel.append(inputlang.label.replace(/ (.+)/, ' ')); //TODO title = $1
151 let transinput = transadded.appendChild(document.createElement('input'));
152 transinput.name = 'trans-'+inputlang.value;
153 translabel.setAttribute('for', transinput.id = transinput.name);
155 if (e.target.length <= 1) e.target.remove();