Debian Codegraph @

src/selection-tools/wikipedia.html

raw
1 <!DOCTYPE html>
2 <html>
3 <meta charset="utf-8">
4 <meta name="viewport" content="width=device-width,initial-scale=1">
5 <link href="common.css" rel="stylesheet">
6 <style>
7 hgroup {
8 color: #fff;
9 background-position: center center;
10 background-size: cover;
11 background-color: rgba(0, 0, 0, .4);
12 background-blend-mode: darken;
13 border-radius: 6px;
14 padding: 12px;
15 margin: -8px;
16 margin-bottom: 0;
17 min-height: 100px;
18 }
19 </style>
20 <main></main>
21 <footer><p></footer>
22 <script>
23 const main = document.querySelector('main')
24 document.body.dataset.state = 'loading'
25
26 globalThis.init = ({ msg, text, lang }) => {
27 document.querySelector('footer p').innerHTML = msg.footer
28 fetch(`https://${lang}.wikipedia.org/api/rest_v1/page/summary/${text}`)
29 .then(res => res.ok ? res.json() : Promise.reject(new Error()))
30 .then(json => {
31 const hgroup = document.createElement('hgroup')
32 const h1 = document.createElement('h1')
33 h1.innerHTML = json.titles.display
34 hgroup.append(h1)
35 if (json.description) {
36 const p = document.createElement('p')
37 p.innerText = json.description
38 hgroup.append(p)
39 }
40 if (json.thumbnail)
41 hgroup.style.backgroundImage = 'url("' + json.thumbnail.source + '")'
42 const div = document.createElement('div')
43 div.innerHTML = json.extract_html
44 main.append(hgroup, div)
45 main.dir = json.dir
46 document.querySelector('#link').href = json.content_urls.desktop.page
47 document.body.dataset.state = 'loaded'
48 })
49 .catch(e => {
50 console.error(e)
51 const div = document.createElement('div')
52 const h1 = document.createElement('h1')
53 h1.innerText = msg.error
54 const p = document.createElement('p')
55 p.innerHTML = `<a href="https://${lang}.wikipedia.org/w/index.php?search=${text}">${msg.errorAction}</a>`
56 div.append(h1, p)
57 main.append(div)
58 document.body.dataset.state = 'error'
59 })
60 }
61 </script>