Telegram Speaker

[SNOLAB] Speak latest telegram message With TTS technology just in your browser. @deprecated Use my new script [SNOLAB] I Heard Telegram Speaking

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @deprecated       Use my new script [SNOLAB] I Heard Telegram Speaking
// @name             Telegram Speaker
// @namespace        https://userscript.snomiao.com/
// @author           [email protected]
// @version          0.1.3
// @description      [SNOLAB] Speak latest telegram message With TTS technology just in your browser. @deprecated Use my new script [SNOLAB] I Heard Telegram Speaking
// @match            https://*.telegram.org/z/
// @grant            none
// @run-at           document-start
// @license          GPL-3.0+
// @supportURL       https://github.com/snomiao/userscript.js/issues
// @contributionURL  https://snomiao.com/donate
// ==/UserScript==

/*
# the legacy way needs you to install and run an saying pipe service in your computer, in which situation that you don't have latest browser with TTS technologies.

npm i -g piserve snosay
piserve | snosay --voice "Microsoft Huihui Desktop"

*/

async function say(s) {
    if (!s) return; // console.error('say empty msg')

    // new method to say
    console.log("saying " + s);
    if (globalThis.speechSynthesis) {
        // wait for voices
        while (speechSynthesis.getVoices().length === 0) {
            await new Promise((r) => setTimeout(r, 1e3));
        }
        const utter = new SpeechSynthesisUtterance(s);
        utter.voice = speechSynthesis
            .getVoices()
            .filter(({ lang }) => navigator.languages.includes(lang))
            .reverse()[0];
        utter.rate = Math.min(Math.max(1, s.length / 60), 4);
        if (speechSynthesis.speaking) speechSynthesis.cancel();
        speechSynthesis.speak(utter);
    } else await fetch("http://localhost:25971/?text=" + encodeURIComponent(s));
}
const lastMsg = () =>
    [...document.querySelectorAll(".Message:not(.own) .text-content")]
        .map((e) => e.textContent)
        .reverse()[0];
const chagnedFilterMaker = (init) => (e) => e !== init ? (init = e) : undefined;
const changedFilter = chagnedFilterMaker("");
const looper = () => (say(changedFilter(lastMsg())), 1);

(async function () {
    while (looper()) await new Promise((r) => setTimeout(r, 1000));
})();