From 99d7950afd67671e494c30859d76661c78f5e801 Mon Sep 17 00:00:00 2001 From: harold Date: Mon, 28 Apr 2025 22:54:27 +0500 Subject: [PATCH] add fix' --- internal/model/widget-templates.go | 112 ++++++++++------------------- 1 file changed, 39 insertions(+), 73 deletions(-) diff --git a/internal/model/widget-templates.go b/internal/model/widget-templates.go index 1ede472..9374005 100644 --- a/internal/model/widget-templates.go +++ b/internal/model/widget-templates.go @@ -86,63 +86,21 @@ async function getDonatInfo(streamerID) { } } -function addImage(container, imageUrl) { - const img = document.createElement('img'); - img.src = imageUrl + '?t=' + new Date().getTime(); - img.onload = () => { - const aspectRatio = img.naturalWidth / img.naturalHeight; - if (aspectRatio > 1) { - img.style.width = '100%%'; - img.style.height = 'auto'; - } else { - img.style.width = 'auto'; - img.style.height = '100%%'; - } - }; - container.appendChild(img); -} +function playSpeech(text, voiceSettings) { + if (!voiceSettings.voice_enabled) return; -function addText(container, text) { - const p = document.createElement('p'); - p.className = 'donation-text'; - p.textContent = text; - container.appendChild(p); -} + const params = new URLSearchParams({ + text: text, + speed: voiceSettings.voice_speed || 'medium', + scenery: voiceSettings.scenery || 'default', + sound_percent: voiceSettings.voice_sound_percent || 100, + min_price: voiceSettings.min_price || 0, + languages: voiceSettings.languages?.join(',') || '' + }); -function clearContainer(container) { - while (container.firstChild) { - container.removeChild(container.firstChild); - } -} - -function playAudio(audioUrl, callback) { - fetch(audioUrl) - .then(response => { - if (!response.ok) throw new Error('Audio network error'); - return response.blob(); - }) - .then(blob => { - const url = URL.createObjectURL(blob); - const audio = new Audio(url); - - audio.play().catch(console.error); - setTimeout(() => { - audio.pause(); - URL.revokeObjectURL(url); - if (callback) callback(); - }, 7000); - }) - .catch(console.error); -} - -function playSpeech(text) { - fetch(ttsUrl + '/generate', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ text: text }), - }) + const url = ttsUrl + '/generate?' + params.toString(); + + fetch(url) .then(response => { if (!response.ok) throw new Error('TTS error'); return response.blob(); @@ -158,14 +116,14 @@ function playSpeech(text) { .catch(console.error); } -function playSpeechAfterAudio(audioUrl, text) { - playAudio(audioUrl, () => playSpeech(text)); +function playSpeechAfterAudio(audioUrl, text, voiceSettings) { + playAudio(audioUrl, () => playSpeech(text, voiceSettings)); } async function widgetView() { const streamerID = '%v'; const contentDiv = document.getElementById('content'); - const REQUEST_INTERVAL = 5000; // Фиксированный интервал 5 секунд + const REQUEST_INTERVAL = 5000; if (!contentDiv) { console.error('Content container not found!'); @@ -173,12 +131,11 @@ async function widgetView() { } while (true) { - const iterationStart = Date.now(); // Замер времени начала итерации + const iterationStart = Date.now(); try { const donat = await getDonatInfo(streamerID); - console.log('Donat received:', donat); - + if (!donat || Object.keys(donat).length === 0) { await new Promise(r => setTimeout(r, 5000)); continue; @@ -186,27 +143,36 @@ async function widgetView() { clearContainer(contentDiv); - // Добавление элементов в DOM + // Добавление элементов if (donat.image_link) { addImage(contentDiv, donat.image_link); } - // Создание текста с суммой - if (donat.text && donat.amount) { - const textWithAmount = createTextWithAmount(donat.text, donat.amount); - contentDiv.appendChild(textWithAmount); - } else if (donat.text) { - addText(contentDiv, donat.text); + // Текст с суммой + if (donat.text) { + const textElement = donat.amount + ? createTextWithAmount(donat.text, donat.amount) + : createTextElement(donat.text); + contentDiv.appendChild(textElement); } - // Воспроизведение аудио + // Воспроизведение аудио и TTS + const voiceSettings = { + voice_speed: donat.voice_speed, + scenery: donat.scenery, + voice_sound_percent: donat.voice_sound_percent, + min_price: donat.min_price, + languages: donat.languages, + voice_enabled: donat.voice_enabled + }; + if (donat.audio_link) { - playSpeechAfterAudio(donat.audio_link, donat.text); - } else if (donat.text) { - playSpeech(donat.text); + playSpeechAfterAudio(donat.audio_link, donat.text, voiceSettings); + } else if (donat.text && donat.voice_enabled) { + playSpeech(donat.text, voiceSettings); } - // Ожидаем указанную длительность + // Таймаут на основе duration await new Promise(r => setTimeout(r, donat.duration * 1000)); // Отправка подтверждения просмотра