-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
151 lines (134 loc) · 3.41 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<title>Speech to text</title>
<style>
*{
margin: 0;
padding: 0;
}
body{
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
.speachtotextContainer{
display: grid;
grid-template-columns: 50% 50%;
}
.speachtotextContainer .microphoneContainer{
background: darkgray;
width: 100%;
height: 100vh;
}
.microphoneContainer .microphone{
font-size: 50px;
text-align: center;
margin-top: 30%;
}
.contentText{
padding: 20px;
margin-top: 5%;
}
.demo{
font-size: 20px;
position: relative;
}
.demo::after{
position: absolute;
content: '';
width: 2px;
height: 20px;
background: black;
margin-left: 5px;
animation: blink 1s infinite;
}
@keyframes blink{
0%{
background: white;
}
10%{
background: white;
}
50%{
background: black;
}
60%{
background: black;
}
100%{
background: white;
}
}
</style>
</head>
<body onload="toggle()">
<div class="speachtotextContainer">
<div class="microphoneContainer">
<div class="microphone">
<i class="fa fa-microphone fa-5x" id="clickable" style="color: #0099CC" onclick="toggle(true)" accesskey="s"></i>
</div>
</div>
<div class="contentText">
<div id="text"></div>
<p class="demo"></p>
</div>
</div>
<div class="mobileSpeachtotextContainer"></div>
<script>
var restart = true
var on = false;
var lastFinal = "";
const content = document.querySelector(".demo")
const speechRecognition = window.speechRecognition || window.webkitSpeechRecognition;
const recognition = new speechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function(){
console.log("voice is activated")
}
recognition.onend = function(){
console.log("voice is no longer activated")
if(on){toggle(false)}
}
recognition.onresult = function(event){
const current = event.resultIndex;
const transcript = event.results[current][0].transcript
const contentDiv = document.getElementById("text")
content.textContent = transcript
if(event.results[current].isFinal){
if(transcript !== lastFinal){
lastFinal = transcript
addThis = document.createElement('p')
addThis.textContent = transcript;
addThis.setAttribute("oncontextmenu", "removeThis();return false;")
addThis.setAttribute("onclick", "editThis(true)")
addThis.setAttribute("onfocusout", "editThis(false)")
contentDiv.appendChild(addThis)
content.textContent = ""
}
}
}
recognition.onerror = function(event){
console.log('Speech recognition error detected: ' + event.error)
recognition.stop()
}
function toggle(clicked){
element = document.getElementById("clickable")
contentDiv = document.getElementById("contentText")
if(on){
on = false
element.setAttribute("style","color: #0099CC")
recognition.stop()
if((restart) && (clicked == false)){toggle(false)}
} else {
on = true
element.setAttribute("style","color: red")
recognition.start();
}
}
function removeThis(){event.target.remove()}
function editThis(editable){event.target.setAttribute("contenteditable", editable)}
</script>
</body>
</html>