| 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 |
html, body { |
| 8 |
margin: 0; |
| 9 |
height: 100%; |
| 10 |
width: 100%; |
| 11 |
} |
| 12 |
body { |
| 13 |
display: flex; |
| 14 |
flex-direction: column; |
| 15 |
gap: 9px; |
| 16 |
padding: 6px; |
| 17 |
} |
| 18 |
select { |
| 19 |
width: 100%; |
| 20 |
} |
| 21 |
main { |
| 22 |
flex: 1; |
| 23 |
overflow-y: auto; |
| 24 |
} |
| 25 |
button { |
| 26 |
display: flex; |
| 27 |
border-radius: 6px; |
| 28 |
padding: 6px; |
| 29 |
} |
| 30 |
input, button { |
| 31 |
background: none; |
| 32 |
border: none; |
| 33 |
color: inherit; |
| 34 |
font: inherit; |
| 35 |
&[hidden] { |
| 36 |
display: none; |
| 37 |
} |
| 38 |
} |
| 39 |
dialog { |
| 40 |
inset: 0; |
| 41 |
width: 100%; |
| 42 |
height: 100%; |
| 43 |
max-width: 100%; |
| 44 |
max-height: 100%; |
| 45 |
border: 0; |
| 46 |
margin: 0; |
| 47 |
padding: 0; |
| 48 |
overflow: hidden; |
| 49 |
display: flex; |
| 50 |
flex-direction: column; |
| 51 |
&:not([open]) { |
| 52 |
display: none; |
| 53 |
} |
| 54 |
input { |
| 55 |
top: 0; |
| 56 |
left: 0; |
| 57 |
right: 0; |
| 58 |
padding: 12px; |
| 59 |
padding-top: 6px; |
| 60 |
border-bottom: 1px solid color-mix(in srgb, currentcolor 15%, transparent); |
| 61 |
outline: 0; |
| 62 |
width: 100%; |
| 63 |
} |
| 64 |
form { |
| 65 |
padding: 6px; |
| 66 |
display: flex; |
| 67 |
flex-direction: column; |
| 68 |
gap: 2px; |
| 69 |
overflow-y: auto; |
| 70 |
} |
| 71 |
button { |
| 72 |
text-align: start; |
| 73 |
&:hover { |
| 74 |
background: color-mix(in srgb, currentcolor 3%, transparent); |
| 75 |
} |
| 76 |
&:active, &[aria-selected="true"] { |
| 77 |
background: color-mix(in srgb, currentcolor 7%, transparent); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
</style> |
| 82 |
<header> |
| 83 |
<button style="width: 100%; background: color-mix(in srgb, currentcolor 7%, transparent)"> |
| 84 |
<span style="text-align: start; flex: 1"></span> |
| 85 |
<foliate-symbolic src="/icons/hicolor/scalable/actions/pan-down-symbolic.svg"></foliate-symbolic> |
| 86 |
</button> |
| 87 |
</header> |
| 88 |
<main><div id="output"></div></main> |
| 89 |
<footer>asdf</footer> |
| 90 |
<dialog> |
| 91 |
<input type="search"> |
| 92 |
<form method="dialog" role="listbox"></form> |
| 93 |
</dialog> |
| 94 |
<script src="../common/widgets.js" type="module"></script> |
| 95 |
<script> |
| 96 |
const googleTranslate = (text, lang) => |
| 97 |
fetch('https://translate.googleapis.com/translate_a/single?client=gtx' |
| 98 |
+ '&ie=UTF-8&oe=UTF-&sl=auto&tl=' + lang |
| 99 |
+ '&dt=t&q=' + text) |
| 100 |
.then(res => res.ok ? res.json() : Promise.reject(new Error())) |
| 101 |
.then(json => json[0].map(x => x[0]).join('')) |
| 102 |
|
| 103 |
const manageFocus = items => { |
| 104 |
const setFocusPrev = el => |
| 105 |
setFocusNext(el, items.slice(0).reverse()) |
| 106 |
const setFocusNext = (el, items) => { |
| 107 |
let justFound, found |
| 108 |
for (const item of items) { |
| 109 |
if (justFound && !item.hidden) { |
| 110 |
item.tabIndex = 0 |
| 111 |
item.focus() |
| 112 |
found = true |
| 113 |
justFound = false |
| 114 |
} |
| 115 |
else { |
| 116 |
item.tabIndex = -1 |
| 117 |
if (item === el) justFound = true |
| 118 |
} |
| 119 |
} |
| 120 |
if (!found) { |
| 121 |
items[0].tabIndex = 0 |
| 122 |
items[0].focus() |
| 123 |
} |
| 124 |
} |
| 125 |
return e => { |
| 126 |
switch (e.key) { |
| 127 |
case 'ArrowUp': |
| 128 |
e.preventDefault() |
| 129 |
e.stopPropagation() |
| 130 |
setFocusPrev(e.target) |
| 131 |
break |
| 132 |
case 'ArrowDown': |
| 133 |
e.preventDefault() |
| 134 |
e.stopPropagation() |
| 135 |
setFocusNext(e.target, items) |
| 136 |
break |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
document.querySelector('header button').addEventListener('click', () => { |
| 142 |
document.querySelector('dialog').showModal() |
| 143 |
document.querySelector('[aria-selected="true"]').scrollIntoView({ block: 'center' }) |
| 144 |
const input = document.querySelector('input') |
| 145 |
input.value = '' |
| 146 |
input.dispatchEvent(new Event('input')) |
| 147 |
input.focus() |
| 148 |
}) |
| 149 |
|
| 150 |
document.querySelector('input').addEventListener('keydown', e => { |
| 151 |
if (e.key === 'Escape') document.querySelector('dialog').close() |
| 152 |
}) |
| 153 |
document.querySelector('input').addEventListener('input', e => { |
| 154 |
const { value } = e.target |
| 155 |
const els = [...document.querySelectorAll('dialog form button')] |
| 156 |
if (value) { |
| 157 |
const lower = value.toLowerCase() |
| 158 |
for (const el of els) { |
| 159 |
el.hidden = !el.textContent.toLowerCase().startsWith(lower) |
| 160 |
el.tabIndex = -1 |
| 161 |
} |
| 162 |
const first = document.querySelector('dialog button:not([hidden])') |
| 163 |
if (first) first.tabIndex = 0 |
| 164 |
document.querySelector('dialog form').scrollTop = 0 |
| 165 |
} |
| 166 |
else { |
| 167 |
for (const el of els) { |
| 168 |
el.hidden = false |
| 169 |
el.tabIndex = -1 |
| 170 |
} |
| 171 |
const el = document.querySelector('[aria-selected="true"]') |
| 172 |
el.tabIndex = 0 |
| 173 |
el.scrollIntoView({ block: 'center' }) |
| 174 |
} |
| 175 |
}) |
| 176 |
|
| 177 |
let msgError |
| 178 |
const translate = (text, lang) => googleTranslate(text, lang) |
| 179 |
.then(result => document.querySelector('#output').innerText = result) |
| 180 |
.catch(e => { |
| 181 |
console.error(e) |
| 182 |
document.querySelector('#output').innerText = msgError |
| 183 |
}) |
| 184 |
|
| 185 |
globalThis.init = ({ msg, text, lang }) => { |
| 186 |
msgError = msg.error |
| 187 |
document.querySelector('footer').textContent = msg.footer |
| 188 |
document.querySelector('input').placeholder = msg.search |
| 189 |
|
| 190 |
const options = msg.langs.map(([code, label]) => { |
| 191 |
const option = document.createElement('button') |
| 192 |
option.role = 'option' |
| 193 |
option.ariaSelected = code === lang ? 'true' : 'false' |
| 194 |
option.tabIndex = code === lang ? 0 : -1 |
| 195 |
option.textContent = label |
| 196 |
option.value = code |
| 197 |
return option |
| 198 |
}) |
| 199 |
const form = document.querySelector('form') |
| 200 |
form.replaceChildren(...options) |
| 201 |
form.addEventListener('keydown', manageFocus(options)) |
| 202 |
form.addEventListener('submit', e => { |
| 203 |
const { submitter } = e |
| 204 |
for (const option of options) option.ariaSelected = option === submitter |
| 205 |
document.querySelector('header button span').textContent = submitter.textContent |
| 206 |
webkit.messageHandlers.settings.postMessage(JSON.stringify({ |
| 207 |
key: 'translate-target-language', |
| 208 |
value: submitter.value, |
| 209 |
})) |
| 210 |
translate(text, submitter.value) |
| 211 |
}) |
| 212 |
document.querySelector('header button span').textContent = |
| 213 |
form.querySelector(`[value="${CSS.escape(lang)}"]`).textContent |
| 214 |
|
| 215 |
translate(text, lang) |
| 216 |
} |
| 217 |
</script> |