dictate-button
Version:
Dictate Button (Web Component)
38 lines (37 loc) • 1.91 kB
JavaScript
function d() {
const a = document.querySelectorAll(
'textarea[data-dictate-button-on]:not([data-dictate-button-enabled]), input[type="text"][data-dictate-button-on]:not([data-dictate-button-enabled]), textarea[data-dictate-button-target]:not([data-dictate-button-enabled]), input[type="text"][data-dictate-button-target]:not([data-dictate-button-enabled])'
);
for (const o of a) {
const n = document.createElement("div");
n.style.position = "relative", n.style.display = "inline-block", n.style.width = "auto", n.style.color = "inherit", o.parentNode.insertBefore(n, o), o.setAttribute("data-dictate-button-enabled", ""), n.appendChild(o), o.style.boxSizing = "border-box";
const t = document.createElement("dictate-button");
t.size = 24, t.style.position = "absolute", t.style.right = "0", t.style.top = "0";
const i = document.documentElement.lang;
i && i.length === 2 && (t.language = i), t.addEventListener("recording:started", (e) => {
console.log("recording:started", e);
}), t.addEventListener("recording:stopped", (e) => {
console.log("recording:stopped", e);
}), t.addEventListener("recording:failed", (e) => {
console.log("recording:failed", e);
}), t.addEventListener("transcribing:started", (e) => {
console.log("transcribing:started", e);
}), t.addEventListener("transcribing:finished", (e) => {
console.log("transcribing:finished", e);
const r = e.detail;
s(o, r);
}), t.addEventListener("transcribing:failed", (e) => {
console.log("transcribing:failed", e);
}), n.appendChild(t);
}
}
function s(a, o) {
const n = a.selectionStart || 0, t = a.selectionEnd || 0;
a.value = a.value.substring(0, n) + o + a.value.substring(t);
}
document.addEventListener("DOMContentLoaded", () => {
d(), new MutationObserver(d).observe(document.body, {
childList: !0,
subtree: !0
});
});