Alt+S to Speak from TTS engines

Supports bing translator,Can be used to pretend to be a robot in online classes. (Alt+Shift+S to speak translated text)

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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            Alt+S to Speak from TTS engines
// @name:zh         Alt+S 翻译TTS自动发声
// @name:ja         Alt+S Alt + SでTTSエンジンから読み上げる
// @description     Supports bing translator,Can be used to pretend to be a robot in online classes. (Alt+Shift+S to speak translated text)
// @description:zh  现支持:必应翻译,可用于在网课中假装机器人讲话(Alt + Shift + S 读翻译后的声音)
// @description:ja  Bingトランスレータをサポートします。オンラインクラスのロボットのふりをするために使用できます。(Alt + Shift + Sで翻訳されたテキストを読み上げる)
// @namespace       https://userscript.snomiao.com/
// @version         0.1
// @author          [email protected]
// @run-at          document-start
// @match           https://*.bing.com/translator*
// @match           https://translate.google.com*
// @grant           none
// ==/UserScript==

(function () {
    'use strict';
    var clickNode = (e) => {
        ['mouseover', 'mousedown', 'mouseup', 'click'].map((eventType) => {
            var clickEvent = document.createEvent('MouseEvents');
            clickEvent.initEvent(eventType, true, true);
            e.dispatchEvent(clickEvent);
        });
    };
    var ClickAndSelectAll = (selBTN, selINPUT) => {
        var btnTTS = document.querySelector(selBTN);
        var textArea = document.querySelector(selINPUT);
        clickNode(btnTTS);
        textArea.focus();
        textArea.select();
    };
    // Bing
    location.href.match(/^https\:\/\/\w*\.bing\.com\/translator/) &&
        window.addEventListener('keydown', (e) => {
            if (e.altKey && !e.shiftKey && !e.ctrlKey && e.code == 'KeyS')
                ClickAndSelectAll('#tta_playiconsrc', '#tta_input_ta');
            if (e.altKey && e.shiftKey && !e.ctrlKey && e.code == 'KeyS')
                ClickAndSelectAll('#tta_playicontgt', '#tta_input_ta');
        });
    // Google
    location.href.startsWith('https://translate.google.com') &&
        window.addEventListener('keydown', (e) => {
            if (e.altKey && !e.shiftKey && !e.ctrlKey && e.code == 'KeyS')
                ClickAndSelectAll('.ttsbutton', '#source');
            if (e.altKey && e.shiftKey && !e.ctrlKey && e.code == 'KeyS')
                ClickAndSelectAll('.ttsbutton-res', '#source');
        });
})();