vits-simple-api for SillyTavern

Add a button to each chat message to play it using TTS API

ของเมื่อวันที่ 16-12-2023 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey to install this script.

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         vits-simple-api for SillyTavern
// @namespace    https://github.com/yujianke100/vits-simple-api-for-SillyTavern
// @version      0.1
// @license MIT
// @description  Add a button to each chat message to play it using TTS API
// @author       shinshi
// @match        *://*:*8000/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // Function to add TTS button to each chat message
    function addTTSButton(message) {
        if (!message.querySelector('.tts-button')) {
            const button = document.createElement('button');
            button.innerText = 'TTS';
            button.classList.add('tts-button');

            button.addEventListener('click', () => {
                console.log('TTS button clicked');
                const messageText = message.querySelector('.mes_text').innerText;
                console.log('TTS for:', messageText);
                playTTS(messageText);
            });

            const buttonsContainer = message.querySelector('.mes_buttons');
            if (buttonsContainer) {
                buttonsContainer.appendChild(button);
                console.log('TTS button added');
            }
        }
    }

    // Function to send text to TTS API and play response
    function playTTS(text) {
        console.log('Sending text to TTS API:', text);
        const apiUrl = `http://127.0.0.1:23456/voice/bert-vits2?text=${encodeURIComponent(text)}&id=0&noise=0.5&noisew=0.5`;

        GM_xmlhttpRequest({
            method: 'GET',
            url: apiUrl,
            responseType: 'blob', // 重要:期望一个二进制响应
            onload: function(response) {
                // 将响应的 Blob 转换为 URL
                const audioUrl = URL.createObjectURL(response.response);
                const audio = new Audio(audioUrl);
                audio.play().catch(e => console.error('Audio play error:', e));
            },
            onerror: function(error) {
                console.error('TTS API Error:', error);
            }
        });
    }

    // Using MutationObserver to dynamically add buttons to new messages
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes) {
                mutation.addedNodes.forEach(node => {
                    if (node.classList && node.classList.contains('mes')) {
                        addTTSButton(node);
                    }
                });
            }
        });
    });

    // Start observing
    const config = { childList: true, subtree: true };
    observer.observe(document.body, config);
})();