RPGEN - Read out Sasayaki-Chat

TL;DR

目前为 2024-10-31 提交的版本。查看 最新版本

// ==UserScript==
// @name         RPGEN - Read out Sasayaki-Chat
// @namespace    https://tampermonkey.net/
// @version      0.8
// @description  TL;DR
// @author       You
// @match        https://rpgen.site/dq/?map=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=rpgen.site
// @license      MIT
// @grant        GM.registerMenuCommand
// @grant        GM.getValue
// @grant        GM.setValue
// ==/UserScript==

(async window => {
    const isEnable = (() => {
        let flag = false;
        const name = 'I/O';
        GM.getValue(name, true).then(value => {
            flag = Boolean(value);
        });
        GM.registerMenuCommand(name, () => {
            flag = !flag;
            GM.setValue(name, flag);
        });
        return () => flag;
    })();

    const removeNonVerbal = str => str
    .replace(/\[.+?\]/g, ' Urawaza ')
    .replace(/h?ttps?:\/\/.+$/g, ' URL ');

    const id = setInterval(() => {
        if (window.dqSock && window.dqSock.socket) {
            clearInterval(id);
            window.dqSock.socket.on("uNC_s", (async function({g, t}) {
                if (isEnable()) {
                    const [a, b] = new Uint8Array(await window.crypto.subtle.digest('SHA-1', (new TextEncoder()).encode(g)));
                    const unique = (a << 8) + b;
                    const uttr = new SpeechSynthesisUtterance(removeNonVerbal(t));
                    const voices = window.speechSynthesis.getVoices().filter(v => v.lang === 'ja-JP');
                    uttr.voice = voices[voices.length * unique / 0xFFFF | 0];
                    window.speechSynthesis.speak(uttr);
                }
            }));
        }
    }, 500);
})(window.unsafeWindow || window);