Debian Codegraph @

src/selection-tools/wiktionary.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 <base href="https://en.wiktionary.org/wiki/Wiktionary:Main_Page">
7 <style>
8 ul {
9 margin: .5em 0;
10 font-style: italic;
11 opacity: .75;
12 font-size: smaller;
13 list-style: none;
14 }
15 h1 {
16 padding-inline-end: 1em;
17 display: inline;
18 }
19 hgroup p {
20 font-size: smaller;
21 display: inline;
22 }
23 </style>
24 <main></main>
25 <footer><p></footer>
26 <script>
27 let msgError, msgErrorAction
28 const main = document.querySelector('main')
29 const footer = document.querySelector('footer')
30 const wiktionary = (word, language, languageName) => {
31 document.body.dataset.state = 'loading'
32 return fetch(`https://en.wiktionary.org/api/rest_v1/page/definition/${word}`)
33 .then(res => res.ok ? res.json() : Promise.reject(new Error()))
34 .then(json => {
35 const results = language ? json[language]
36 : languageName ? Object.values(json)
37 .find(x => x.some(x => x.language === languageName))
38 : json['en']
39 const hgroup = document.createElement('hgroup')
40 const h1 = document.createElement('h1')
41 h1.innerText = word
42 const p = document.createElement('p')
43 p.innerText = results[0].language
44 hgroup.append(h1, p)
45 main.append(hgroup)
46 for (const { partOfSpeech, definitions } of results) {
47 const h2 = document.createElement('h2')
48 h2.innerText = partOfSpeech
49 const ol = document.createElement('ol')
50 main.append(h2, ol)
51 for (const { definition, examples } of definitions) {
52 const li = document.createElement('li')
53 li.innerHTML = definition
54 ol.append(li)
55 const ul = document.createElement('ul')
56 li.append(ul)
57 if (examples) for (const example of examples) {
58 const li = document.createElement('li')
59 li.innerHTML = example
60 ul.append(li)
61 }
62 }
63 }
64 document.querySelector('#link').href = '/wiki/' + word
65 document.body.dataset.state = 'loaded'
66 })
67 .catch(e => {
68 console.error(e)
69 const lower = word.toLocaleLowerCase(language)
70 if (lower !== word) return wiktionary(lower, language)
71 else {
72 const div = document.createElement('div')
73 const h1 = document.createElement('h1')
74 h1.innerText = msgError
75 const p = document.createElement('p')
76 p.innerHTML = `<a href="https://en.wiktionary.org/w/index.php?search=${encodeURIComponent(word)}">${msgErrorAction}</a>`
77 div.append(h1, p)
78 main.append(div)
79 document.body.dataset.state = 'error'
80 }
81 })
82 }
83
84 // see https://en.wiktionary.org/wiki/Wiktionary:Namespace
85 const wikiNamespaces = [
86 'Media', 'Special', 'Talk', 'User', 'Wiktionary', 'File', 'MediaWiki',
87 'Template', 'Help', 'Category',
88 'Summary', 'Appendix', 'Concordance', 'Index', 'Rhymes', 'Transwiki',
89 'Thesaurus', 'Citations', 'Sign',
90 ]
91
92 main.addEventListener('click', e => {
93 const { target } = e
94 if (target.tagName === 'A') {
95 const href = target.getAttribute('href')
96 if (href.startsWith('/wiki/')) {
97 const [word, languageName] = href.replace('/wiki/', '').split('#')
98 if (wikiNamespaces.every(namespace => !word.startsWith(namespace + ':')
99 && !word.startsWith(namespace + '_talk:'))) {
100 e.preventDefault()
101 main.replaceChildren()
102 wiktionary(word.replaceAll('_', ' '), null, languageName)
103 }
104 }
105 }
106 })
107
108 globalThis.init = ({ msg, text, lang }) => {
109 document.querySelector('footer').innerHTML = msg.footer
110 msgError = msg.error
111 msgErrorAction = msg.errorAction
112 wiktionary(text, lang)
113 }
114 </script>