From: Mischa POSLAWSKY Date: Fri, 29 May 2020 05:08:12 +0000 (+0200) Subject: word edit: separate javascript include X-Git-Tag: v1.13~245 X-Git-Url: http://git.shiar.nl/sheet.git/commitdiff_plain/0788c4247ca8ea24d8238a910a68efb9e7e3a04a word edit: separate javascript include --- diff --git a/writer.js b/writer.js new file mode 100644 index 0000000..450a8f2 --- /dev/null +++ b/writer.js @@ -0,0 +1,34 @@ +document.addEventListener('DOMContentLoaded', () => { + var wpinput = document.getElementById('wptitle'); + var wpbutton = wpinput.parentNode.appendChild(document.createElement('button')); + wpbutton.type = 'button'; + wpbutton.append('Copy'); + wpbutton.onclick = () => { + let wptitle = wpinput.value || document.getElementById('form').value; + let wplang = document.getElementById('lang').value.substr(0, 2); // crude iso-639-3→2 + let wpapi = `https://${wplang}.wikipedia.org/w/api.php`; + let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text&page='+wptitle; + fetch(wppage).then(res => res.json()).then(json => { + if (json.error) throw `error returned: ${json.error.info}`; + wpinput.value = json.parse.title; + let imginput = document.getElementById('source'); + if (imginput.value) return; + let wpimages = json.parse.text['*'].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; + }; +}); diff --git a/writer.plp b/writer.plp index b64fea0..2df9e55 100644 --- a/writer.plp +++ b/writer.plp @@ -85,42 +85,7 @@ h1 { } - + EOT });