Greasy Fork is available in English.

chat-gpt-voice-recognition-userscript

A userscript to add voice recognition to Chat GPT

  1. // ==UserScript==
  2. // @name chat-gpt-voice-recognition-userscript
  3. // @match https://chat.openai.com/chat
  4. // @version 2022.12.7
  5. // @author Jared Jacobsen (https://github.com/JaredJacobsen)
  6. // @license MIT
  7. // @description A userscript to add voice recognition to Chat GPT
  8. // @namespace https://greatest.deepsurf.us/users/994126
  9. // ==/UserScript==
  10.  
  11. let textArea;
  12. let transcript = '';
  13. var recognition = new webkitSpeechRecognition();
  14. recognition.continuous = false;
  15. recognition.interimResults = true;
  16.  
  17. let recognizing = false;
  18. recognition.onstart = function () {
  19. textArea.parentElement.style.borderColor = 'red';
  20. textArea.value = '';
  21. recognizing = true;
  22. transcript = '';
  23. };
  24. recognition.onresult = function (event) {
  25. transcript = '';
  26. for (var i = event.resultIndex; i < event.results.length; ++i) {
  27. transcript += event.results[i][0].transcript;
  28. }
  29. textArea.focus();
  30. textArea.value = transcript;
  31. let ev = new Event('input', { bubbles: true });
  32. textArea.dispatchEvent(ev);
  33. };
  34. recognition.onend = function () {
  35. textArea.parentElement.style.borderColor = 'lightgray';
  36. recognizing = false;
  37. transcript = '';
  38. textArea.parentElement.querySelector('button').click();
  39. };
  40. recognition.onerror = function (event) {
  41. console.log('error', event);
  42. textArea.parentElement.style.borderColor = 'lightgray';
  43. };
  44.  
  45. document.addEventListener(
  46. 'keydown',
  47. (e) => {
  48. if (e.code === 'Tab') {
  49. e.preventDefault();
  50. e.stopImmediatePropagation();
  51. textArea = document.querySelector('textarea');
  52. if (recognizing) {
  53. recognizing = false;
  54. recognition.stop();
  55. } else recognition.start();
  56. }
  57. },
  58. true
  59. );