Voice input for chat

To turn it on, press F2. When the chat is open, it will work by itself. Use the HTTPS protocol of the page. You can use 2 languages [ru, en] to switch, press F8.

  1. // ==UserScript==
  2. // @name Voice input for chat
  3. // @namespace -
  4. // @version 0.1
  5. // @description To turn it on, press F2. When the chat is open, it will work by itself. Use the HTTPS protocol of the page. You can use 2 languages [ru, en] to switch, press F8.
  6. // @author Nudo#9482
  7. // @match *://sploop.io/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=sploop.io
  9. // @run-at document-end
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. const Chat = document.getElementById("chat")
  15. const Keys = {}
  16. const Recognizer = new window.webkitSpeechRecognition()
  17.  
  18. Chat.wrapper = document.getElementById("chat-wrapper")
  19. Chat.voiceActive = false
  20. Chat.voiceLang = "en-En"
  21. Chat._voiceLangs = ["en-En", "ru-Ru"]
  22. Chat.counter = 0
  23.  
  24. Chat.getNextLang = function() {
  25. const length = Chat._voiceLangs.length - 1
  26.  
  27. this.counter += 1
  28.  
  29. if (this.counter > length) {
  30. this.counter = 0
  31. }
  32.  
  33. return this.counter
  34. }
  35.  
  36. Recognizer.active = false
  37.  
  38. Recognizer.setup = function() {
  39. this.interimResults = true
  40.  
  41. this.reset = function() {
  42. this.stop()
  43.  
  44. setTimeout(() => {
  45. this.start()
  46. }, 750)
  47. }
  48.  
  49. this.launch = function() {
  50. this.lang = Chat.voiceLang
  51.  
  52. this.active = !this.active
  53.  
  54. this.start()
  55. }
  56.  
  57. this.onresult = function() {
  58. try {
  59. if (!this.active) {
  60. return void 0
  61. }
  62.  
  63. const result = event.results[event.resultIndex]
  64. const words = result[0].transcript
  65.  
  66. if (words.length) {
  67. Chat.value = words
  68.  
  69. Chat.focus()
  70. }
  71. } catch {
  72. this.disable()
  73. }
  74. }
  75.  
  76. this.disable = function() {
  77. if (this.active) {
  78. this.active = !Recognizer.active
  79.  
  80. this.stop()
  81. }
  82. }
  83.  
  84. this.onend = function() {
  85. if (this.active) {
  86. this.reset()
  87. }
  88. }
  89. }
  90.  
  91. window.addEventListener("load", () => {
  92. Recognizer.setup()
  93. })
  94.  
  95. setInterval(() => {
  96. try {
  97. if (Chat.wrapper.style.display === "block") {
  98. if (Chat.voiceActive) {
  99. if (!Recognizer.active) {
  100. Recognizer.launch()
  101. }
  102. } else {
  103. Recognizer.disable()
  104. }
  105. } else {
  106. Recognizer.disable()
  107. }
  108. } catch {
  109. Recognizer.disable()
  110. }
  111. }, 100)
  112.  
  113. function onKeyDown(event) {
  114. if (event.code === "F2") {
  115. Chat.voiceActive = !Chat.voiceActive
  116. }
  117.  
  118. if (event.code === "F8") {
  119. Chat.voiceLang = Chat._voiceLangs[Chat.getNextLang()]
  120.  
  121. document.title = `Voice language: ${Chat.voiceLang}`
  122.  
  123. setTimeout(() => {
  124. document.title = "Sploop.io - Fast paced combat game"
  125. }, 2000)
  126. }
  127. }
  128.  
  129. document.addEventListener("keydown", onKeyDown)
  130. })()