ttsspeak

tts

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/534400/1697234/ttsspeak.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

;(() => {
    function prioritizeNaturalVoices(voices) {
        const arr = [];
        if (voices.length < 1) {
            return [];
        }
        const allVoices = voices.filterAndMapX(voice => voice.name.includes('Natural') ? voice : (arr.push(voice) , false));
        allVoices.push(...arr);
        return allVoices
    }

    let voices = prioritizeNaturalVoices(speechSynthesis.getVoices()), utterance, vice, viceMap = {}, playStat = 0,
        icon;

    (() => {
        const fn = () => {
            utterance = new SpeechSynthesisUtterance();
            utterance.addEventListener('end', () => {
                playStat = 0;
                setIcon('▶️');
            })
            utterance.addEventListener('pause', (e) => {
                playStat = 2;
                setIcon('▶️');
            })
            utterance.addEventListener('resume', (e) => {
                playStat = 1;
                setIcon('⏸️');
            })
            voices.map(v => viceMap[v.voiceURI] = v);
        };
        voices.length > 1 ? fn() : speechSynthesis.addEventListener("voiceschanged", () => (voices = prioritizeNaturalVoices(speechSynthesis.getVoices()), fn()));
        return voices;
    })();

    let selectVice = GM_getValue('ttsVice', '自动选择');
    let rate = GM_getValue('ttsrate', 1);
    const setIcon = (i) => {
        if (!icon) {
            return
        }
        const pp = icon.parentElement.querySelector('button.pp');
        pp && (pp.innerHTML = i);
    };

    function play(text, vice = null) {
        utterance.voice = vice ? vice : viceMap[selectVice];
        utterance.text = text;
        utterance.rate = rate;
        playStat = 1;
        speechSynthesis.speak(utterance);
    }

    function speak(speakText) {
        if (viceMap[selectVice]) {
            play(speakText);
            return
        }
        const la = eld.detect(speakText).language;
        console.log(la);
        for (const value of voices) {
            const lang = value.lang.toLowerCase();
            if (lang.indexOf(la) > -1) {
                vice = value
                break;
            }
        }
        if (!vice) {
            icon.title = '似乎无可用的tts,请先安装';
            return
        }
        play(speakText, vice);
    }

    PushIconAction({
        name: 'tts发音 右键设置语速和语言',
        id: 'icon-speech',
        image: GM_getResourceURL('icon-speak'),
        trigger: function (speakText, _, ev) {
            if (voices.length < 1) {
                ev.target.title = 'tts还没有准备好,请稍等';
                return
            }
            speak(speakText);
        },
        call: (img) => {
            img.addEventListener('contextmenu', (e) => {
                e.preventDefault();
                const content = img.parentElement.querySelector('tr-content');
                content.style.display = 'block';
                const arr = voices.map(v => [`${v.lang} - ${v.localService && !v.name.includes('Online') ? 'local' : ''}-${v.name}`, v.voiceURI]);
                arr.unshift(['自动选择', '']);
                const options = buildOption(arr, selectVice, 1, 0);
                content.querySelector('div').innerHTML = `
                <div class="item">
                    <button class="pp">${playStat === 1 ? '⏸️' : '▶️'}</button>
                    <button class="stop">⏹️</button>

                </div>
                <div class="item">
                    <label for="speakspeed">语速:</label>
                    <input id="speakspeed" value="${rate}" min="0.1" step="0.1" type="number">
                </div>
                <div class="item">
                    <label for="language">语言:</label>
                    <select id="language">${options}</select>
                </div>
                `;
                icon = content.querySelector('.pp');
                content.querySelector('.stop').addEventListener('click', () => {
                    speechSynthesis.cancel();
                    playStat = 0;
                    icon.innerHTML = '▶️';
                });
                icon.addEventListener('click', function (e) {
                    switch (playStat) {
                        case 0:
                            speak(window.getSelection().toString().trim());
                            this.innerHTML = '⏸️';
                            break;
                        case 1:
                            speechSynthesis.pause();
                            this.innerHTML = '▶️';
                            break;
                        case 2:
                            speechSynthesis.resume();
                            this.innerHTML = '⏸️';
                            break;
                        default:
                            break;
                    }

                })
                content.querySelector('#speakspeed').addEventListener('change', function (e) {
                    rate = this.value;
                    GM_setValue('ttsrate', rate);
                });
                content.querySelector('#language').addEventListener('change', function (e) {
                    selectVice = this.value;
                    GM_setValue('ttsVice', this.value);
                })
            })
        },
        hide: (icon) => {
            speechSynthesis.cancel();
            setIcon('▶️');
            playStat = 0;
        }
    })
})();