This commit is contained in:
harold 2025-04-28 22:54:27 +05:00
parent 9e2822e70e
commit 99d7950afd

View File

@ -86,63 +86,21 @@ async function getDonatInfo(streamerID) {
} }
} }
function addImage(container, imageUrl) { function playSpeech(text, voiceSettings) {
const img = document.createElement('img'); if (!voiceSettings.voice_enabled) return;
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 addText(container, text) { const params = new URLSearchParams({
const p = document.createElement('p'); text: text,
p.className = 'donation-text'; speed: voiceSettings.voice_speed || 'medium',
p.textContent = text; scenery: voiceSettings.scenery || 'default',
container.appendChild(p); sound_percent: voiceSettings.voice_sound_percent || 100,
} min_price: voiceSettings.min_price || 0,
languages: voiceSettings.languages?.join(',') || ''
});
function clearContainer(container) { const url = ttsUrl + '/generate?' + params.toString();
while (container.firstChild) {
container.removeChild(container.firstChild); fetch(url)
}
}
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 }),
})
.then(response => { .then(response => {
if (!response.ok) throw new Error('TTS error'); if (!response.ok) throw new Error('TTS error');
return response.blob(); return response.blob();
@ -158,14 +116,14 @@ function playSpeech(text) {
.catch(console.error); .catch(console.error);
} }
function playSpeechAfterAudio(audioUrl, text) { function playSpeechAfterAudio(audioUrl, text, voiceSettings) {
playAudio(audioUrl, () => playSpeech(text)); playAudio(audioUrl, () => playSpeech(text, voiceSettings));
} }
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 секунд const REQUEST_INTERVAL = 5000;
if (!contentDiv) { if (!contentDiv) {
console.error('Content container not found!'); console.error('Content container not found!');
@ -173,12 +131,11 @@ async function widgetView() {
} }
while (true) { while (true) {
const iterationStart = Date.now(); // Замер времени начала итерации const iterationStart = Date.now();
try { try {
const donat = await getDonatInfo(streamerID); const donat = await getDonatInfo(streamerID);
console.log('Donat received:', donat);
if (!donat || Object.keys(donat).length === 0) { if (!donat || Object.keys(donat).length === 0) {
await new Promise(r => setTimeout(r, 5000)); await new Promise(r => setTimeout(r, 5000));
continue; continue;
@ -186,27 +143,36 @@ 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) {
const textWithAmount = createTextWithAmount(donat.text, donat.amount); const textElement = donat.amount
contentDiv.appendChild(textWithAmount); ? createTextWithAmount(donat.text, donat.amount)
} else if (donat.text) { : createTextElement(donat.text);
addText(contentDiv, 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) { if (donat.audio_link) {
playSpeechAfterAudio(donat.audio_link, donat.text); playSpeechAfterAudio(donat.audio_link, donat.text, voiceSettings);
} else if (donat.text) { } else if (donat.text && donat.voice_enabled) {
playSpeech(donat.text); playSpeech(donat.text, voiceSettings);
} }
// Ожидаем указанную длительность // Таймаут на основе duration
await new Promise(r => setTimeout(r, donat.duration * 1000)); await new Promise(r => setTimeout(r, donat.duration * 1000));
// Отправка подтверждения просмотра // Отправка подтверждения просмотра