ChatGPT Voice-to-Text

Enables voice-to-text functionality on chat.openai by holding down the 'v' key and speaking into your microphone, using the Web Speech API, and automatically inserting the transcribed text into the chat input field.

当前为 2023-05-03 提交的版本,查看 最新版本

// ==UserScript==
// @name         ChatGPT Voice-to-Text
// @namespace    openai_voice_to_text
// @version      2.1
// @description  Enables voice-to-text functionality on chat.openai by holding down the 'v' key and speaking into your microphone, using the Web Speech API, and automatically inserting the transcribed text into the chat input field.
// @match        https://chat.openai.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const recognition = new webkitSpeechRecognition();
    recognition.lang = 'en-US';
    recognition.continuous = true;
    recognition.interimResults = false;

    let isListening = false;
    let finalTranscript = '';

    const langSelector = document.createElement('select');
    langSelector.style.position = 'fixed';
    langSelector.style.top = '10px';
    langSelector.style.right = '10px';
    langSelector.style.zIndex = '99999';
    langSelector.style.fontSize = '18px';
    langSelector.style.padding = '10px';
    langSelector.style.borderRadius = '5px';
    langSelector.style.fontWeight = 'bold';
    langSelector.addEventListener('change', event => {
        recognition.lang = event.target.value;
    });

    langSelector.style.backgroundColor = '#565759';

    const langOptions = [
        { label: 'English (US)', value: 'en-US' },
        { label: 'English (UK)', value: 'en-GB' },
        { label: 'Español', value: 'es-ES' },
        { label: 'Français', value: 'fr-FR' },
        { label: 'Italiano', value: 'it-IT' },
        { label: 'Deutsch', value: 'de-DE' },
        { label: '日本語', value: 'ja-JP' },
        { label: '中文', value: 'zh-CN' }
    ];

    langOptions.forEach(option => {
        const langOption = document.createElement('option');
        langOption.value = option.value;
        langOption.textContent = option.label;
        if (option.value === recognition.lang) {
            langOption.selected = true;
        }
        langSelector.appendChild(langOption);
    });

    document.body.appendChild(langSelector);

    const micIcon = document.createElement('div');
    micIcon.style.position = 'fixed';
    micIcon.style.bottom = '10px';
    micIcon.style.right = '10px';
    micIcon.style.zIndex = '99999';
    micIcon.style.fontSize = '18px';
    micIcon.style.color = 'white';
    micIcon.style.padding = '10px';
    micIcon.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
    micIcon.style.borderRadius = '5px';
    micIcon.style.fontWeight = 'bold';
    micIcon.textContent = 'Press and hold V to talk';
    document.body.appendChild(micIcon);

    document.addEventListener('keydown', event => {
        if (event.key === 'v' && !isListening) {
            micIcon.textContent = 'Recording... (Release V to Stop)';
            startRecognition();
        }
    });

    document.addEventListener('keyup', event => {
        if (event.key === 'v' && isListening) {
            micIcon.textContent = 'Press and hold V to talk';
            stopRecognition();
        }
    });

     recognition.addEventListener('result', event => {
        const transcript = Array.from(event.results)
            .map(result => result[0].transcript)
            .join('');

        if (event.results[0].isFinal) {
            finalTranscript += transcript;
            insertText(finalTranscript);
            finalTranscript = '';
        }
    });

    function startRecognition() {
        isListening = true;
        recognition.start();
        console.log('Recording... (Release V to Stop)');
    }

    function stopRecognition() {
        isListening = false;
        recognition.stop();
        console.log('Stopped Recording');
    }

    function insertText(text) {
        const input = document.querySelector('textarea');
        input.value += text;
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址