add fix for template response

This commit is contained in:
harold 2025-04-29 00:08:10 +05:00
parent 99d7950afd
commit 47b15a6318

View File

@ -69,7 +69,7 @@ function createTextWithAmount(text, amount) {
const amountElem = document.createElement('div');
amountElem.className = 'donation-amount';
amountElem.textContent = amount + '₽';
amountElem.textContent = amount + '₽';
container.appendChild(textElem);
container.appendChild(amountElem);
@ -86,21 +86,36 @@ async function getDonatInfo(streamerID) {
}
}
function playAudio(url, callback, volume) {
const audio = new Audio(url);
audio.volume = volume;
audio.play().then(() => {
audio.addEventListener('ended', callback);
}).catch(error => {
console.error('Error playing audio:', error);
callback();
});
}
function playSpeech(text, voiceSettings) {
if (!voiceSettings.voice_enabled) return;
const params = new URLSearchParams({
const requestBody = {
text: text,
speed: voiceSettings.voice_speed || 'medium',
speed: (voiceSettings.voice_speed || 'medium').toLowerCase(),
scenery: voiceSettings.scenery || 'default',
sound_percent: voiceSettings.voice_sound_percent || 100,
min_price: voiceSettings.min_price || 0,
languages: voiceSettings.languages?.join(',') || ''
});
languages: voiceSettings.languages || ['ru']
};
const url = ttsUrl + '/generate?' + params.toString();
fetch(url)
fetch(ttsUrl + '/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => {
if (!response.ok) throw new Error('TTS error');
return response.blob();
@ -108,6 +123,7 @@ function playSpeech(text, voiceSettings) {
.then(blob => {
const url = URL.createObjectURL(blob);
const audio = new Audio(url);
audio.volume = (voiceSettings.voice_sound_percent || 100) / 100;
audio.play().catch(console.error);
audio.addEventListener('ended', () => {
URL.revokeObjectURL(url);
@ -117,7 +133,30 @@ function playSpeech(text, voiceSettings) {
}
function playSpeechAfterAudio(audioUrl, text, voiceSettings) {
playAudio(audioUrl, () => playSpeech(text, voiceSettings));
const volume = (voiceSettings.voice_sound_percent || 100) / 100;
playAudio(audioUrl, () => playSpeech(text, voiceSettings), volume);
}
function clearContainer(container) {
while (container.firstChild) {
container.removeChild(container.firstChild);
}
}
function addImage(container, imageUrl) {
const img = document.createElement('img');
img.src = imageUrl;
container.appendChild(img);
}
function createTextElement(text) {
const container = document.createElement('div');
container.className = 'text-container';
const textElem = document.createElement('p');
textElem.className = 'donation-text';
textElem.textContent = text;
container.appendChild(textElem);
return container;
}
async function widgetView() {
@ -156,7 +195,7 @@ async function widgetView() {
contentDiv.appendChild(textElement);
}
// Воспроизведение аудио и TTS
// Настройки голоса и громкости
const voiceSettings = {
voice_speed: donat.voice_speed,
scenery: donat.scenery,
@ -166,6 +205,7 @@ async function widgetView() {
voice_enabled: donat.voice_enabled
};
// Воспроизведение аудио и TTS
if (donat.audio_link) {
playSpeechAfterAudio(donat.audio_link, donat.text, voiceSettings);
} else if (donat.text && donat.voice_enabled) {