mirror of
https://github.com/rhasspy/espeak-ng.git
synced 2026-10-31 05:35:35 +00:00
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
onload = async () => {
|
|
chrome.runtime.sendNativeMessage(
|
|
'native_messaging_espeakng',
|
|
{},
|
|
async (nativeMessage) => {
|
|
parent.postMessage(nativeMessage, name);
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
const controller = new AbortController();
|
|
const { signal } = controller;
|
|
parent.postMessage('Ready.', name);
|
|
onmessage = async (e) => {
|
|
if (e.data instanceof ReadableStream) {
|
|
try {
|
|
const { value: file, done } = await e.data.getReader().read();
|
|
const fd = new FormData();
|
|
const stdin = await file.text();
|
|
fd.append(file.name, stdin);
|
|
const { body } = await fetch('http://localhost:8000', {
|
|
method: 'post',
|
|
cache: 'no-store',
|
|
credentials: 'omit',
|
|
body: fd,
|
|
signal,
|
|
});
|
|
parent.postMessage(body, name, [body]);
|
|
} catch (err) {
|
|
parent.postMessage(err, name);
|
|
}
|
|
} else {
|
|
if (e.data === 'Done writing input stream.') {
|
|
chrome.runtime.sendNativeMessage(
|
|
'native_messaging_espeakng',
|
|
{},
|
|
(nativeMessage) => {
|
|
parent.postMessage(nativeMessage, name);
|
|
}
|
|
);
|
|
}
|
|
if (e.data === 'Abort.') {
|
|
controller.abort();
|
|
}
|
|
}
|
|
};
|
|
}
|
|
);
|
|
};
|