word edit: visit button to open linked wikipedia page
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 30 May 2020 22:22:44 +0000 (00:22 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 6 Jun 2020 01:49:20 +0000 (03:49 +0200)
writer.js

index 714884d242525d16591ce767952d38a32ead77c4..e9c81ef37447275740312fccc9264c26c206dd8d 100644 (file)
--- a/writer.js
+++ b/writer.js
@@ -1,36 +1,48 @@
 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(/<img\s[^>]+>/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;
-       };
+       let wpinput = document.getElementById('wptitle');
+       if (wpinput) {
+               let wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
+               wpbutton.type = 'button';
+               wpbutton.append('Download');
+               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(/<img\s[^>]+>/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.substr(0, 2); // crude iso-639-3→2
+                       let wpurl = `https://${wplang}.wikipedia.org/wiki/${wptitle}`;
+                       window.open(wpurl, 'sheet-wikipedia').focus();
+                       return false;
+               };
+       }
 
        let imgpreview = document.getElementById('sourcepreview');
        if (imgpreview) {