Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Speech Detection</title>
  6. </head>
  7. <body>
  8.  
  9. <div class="words" contenteditable>
  10. </div>
  11.  
  12. <script>
  13. window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
  14.  
  15. const recognition = new SpeechRecognition();
  16. recognition.interimResults = true;
  17. recognition.lang = 'en-US';
  18.  
  19. let p = document.createElement('p');
  20. const words = document.querySelector('.words');
  21. words.appendChild(p);
  22.  
  23. recognition.addEventListener('result', e => {
  24. var transcript = Array.from(e.results)
  25. .map(result => result[0])
  26. .map(result => result.transcript)
  27. .join('');
  28.  
  29.  
  30. var mySpeechShortcuts = [{ found: "smile", substitute: '🤑' }, { found: /poop|poo|shit|dump/gi, substitute: '💩' }, { found: "funk", substitute: 'function() { }' }];
  31.  
  32. mySpeechShortcuts.map((shortcut) => {
  33. if (transcript.indexOf(shortcut.found) >=0) {
  34. transcript = transcript.replace(shortcut.found, shortcut.substitute);
  35. }
  36. })
  37. p.textContent = transcript;
  38. if (e.results[0].isFinal) {
  39. p = document.createElement('p');
  40. words.appendChild(p);
  41. }
  42. });
  43.  
  44.  
  45.  
  46. recognition.addEventListener('end', recognition.start);
  47.  
  48. recognition.start();
  49.  
  50. </script>
  51.  
  52.  
  53. <style>
  54. html {
  55. font-size: 10px;
  56. }
  57.  
  58. body {
  59. background: #ffc600;
  60. font-family: 'helvetica neue';
  61. font-weight: 200;
  62. font-size: 20px;
  63. }
  64.  
  65. .words {
  66. max-width: 500px;
  67. margin: 50px auto;
  68. background: white;
  69. border-radius: 5px;
  70. box-shadow: 10px 10px 0 rgba(0,0,0,0.1);
  71. padding: 1rem 2rem 1rem 5rem;
  72. background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
  73. background-size: 100% 3rem;
  74. position: relative;
  75. line-height: 3rem;
  76. }
  77.  
  78. p {
  79. margin: 0 0 3rem;
  80. }
  81.  
  82. .words:before {
  83. content: '';
  84. position: absolute;
  85. width: 4px;
  86. top: 0;
  87. left: 30px;
  88. bottom: 0;
  89. border: 1px solid;
  90. border-color: transparent #efe4e4;
  91. }
  92. </style>
  93.  
  94. </body>
  95. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement