|
This JavaScript script puts a play button and a stop button ▶︎ ⏹︎ before elements with a class of "seatts". It uses the Web Speech API speechSynthesis interface in JavaScript. Features
Demonstration
This is a div with class="seatts". Try translating the website to another
language using the built-in translator tool in Chrome or Edge browser and see how this script detects the new language.
(Right click in the browser window and look for "Translate to...")
How To Use
Download Downloaded 0 times.
Please make a donation to help with server costs and other expenses.
SpeechSynthesis Testing Here you can test JavaScript SpeechSynthesis in your browser and see all the events that are dispatched (or not dispatched) with different browsers and different voices. SpeechSynthesis Info All the functions and variables are declared in a JavaScript object called "seatts". When the HTML document is ready the script will look for elements with class of "seatts" and add the play and stop button to them. It also uses a MutationObserver to see if an element has received a class of "seattts" and adds a play and stop button before the element. In addition, a play and stop button can be added to an element by calling the following function: seatts.add(element); When a user presses the play button the play button will change to a pause button ⏸︎ (⏸︎). If the pause button is pressed then the button will change to a play/pause or resume button ⏯︎ (⏯︎). If the stop button is pressed then the speechSynthesis will be stopped with the cancel() method and the play button will change to a play button ▶ (▶︎). The stop button can also be pressed with the ESC key on the keyboard. When the play button is pressed the script will determine what voice to use by several factors:
Instead of relying on the SpeechSynthesis.speak() queue the script will create its own sentence queue by splitting the text in the element into sentences using a regular expression like this: var sentence_re = /([\S\s]+?(?:[.?!;:—\|]|\n(?!\t)|$)(?:[\s\n]+|$|(?=[A-Z])))/g; // Match all sentences It will then replace \n with a space in all sentences because HTML documents sometimes have a line feed and the speechSynthesis should not treat that line feed as a new sentence. When one sentence (utterance) ends then the script will start the next utterance or sentence. Because of bugs in Chrome where it gets stuck sometimes, when the play button is pressed the script will always call speechSynthesis.cancel() before calling speechSynthesis.speak(); Google voices have a bug where they stop playing indefinitely if the utterance is over 14 seconds long unless the script calls pause() and resume(). So the script needs to detect if a voice starts with "Google" and if there is an utterance that is playing then it needs to call speechSynthesis.pause(); speechSynthesis.resume() every 12 seconds while it is playing. Edge browser often has bugs where some voices stop working. So if Edge returns a SpeechSynthesisErrorEvent: error: 'synthesis-failed' then Edge should show the error in the console and choose the next voice in the list of voices for the browser. Because of "pause" bugs in Chrome for desktop and Android, when the pause button is pressed the script should call speechSynthesis.cancel() but keep track of which sentence was paused in the queue. When the resume button is pressed then the script should start playing from that sentence in the queue. Last updated on July 5, 2026 |
|
|
User Comments
|