chat-gpt-voice-recognition-userscript

A userscript to add voice recognition to Chat GPT

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

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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        chat-gpt-voice-recognition-userscript
// @match       https://chat.openai.com/chat
// @version     2022.12.7
// @author      Jared Jacobsen (https://github.com/JaredJacobsen)
// @license     MIT
// @description A userscript to add voice recognition to Chat GPT
// @namespace https://greasyfork.org/users/994126
// ==/UserScript==

let textArea;
let transcript = '';
var recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimResults = true;

let recognizing = false;
recognition.onstart = function () {
	textArea.parentElement.style.borderColor = 'red';
	textArea.value = '';
	recognizing = true;
	transcript = '';
};
recognition.onresult = function (event) {
	transcript = '';
	for (var i = event.resultIndex; i < event.results.length; ++i) {
		transcript += event.results[i][0].transcript;
	}
	textArea.focus();
	textArea.value = transcript;
	let ev = new Event('input', { bubbles: true });
	textArea.dispatchEvent(ev);
};
recognition.onend = function () {
	textArea.parentElement.style.borderColor = 'lightgray';
	recognizing = false;
	transcript = '';
	textArea.parentElement.querySelector('button').click();
};
recognition.onerror = function (event) {
	console.log('error', event);
	textArea.parentElement.style.borderColor = 'lightgray';
};

document.addEventListener(
	'keydown',
	(e) => {
		if (e.code === 'Tab') {
			e.preventDefault();
			e.stopImmediatePropagation();
			textArea = document.querySelector('textarea');
			if (recognizing) {
				recognizing = false;
				recognition.stop();
			} else recognition.start();
		}
	},
	true
);