Fix TTS in Safari

This commit is contained in:
Tetrakern 2023-07-09 12:08:34 +02:00
parent ce6a1034f6
commit f211fe4892
3 changed files with 13 additions and 13 deletions

View File

@ -88,7 +88,9 @@ if ( ! defined( 'FICTIONEER_SITE_DESCRIPTION' ) ) {
// String: TTS regex (used to split text into sentences)
if ( ! defined( 'FICTIONEER_TTS_REGEX' ) ) {
define( 'FICTIONEER_TTS_REGEX', '(?<=[.!?:"\'\u201C\u201D])\s+(?=[A-Z"\'\u201C\u201D])' );
// Note: Because lookbehind assertions do not work in Safari, the script uses a little
// detour to preserve punctuation marks: text.replace(regex, '$1|').split('|')
define( 'FICTIONEER_TTS_REGEX', '([.!?:"\'\u201C\u201D])\s+(?=[A-Z"\'\u201C\u201D])' );
}
/*

2
js/tts.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -95,8 +95,7 @@ function fcn_getTTSsettings() {
function fcn_setUpVoices() {
const systemVoices = fcn_synth.getVoices(),
select = _$$$('tts-voice-select'),
languages = [fcn_theRoot.lang];
select = _$$$('tts-voice-select');
if (!select) return;
@ -105,11 +104,6 @@ function fcn_setUpVoices() {
for (let i = 0; i < systemVoices.length; i++) {
const voice = systemVoices[i];
// Filter voices by set language
if (fcn_theRoot.lang == 'en-US') languages.push('en-GB');
if (fcn_theRoot.lang == 'en-GB') languages.push('en-US');
if (!languages.includes(voice.lang)) continue;
// Add voice to selection
fcn_voices.push(voice);
@ -279,7 +273,7 @@ if (typeof speechSynthesis !== 'undefined') {
const hideSensitive = _$('.chapter-formatting')?.classList.contains('hide-sensitive') ?? false,
sensitiveClass = hideSensitive ? 'sensitive-content' : 'sensitive-alternative',
playButton = _$$$('button-tts-play'),
regex = new RegExp(fcn_ttsInterface.dataset.regex, 'g');
regex = new RegExp(fcn_ttsInterface.dataset.regex, 'gm');
// Cancel ongoing reading if any
if (fcn_synth.speaking) fcn_utter.removeEventListener('end', fcn_readTextStack);
@ -290,9 +284,8 @@ if (typeof speechSynthesis !== 'undefined') {
fcn_ttsStack.push(node);
while (node = node.nextSibling) {
while (node = node.nextElementSibling) {
if (
node.nodeType == 1 &&
fcn_ttsAllowedTags.includes(node.tagName) &&
!node.classList.contains('skip-tts') &&
!node.classList.contains('inside-epub') &&
@ -309,7 +302,7 @@ if (typeof speechSynthesis !== 'undefined') {
text = inner ? inner.textContent : node.textContent;
// Split text into array of sentences using a regex pattern
const sentences = text.split(regex);
const sentences = text.replace(regex, '$1|').split('|');
sentences.forEach(sentence => {
const trimmedSentence = sentence.trim();
@ -415,4 +408,9 @@ if (typeof speechSynthesis !== 'undefined') {
_$$$('tts-settings-toggle')?.addEventListener('change', event => {
event.currentTarget.closest('#tts-interface').dataset.showSettings = event.currentTarget.checked;
});
// Terminate TTS on any chapter page reload (otherwise, it will keep running in the background)
window.addEventListener('beforeunload', () => {
fcn_synth.cancel();
});
}