From d1c536acbc7cea5c9c2a6105d7c90aa856878e37 Mon Sep 17 00:00:00 2001 From: harold Date: Fri, 18 Apr 2025 18:41:16 +0500 Subject: [PATCH] add fix for widget template --- internal/model/widget-templates.go | 33 ++++++++++++++++++------------ 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/internal/model/widget-templates.go b/internal/model/widget-templates.go index 838e65a..1ede472 100644 --- a/internal/model/widget-templates.go +++ b/internal/model/widget-templates.go @@ -165,6 +165,7 @@ function playSpeechAfterAudio(audioUrl, text) { async function widgetView() { const streamerID = '%v'; const contentDiv = document.getElementById('content'); + const REQUEST_INTERVAL = 5000; // Фиксированный интервал 5 секунд if (!contentDiv) { console.error('Content container not found!'); @@ -172,6 +173,8 @@ async function widgetView() { } while (true) { + const iterationStart = Date.now(); // Замер времени начала итерации + try { const donat = await getDonatInfo(streamerID); console.log('Donat received:', donat); @@ -183,10 +186,12 @@ 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); @@ -194,36 +199,39 @@ async function widgetView() { addText(contentDiv, donat.text); } + // Воспроизведение аудио if (donat.audio_link) { playSpeechAfterAudio(donat.audio_link, donat.text); } else if (donat.text) { playSpeech(donat.text); } + // Ожидаем указанную длительность await new Promise(r => setTimeout(r, donat.duration * 1000)); + // Отправка подтверждения просмотра if (donat.order_id) { try { const response = await fetch(widgetUrl + '/widget/donat/viewed', { method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ order_id: donat.order_id }), + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({order_id: donat.order_id}), }); - if (!response.ok) { - console.error('Failed to mark donat as viewed:', response.status); - } else { - console.log('Donat marked as viewed:', donat.order_id); - } + if (!response.ok) console.error('Ошибка подтверждения просмотра'); } catch (error) { - console.error('Error marking donat as viewed:', error); + console.error('Ошибка:', error); } } } catch (error) { - console.error('Main loop error:', error); - await new Promise(r => setTimeout(r, 5000)); + console.error('Ошибка в цикле:', error); + } finally { + // Гарантируем задержку между итерациями + const elapsed = Date.now() - iterationStart; + const remaining = REQUEST_INTERVAL - elapsed; + if (remaining > 0) { + await new Promise(r => setTimeout(r, remaining)); + } } } } @@ -234,7 +242,6 @@ document.addEventListener('DOMContentLoaded', widgetView);`, donatHost, ttsHost, -