1 document.addEventListener('DOMContentLoaded', () => {
2 document.querySelectorAll('#search').forEach(p => {
3 let [input, button] = p.children;
4 button.onclick = e => {
5 if (input.value && input.offsetWidth > 50) {
6 return true; // bubble to submit
15 document.querySelectorAll('.multiinput > input[id]').forEach(el => {
17 if (e.target.value == '') return;
18 // insert another empty input element option
19 let add = e.target.cloneNode(true);
21 add.oninput = e.target.oninput;
22 e.target.parentNode.appendChild(add);
23 e.target.oninput = undefined;
24 e.target.removeAttribute('id');
28 let wpinput = document.getElementById('wptitle');
30 let wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
31 wpbutton.type = 'button';
32 wpbutton.append('Download');
33 wpbutton.onclick = () => {
34 let wptitle = wpinput.value || document.getElementById('form').value;
35 let wplang = document.getElementById('lang').value;
36 if (wplang == 'la') wplang = 'en'; // most likely presence of scientific names
37 let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
38 let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text|langlinks&page='+wptitle;
39 fetch(wppage).then(res => res.json()).then(json => {
40 if (json.error) throw `error returned: ${json.error.info}`;
41 wpinput.value = json.parse.title;
43 let wptext = json.parse.text['*'];
44 let transrow = document.getElementById('trans-la');
45 if (transrow && !transrow.value && wptext) {
46 const binom = wptext.match(/ class="binomial">.*?<i>(.*?)<\/i>/);
48 transrow.value = binom[1]
52 // translations from language links
53 let wplangs = json.parse.langlinks;
54 if (wplangs) wplangs.forEach(wptrans => {
55 let transrow = document.getElementById('trans-' + wptrans.lang);
56 if (!transrow || transrow.value) return;
57 transrow.value = wptrans['*'].replace(/([^,(]*).*/, (link, short) => {
58 return short.toLocaleLowerCase(wptrans.lang).trimEnd() + ' [' + link + ']';
62 // copy first paragraph to story
63 let storyinput = document.getElementById('story');
64 if (storyinput && !storyinput.value && wptext) {
65 storyinput.value = wptext
66 .replace(/<h2.*/s, '') // prefix
67 .replace(/<table.*?<\/table>/sg, '') // ignore infobox
68 .match(/<p>(.*?)<\/p>/s)[0] // first paragraph
69 .replace(/<[^>]*>/g, '') // strip html tags
72 // list images in article html
73 let imginput = document.getElementById('source');
74 if (!imginput || imginput.value) return;
75 let wpimages = wptext.match(/<img\s[^>]+>/g);
76 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
77 wpselect.className = 'popup';
78 wpimages.forEach(img => {
79 let selectitem = wpselect.appendChild(document.createElement('li'));
80 selectitem.insertAdjacentHTML('beforeend', img);
81 selectitem.onclick = e => {
82 let imgsrc = e.target.src
83 .replace(/^(?=\/\/)/, 'https:')
84 .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
85 imginput.value = imgsrc;
90 }).catch(error => alert(error));
93 wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
94 wpbutton.type = 'button';
95 wpbutton.append('Visit');
96 wpbutton.onclick = () => {
97 let wptitle = wpinput.value || document.getElementById('form').value;
98 let wplang = document.getElementById('lang').value;
100 wplang == 'la' ? `https://species.wikimedia.org/wiki/${wptitle}` :
101 `https://${wplang}.wikipedia.org/wiki/${wptitle}`;
102 window.open(wpurl, 'sheet-wikipedia').focus();
107 let imgpreview = document.getElementById('sourcepreview');
109 let imginput = document.getElementById('source');
110 imginput.parentNode.parentNode.append(imgpreview); // separate row
111 let previewbutton = imginput.parentNode.appendChild(document.createElement('button'));
112 previewbutton.type = 'button';
113 previewbutton.append('View');
114 previewbutton.onclick = () => {
115 previewbutton.childNodes[0].nodeValue = imgpreview.hidden ? 'Hide' : 'View';
116 imgpreview.hidden = !imgpreview.hidden;
120 let thumbpreview = document.getElementById('convertpreview');
121 if (thumbpreview && imgpreview) {
122 thumbpreview.onclick = e => {
123 thumbpreview.onclick = null; // setup once
124 const cropinput = document.getElementById('crop32');
125 const imgselect = thumbpreview.children[0];
126 const border = [300,200];
127 let crop = cropinput.value.split(/[^0-9]/).map(pos => pos / 1000);
128 let scale = 1 / (crop[2] - crop[0]) || 1;
129 crop.push(0); // defined y dimension
130 crop.splice(2); // end coordinates applied to zoom
131 crop = crop.map((rel, axis) => rel * border[axis % 2] * scale);
134 function applydrag(e) {
135 const touch = e.touches ? e.touches[0] : e;
136 let pos = [touch.pageX, touch.pageY];
137 if (e.type === 'touchmove' && e.touches.length > 1) {
138 // distance to second point
139 pos[0] -= e.touches[1].pageX;
140 pos[1] -= e.touches[1].pageY;
141 const span = Math.sqrt(pos[0]**2 + pos[1]**2);
143 cropzoom(.01 * (span - pinch));
149 // apply drag delta to crop position
150 crop[0] += drag[0] - pos[0];
151 crop[1] += drag[1] - pos[1];
158 [0, 1].forEach(axis => {
159 if (crop[axis] > border[axis] * (scale - 1)) {
160 crop[axis] = border[axis] * (scale - 1);
162 if (crop[axis] < 0) {
166 imgselect.style.left = -crop[0]+'px';
167 imgselect.style.top = -crop[1]+'px';
168 imgselect.style.width = (scale * 100)+'%';
170 crop[0] / border[0], crop[1] / border[1],
171 crop[0] / border[0] + 1, crop[1] / border[1] + 1,
172 ].map(pos => Math.round(1000 * pos / scale));
175 function cropzoom(delta) {
176 if (scale + delta < 1) {
177 delta = 1 - scale; // scale = 1
179 [0, 1].forEach(axis => {
180 // same area center at altered scale
181 crop[axis] += (crop[axis] + border[axis] / 2) / scale * delta;
187 imgselect.src = imgpreview.children[0].src;
188 imgselect.style.cursor = 'grab';
189 imgselect.style.position = 'absolute';
192 imgselect.ontouchstart =
193 imgselect.onmousedown = e => {
195 drag = pinch = false;
197 imgselect.style.cursor = 'grabbing';
199 window.onmousemove = e => {
204 window.onmouseup = e => {
206 imgselect.style.cursor = 'grab';
207 window.ontouchmove = window.ontouchend =
208 window.onmousemove = window.onmouseup = null;
212 imgselect.onwheel = e => {
214 let delta = (-e.deltaY || e.wheelDelta) * .001 * scale;
215 if (e.deltaMode == 1) { // DOM_DELTA_LINE
216 delta *= 18; // convert number of lines to pixels
223 let translist = document.getElementById('trans');
225 let langoptions = Array.prototype.filter.call(document.getElementById('lang').options, opt => {
226 if (document.getElementById('trans-' + opt.value)) return;
227 if (document.getElementById('lang').value == opt.value) return;
230 if (!langoptions.length) return;
232 let transadd = translist.appendChild(document.createElement('li'));
233 let transselect = transadd.appendChild(document.createElement('select'));
234 transselect.appendChild(document.createElement('option'));
235 for (let langoption of langoptions) {
236 let transoption = document.createElement('option');
237 transoption.value = langoption.value;
238 transoption.append(langoption.label);
239 transselect.appendChild(transoption);
241 transselect.onchange = e => {
242 let inputlang = e.target.selectedOptions[0];
243 let transadded = translist.insertBefore(document.createElement('li'), transadd);
244 let translabel = transadded.appendChild(document.createElement('label'));
245 translabel.append(inputlang.label.replace(/ (.+)/, ' ')); //TODO title = $1
246 let transinput = transadded.appendChild(document.createElement('input'));
247 transinput.name = 'trans-'+inputlang.value;
248 translabel.setAttribute('for', transinput.id = transinput.name);
250 if (e.target.length <= 1) e.target.remove();