add fix for widget template
This commit is contained in:
parent
d6180ddc3e
commit
d1c536acbc
@ -165,6 +165,7 @@ function playSpeechAfterAudio(audioUrl, text) {
|
|||||||
async function widgetView() {
|
async function widgetView() {
|
||||||
const streamerID = '%v';
|
const streamerID = '%v';
|
||||||
const contentDiv = document.getElementById('content');
|
const contentDiv = document.getElementById('content');
|
||||||
|
const REQUEST_INTERVAL = 5000; // Фиксированный интервал 5 секунд
|
||||||
|
|
||||||
if (!contentDiv) {
|
if (!contentDiv) {
|
||||||
console.error('Content container not found!');
|
console.error('Content container not found!');
|
||||||
@ -172,6 +173,8 @@ async function widgetView() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
const iterationStart = Date.now(); // Замер времени начала итерации
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const donat = await getDonatInfo(streamerID);
|
const donat = await getDonatInfo(streamerID);
|
||||||
console.log('Donat received:', donat);
|
console.log('Donat received:', donat);
|
||||||
@ -183,10 +186,12 @@ async function widgetView() {
|
|||||||
|
|
||||||
clearContainer(contentDiv);
|
clearContainer(contentDiv);
|
||||||
|
|
||||||
|
// Добавление элементов в DOM
|
||||||
if (donat.image_link) {
|
if (donat.image_link) {
|
||||||
addImage(contentDiv, donat.image_link);
|
addImage(contentDiv, donat.image_link);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Создание текста с суммой
|
||||||
if (donat.text && donat.amount) {
|
if (donat.text && donat.amount) {
|
||||||
const textWithAmount = createTextWithAmount(donat.text, donat.amount);
|
const textWithAmount = createTextWithAmount(donat.text, donat.amount);
|
||||||
contentDiv.appendChild(textWithAmount);
|
contentDiv.appendChild(textWithAmount);
|
||||||
@ -194,36 +199,39 @@ async function widgetView() {
|
|||||||
addText(contentDiv, donat.text);
|
addText(contentDiv, donat.text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Воспроизведение аудио
|
||||||
if (donat.audio_link) {
|
if (donat.audio_link) {
|
||||||
playSpeechAfterAudio(donat.audio_link, donat.text);
|
playSpeechAfterAudio(donat.audio_link, donat.text);
|
||||||
} else if (donat.text) {
|
} else if (donat.text) {
|
||||||
playSpeech(donat.text);
|
playSpeech(donat.text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ожидаем указанную длительность
|
||||||
await new Promise(r => setTimeout(r, donat.duration * 1000));
|
await new Promise(r => setTimeout(r, donat.duration * 1000));
|
||||||
|
|
||||||
|
// Отправка подтверждения просмотра
|
||||||
if (donat.order_id) {
|
if (donat.order_id) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(widgetUrl + '/widget/donat/viewed', {
|
const response = await fetch(widgetUrl + '/widget/donat/viewed', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {'Content-Type': 'application/json'},
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({order_id: donat.order_id}),
|
body: JSON.stringify({order_id: donat.order_id}),
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) console.error('Ошибка подтверждения просмотра');
|
||||||
console.error('Failed to mark donat as viewed:', response.status);
|
|
||||||
} else {
|
|
||||||
console.log('Donat marked as viewed:', donat.order_id);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error marking donat as viewed:', error);
|
console.error('Ошибка:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Main loop error:', error);
|
console.error('Ошибка в цикле:', error);
|
||||||
await new Promise(r => setTimeout(r, 5000));
|
} 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,
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<style>%s</style>
|
<style>%s</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user