save code

This commit is contained in:
xsl
2026-05-24 12:35:35 +08:00
parent af3a23520d
commit b3384499e3
6 changed files with 121 additions and 167 deletions
+4 -2
View File
@@ -22,15 +22,17 @@ const TTS = {
return this._voices.find(v => v.lang.startsWith('zh') || v.name.includes('Chinese'))
|| this._voices[0] || null;
},
get available() { return !!window.speechSynthesis; },
speak(text, { rate = 0.85, volume = 0.8 } = {}) {
return new Promise(resolve => {
if (!window.speechSynthesis) { resolve(); return; }
if (!window.speechSynthesis) { resolve(false); return; }
speechSynthesis.cancel();
const u = new SpeechSynthesisUtterance(text);
u.lang = 'zh-CN'; u.rate = rate; u.volume = volume;
const v = this._pick();
if (v) u.voice = v;
u.onend = resolve; u.onerror = resolve;
u.onend = () => resolve(true);
u.onerror = () => resolve(false);
speechSynthesis.speak(u);
});
},