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) {
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));
// Отправка подтверждения просмотра