-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtts-integration.js
More file actions
69 lines (63 loc) · 2.11 KB
/
tts-integration.js
File metadata and controls
69 lines (63 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Lightweight TTS integration for Caption.Ninja pages
(function () {
var urlParams = new URLSearchParams(window.location.search);
var enableTTS = urlParams.has('tts') || urlParams.has('speech') || urlParams.has('speak');
var ttsStream = urlParams.has('ttsstream');
function loadScript(src) {
return new Promise(function (resolve, reject) {
var s = document.createElement('script');
s.src = src;
s.async = true;
s.onload = resolve;
s.onerror = reject;
document.head.appendChild(s);
});
}
async function init() {
if (!enableTTS) {
return; // No-op unless explicitly enabled via URL
}
try {
// Load the TTS library; prefer local path, then absolute fallback
try {
await loadScript('tts.rocks/tts.js');
} catch (e) {
await loadScript('https://tts.rocks/tts.js');
}
// Configure using current URL params if available
if (window.TTS && typeof window.TTS.configure === 'function') {
window.TTS.configure(urlParams);
} else {
// Fallback minimal config
window.TTS = window.TTS || {};
window.TTS.speech = true;
}
// If a capture language is known, prefer it when none specified
try {
if (window.myLang && (!window.TTS.speechLang || window.TTS.speechLang === 'en-US')) {
window.TTS.speechLang = window.myLang;
}
} catch (e) {}
// Expose a small helper API for pages to call
window.CAPTION_TTS = {
speak: function (text, allow) {
try {
if (!text) return;
if (!window.TTS || typeof window.TTS.speak !== 'function') return;
window.TTS.speak(String(text), !!allow);
} catch (e) {
console.warn('TTS speak failed', e);
}
},
stream: !!ttsStream
};
} catch (e) {
console.warn('Failed to initialize TTS integration', e);
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();