// ==UserScript==
// @name Hebrew Live Dictation - הקלדה קולית חיה
// @namespace https://local.example/hebrew-live-dictation
// @version 0.4.0
// @description Hebrew voice typing with clickable controls inside modal dialogs and concise voice punctuation commands.
// @match https://*/*
// @match http://localhost/*
// @match http://127.0.0.1/*
// @run-at document-idle
// @noframes
// @grant none
// ==/UserScript==
(() => {
'use strict';
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition || document.getElementById('hebrew-voice-dictation')) return;
let target = null;
let recognition = null;
let wantsToListen = false;
let isRunning = false;
let isApplyingText = false;
let finalizedIndex = 0;
let draft = null; // {kind, element, start, end, range, text}
let restartTimer = null;
let overlapFinalSpeech = '';
let overlapChecksRemaining = 0;
let echoResultIndex = -1;
// Isolate the floating controls from the site's CSS.
const host = document.createElement('div');
host.id = 'hebrew-voice-dictation';
// Popover API puts the microphone in the browser's top layer, above ordinary
// overlays and modal backdrops (unlike z-index alone).
host.setAttribute('popover', 'manual');
host.style.cssText = 'all:initial;display:block;position:fixed;top:auto;left:auto;right:20px;bottom:20px;z-index:2147483647;margin:0;padding:0;border:0;background:transparent;overflow:visible;pointer-events:auto;';
const root = host.attachShadow({mode: 'open'});
root.innerHTML = `
`;
document.documentElement.appendChild(host);
try {
if (typeof host.showPopover === 'function') host.showPopover();
} catch (_) { /* Keep the high-z-index fallback if Popover API is unavailable. */ }
const button = root.querySelector('button');
const tip = root.querySelector('.tip');
let infoTimer = null;
function showInfo(message, hideAfter = 3500) {
tip.textContent = message;
tip.classList.add('show');
clearTimeout(infoTimer);
if (hideAfter) infoTimer = setTimeout(() => tip.classList.remove('show'), hideAfter);
}
function updateButton() {
button.classList.toggle('on', wantsToListen);
button.setAttribute('aria-label', wantsToListen ? 'הפסק הכתבה קולית' : 'התחל הכתבה קולית');
button.textContent = wantsToListen ? '⏹' : '🎙️';
}
function isTextField(el) {
if (!el || !(el instanceof HTMLElement)) return false;
if (el instanceof HTMLTextAreaElement) return !el.disabled && !el.readOnly;
if (el instanceof HTMLInputElement) {
return ['text', 'search', 'email', 'url', 'tel'].includes(el.type)
&& !el.disabled && !el.readOnly;
}
if (el.getAttribute('role') === 'textbox') {
return el.getAttribute('aria-disabled') !== 'true'
&& el.getAttribute('aria-readonly') !== 'true';
}
return el.isContentEditable;
}
function findEditor(from) {
if (!(from instanceof Element)) return null;
const field = from.closest('textarea,input,[contenteditable=""],[contenteditable="true"],[role="textbox"]');
if (!field || !isTextField(field)) return null;
// For nested contenteditables, find the nearest editable root.
if (field.isContentEditable && !(field instanceof HTMLInputElement) && !(field instanceof HTMLTextAreaElement)) {
let current = field;
while (current.parentElement && current.parentElement.isContentEditable) current = current.parentElement;
return current;
}
return field;
}
// Search the composed event path as well as event.target. This also detects
// inputs inside a site's open Shadow DOM.
function findEditorFromEvent(event) {
const path = typeof event.composedPath === 'function' ? event.composedPath() : [event.target];
for (const node of path) {
const found = findEditor(node);
if (found) return found;
}
return findEditor(event.target);
}
let raiseTimer = null;
function containingModal(element) {
// Native modal