|
| 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 | + |
| 16 | + const recognition = new SpeechRecognition(); |
| 17 | + recognition.interimResults = true; |
| 18 | + |
| 19 | + let p = document.createElement('p'); |
| 20 | + const words = document.querySelector('.words'); |
| 21 | + words.appendChild(p); |
| 22 | + |
| 23 | + recognition.addEventListener('result', e => { |
| 24 | + // console.log(e.results); |
| 25 | + const transcript = Array.from(e.results) |
| 26 | + .map(result => result[0]) |
| 27 | + .map(result => result.transcript) |
| 28 | + .join('') |
| 29 | + |
| 30 | + p.textContent = transcript; |
| 31 | + if(e.results[0].isFinal) { |
| 32 | + p = document.createElement('p'); |
| 33 | + words.appendChild(p); |
| 34 | + } |
| 35 | + |
| 36 | + if(transcript.includes('get the weather')){ |
| 37 | + console.log('unicorn') |
| 38 | + } |
| 39 | + console.log(transcript); |
| 40 | + |
| 41 | + }); |
| 42 | + |
| 43 | + recognition.addEventListener('end', recognition.start); |
| 44 | + recognition.start(); |
| 45 | + |
| 46 | +</script> |
| 47 | + |
| 48 | + |
| 49 | + <style> |
| 50 | + html { |
| 51 | + font-size: 10px; |
| 52 | + } |
| 53 | + |
| 54 | + body { |
| 55 | + background:#ffc600; |
| 56 | + font-family: 'helvetica neue'; |
| 57 | + font-weight: 200; |
| 58 | + font-size: 20px; |
| 59 | + } |
| 60 | + |
| 61 | + .words { |
| 62 | + max-width:500px; |
| 63 | + margin:50px auto; |
| 64 | + background:white; |
| 65 | + border-radius:5px; |
| 66 | + box-shadow:10px 10px 0 rgba(0,0,0,0.1); |
| 67 | + padding:1rem 2rem 1rem 5rem; |
| 68 | + background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px; |
| 69 | + background-size: 100% 3rem; |
| 70 | + position: relative; |
| 71 | + line-height:3rem; |
| 72 | + } |
| 73 | + p { |
| 74 | + margin: 0 0 3rem 0; |
| 75 | + } |
| 76 | + |
| 77 | + .words:before { |
| 78 | + content: ''; |
| 79 | + position: absolute; |
| 80 | + width: 4px; |
| 81 | + top: 0; |
| 82 | + left: 30px; |
| 83 | + bottom: 0; |
| 84 | + border: 1px solid; |
| 85 | + border-color: transparent #efe4e4; |
| 86 | + } |
| 87 | + </style> |
| 88 | + |
| 89 | +</body> |
| 90 | +</html> |
0 commit comments