word edit: wikipedia copy button
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 24 May 2020 07:22:56 +0000 (09:22 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 6 Jun 2020 01:49:20 +0000 (03:49 +0200)
Automatically find a matching title and return the first image from article
contents.

writer.plp

index 37424a8b65bf2cf00fc7c317a7fe1c4b1506ea55..24c5599635e0a19ca13cbe6dde820368cae7a9ee 100644 (file)
@@ -25,7 +25,34 @@ dd > img {
        max-width: 300px;
        display: block;
 }
+dd input ~ button {
+       margin-left: -5em;
+}
 </style>
+
+<script>
+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 wppage = 'https://en.wikipedia.org/w/api.php?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);
+                       imginput.value = wpimages[0].match(/\ssrc="([^"]+)"/)[1]
+                               .replace(/^(?=\/\/)/, 'https:')
+                               .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
+               }).catch(error => alert(error));
+               return false;
+       };
+});
+</script>
 EOT
 });
 
@@ -44,12 +71,12 @@ my $db = eval {
 
 my @wordcols = (
        form    => 'Translation',
+       wptitle => 'Wikipedia',
        ref     => 'Reference',
        cat     => 'Category',
        lang    => 'Language',
        source  => 'Image URL',
        thumb   => 'Convert options',
-       wptitle => 'Wikipedia',
 );
 my ($find) = map {{id => $_}} $fields{id} || $Request || ();