Source code

Revision control

Copy as Markdown

Other Tools

<!DOCTYPE html>
<html>
<head>
<title>Web Speech audibility page</title>
</head>
<body>
<button id="start">start</button>
<script>
let utt;
let speechStarted = false;
document.getElementById("start").addEventListener("click", () => {
utt = new SpeechSynthesisUtterance("audio focus competition test");
// Pick the test fake-synth voice that fires "start" but never auto-fires
// "end", so the utterance stays audible across audio-focus competition
// until the test explicitly cancels it. See dom/media/webspeech/synth/test/
// nsFakeSynthServices.cpp (the "it-IT-noend" / "teresa" voice carries
// eSuppressEnd).
utt.lang = "it-IT-noend";
speechStarted = false;
utt.addEventListener("start", () => {
speechStarted = true;
});
speechSynthesis.speak(utt);
});
// Poll the observable state rather than waiting on a one-shot event: the
// start/pause/resume transition can land before the test attaches a listener,
// and a missed one-shot event would hang the spawn (and leak the tab).
function waitForState(predicate) {
return new Promise(resolve => {
if (predicate()) {
resolve(true);
return;
}
const timer = setInterval(() => {
if (predicate()) {
clearInterval(timer);
resolve(true);
}
}, 50);
});
}
window.waitForSpeechStart = () => waitForState(() => speechStarted);
window.waitForSpeechPause = () =>
waitForState(() => !!utt && speechSynthesis.paused);
window.waitForSpeechResume = () =>
waitForState(() => !!utt && speechStarted && !speechSynthesis.paused);
window.cancelSpeech = () => {
if (utt) {
speechSynthesis.cancel();
utt = null;
}
};
</script>
</body>
</html>