Speech-to-Text for Textareas

Adds a microphone button to textareas for speech-to-text input

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Speech-to-Text for Textareas
// @namespace    http://yourdomain.example
// @version      1.0
// @description  Adds a microphone button to textareas for speech-to-text input
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    if (!SpeechRecognition) return;

    function addMicButton(textarea) {
        if (textarea.nextSibling && textarea.nextSibling.className === 'stt-mic-btn') return;

        const btn = document.createElement('button');
        btn.textContent = '🎤';
        btn.className = 'stt-mic-btn';
        btn.style.marginLeft = '5px';
        btn.title = 'Click to start speech-to-text';

        btn.onclick = function(e) {
            e.preventDefault();
            const recognition = new SpeechRecognition();
            recognition.lang = 'en-US';
            recognition.interimResults = false;
            recognition.maxAlternatives = 1;

            recognition.onresult = function(event) {
                const transcript = event.results[0][0].transcript;
                textarea.value += (textarea.value ? ' ' : '') + transcript;
            };
            recognition.onerror = function(event) {
                alert('Speech recognition error: ' + event.error);
            };
            recognition.start();
        };

        textarea.parentNode.insertBefore(btn, textarea.nextSibling);
    }

    document.querySelectorAll('textarea').forEach(addMicButton);

    const observer = new MutationObserver(() => {
        document.querySelectorAll('textarea').forEach(addMicButton);
    });
    observer.observe(document.body, { childList: true, subtree: true });
})();