(.*?)<\/p>/s)[0] // first paragraph + .replace(/<[^>]*>/g, '') // strip html tags + } + + // list images in article html + let imginput = document.getElementById('source'); + if (!imginput || imginput.value) return; + let wpimages = wptext.match(/]+>/g); + let wpselect = wpinput.parentNode.appendChild(document.createElement('ul')); + wpselect.className = 'popup'; + wpimages.forEach(img => { + let selectitem = wpselect.appendChild(document.createElement('li')); + selectitem.insertAdjacentHTML('beforeend', img); + selectitem.onclick = e => { + let imgsrc = e.target.src + .replace(/^(?=\/\/)/, 'https:') + .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1'); + imginput.value = imgsrc; + wpselect.remove(); + return false; + }; + }); + }).catch(error => alert(error)); + return false; + }; + wpbutton = wpinput.parentNode.appendChild(document.createElement('button')); + wpbutton.type = 'button'; + wpbutton.append('Visit'); + wpbutton.onclick = () => { + let wptitle = wpinput.value || document.getElementById('form').value; + let wplang = document.getElementById('lang').value; + let wpurl = + wplang == 'la' ? `https://species.wikimedia.org/wiki/${wptitle}` : + `https://${wplang}.wikipedia.org/wiki/${wptitle}`; + window.open(wpurl, 'sheet-wikipedia').focus(); + return false; + }; + } let imgpreview = document.getElementById('sourcepreview'); if (imgpreview) { @@ -45,13 +102,42 @@ document.addEventListener('DOMContentLoaded', () => { }; } + let thumbpreview = document.getElementById('convertpreview'); + if (thumbpreview && imgpreview) { + thumbpreview.onclick = e => { + let imgselect = imgpreview; /* TODO clone */ + imgselect.hidden = false; + imgselect.classList.add('popup'); + imgselect.onmousemove = e => { + let border = imgselect.getBoundingClientRect(); + let pos = [ + Math.round(1000 * (e.clientX - border.x) / border.width), + Math.round(1000 * (e.clientY - border.y) / border.height) + ]; + return pos; + }; + imgselect.onclick = e => { + let imgoption = document.getElementById('convert'); + imgoption.value += (imgoption.value && '-') + imgselect.onmousemove(e); + imgselect.hidden = true; + imgselect.classList.remove('popup'); + }; + }; + } + let translist = document.getElementById('trans'); if (translist) { + let langoptions = Array.prototype.filter.call(document.getElementById('lang').options, opt => { + if (document.getElementById('trans-' + opt.value)) return; + if (document.getElementById('lang').value == opt.value) return; + return true; + }); + if (!langoptions.length) return; + let transadd = translist.appendChild(document.createElement('li')); let transselect = transadd.appendChild(document.createElement('select')); transselect.appendChild(document.createElement('option')); - for (let langoption of document.getElementById('lang').options) { - if (document.getElementById('trans-'+langoption.value)) continue; + for (let langoption of langoptions) { let transoption = document.createElement('option'); transoption.value = langoption.value; transoption.append(langoption.label);