-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
116 lines (103 loc) · 3.18 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
<!DOCTYPE html>
<html lang="pt-br">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Speech Recognition App</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<style>
body {
font-size: 40px;
}
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
min-height: 500px;
padding: 10px;
}
.luz-apagada {
color: white;
background: black;
animation: luz-apagada .5s ease-in-out;
}
.luz-acessa {
color: black;
background: white;
animation: luz-acessa .5s ease-in-out;
}
@keyframes luz-apagada {
0% {
background: white;
color: black;
}
100% {
background: black;
color: white;
}
}
@keyframes luz-acessa {
0% {
background: black;
color: white;
}
100% {
background: white;
color: black;
}
}
</style>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="page-header">
<p> Este site utiliza comandos por voz.</p>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p> Para <span id="estado">Apagar</span> as luzes diga: 'luz'</p>
</div>
</div>
</div>
<script type="text/javascript">
var luzAcessa = true;
var estadoSpan = document.querySelector('#estado');
window.addEventListener('DOMContentLoaded', function (e) {
var speakBtn = document.querySelector('#speakBtn');
var speech = window.SpeechRecognition || window.webkitSpeechRecognition
var recognition;
try {
recognition = new SpeechRecognition();
} catch (error) {
recognition = new webkitSpeechRecognition();
}
recognition.continuous = true;
recognition.start();
recognition.addEventListener('result', function (e) {
setTimeout(function () {
console.log(e.results[e.resultIndex][0].transcript)
var result = e.results[e.resultIndex][0].transcript.trim();
if (result.toLowerCase() === "luz") {
if (luzAcessa) {
document.body.classList.remove('luz-acessa');
document.body.classList.add('luz-apagada');
estadoSpan.innerHTML = 'Acender';
} else {
document.body.classList.remove('luz-apagada');
document.body.classList.add('luz-acessa');
estadoSpan.innerHTML = 'Apagar';
}
luzAcessa = !luzAcessa;
};
}, 50)
}, false);
if (window.SpeechRecognition || window.webkitSpeechRecognition) {
} else {
alert('Este navegador não suporta esta funcionalidade.');
}
}, false);
</script>
</html>