Skip to content

Commit 6ac023c

Browse files
authored
Merge pull request #800 from vasudhawaman/gh-pages
Added Speech recognition using Web speech API
2 parents 56bb1d4 + a0127f8 commit 6ac023c

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

index.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<link rel="stylesheet" href="styles/modal.css">
1414
<link rel="stylesheet" href="styles/styles.css" />
1515
<link rel="stylesheet" href="styles/pagination.css" />
16+
<link rel="stylesheet" href="styles/speech.css" />
1617
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
1718
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js"></script>
1819
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-database.js"></script>
@@ -131,7 +132,29 @@ <h1 class="main-heading">Awesome GitHub Profile READMEs</h1>
131132
class="search-input"
132133
placeholder="Search Nickname or Username..."
133134
/>
134-
<i class="fas fa-search" ></i>
135+
136+
137+
<i class="fas fa-search" style="position: relative;right: 40px;"></i>
138+
<button class="mic-button">
139+
<i class="fa fa-microphone"></i>
140+
<svg id="animated-voice" width="1rem" height="1rem" viewBox="0 0 100 100">
141+
<g stroke="#fff" stroke-width="15">
142+
<path d="M20,20 20,80">
143+
<animate attributeName="d" values="M20,40 20,60;M20,20 20,80;M20,40 20,60" dur="1s" repeatCount="indefinite" />
144+
</path>
145+
<path d="M50,10 50,90">
146+
<animate attributeName="d" values="M50,10 50,90;M50,40 50,60;M50,10 50,90" dur="1s" repeatCount="indefinite" />
147+
</path>
148+
<path d="M80,20 80,80">
149+
<animate attributeName="d" values="M80,40 80,60;M80,20 80,80;M80,40 80,60" dur="1s" repeatCount="indefinite" />
150+
</path>
151+
</g>
152+
</svg>
153+
154+
</button>
155+
156+
157+
135158
<i class="fa-solid fa-caret-down dropdown" id="caret-down"></i>
136159
</div>
137160
<!-- Modal Structure -->
@@ -234,6 +257,7 @@ <h2>Join the Recode Hive Community</h2>
234257
<script src="scripts/dark-mode.js"></script>
235258
<script src="scripts/hamburger.js"></script>
236259
<script src="scripts/revealelementsonscroll.js"></script>
260+
<script src="scripts/speechRecognition.js"></script>
237261
<!-- Added script for new Twitter logo -->
238262
<script src="https://kit.fontawesome.com/856f4a44d7.js" crossorigin="anonymous"></script>
239263

scripts/retriveprofile.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,13 @@ document.addEventListener("DOMContentLoaded", function () {
230230
searchBar.addEventListener("input", () => {
231231
renderProfiles(searchBar.value);
232232
});
233+
searchBar.addEventListener("click", () => {
234+
renderProfiles(searchBar.value);
235+
});
233236
});
234237

238+
239+
235240
document.addEventListener("mouseover", function (e) {
236241
if (e.target.tagName === "IMG" && e.target.closest(".scroll-on-hover")) {
237242
const imgContainer = e.target.closest(".img-container");

scripts/speechRecognition.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
function startSearch() {
3+
// get main elements
4+
const input = document.querySelector(".search-input");
5+
const btnListen = document.querySelector(".mic-button");
6+
let listening = false;
7+
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
8+
9+
// if there's speech recognition, show the microphone
10+
if (SpeechRecognition) {
11+
setTimeout(function () {
12+
btnListen.classList.add("show");
13+
}, 1000);
14+
console.log("listening");
15+
}
16+
17+
// show/hide placeholder
18+
19+
20+
// listen to speech
21+
btnListen.addEventListener("click", function () {
22+
btnListen.classList.add("listening");
23+
24+
if (!listening) {
25+
26+
const recognition = new SpeechRecognition();
27+
28+
recognition.onstart = function () {
29+
btnListen.classList.add("listening");
30+
listening = true;
31+
};
32+
33+
recognition.onspeechend = function () {
34+
recognition.stop();
35+
btnListen.classList.remove("listening");
36+
listening = false;
37+
};
38+
39+
recognition.onerror = function () {
40+
btnListen.classList.remove("listening");
41+
listening = false;
42+
};
43+
44+
recognition.onresult = function (event) {
45+
const transcript = event.results[0][0].transcript;
46+
const confidence = event.results[0][0].confidence;
47+
48+
input.value = transcript;
49+
console.log(transcript);
50+
input.focus();
51+
};
52+
53+
recognition.start();
54+
}
55+
});
56+
}
57+
58+
startSearch();
59+

styles/speech.css

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.mic-button{
2+
display: none;
3+
position: relative;
4+
right : 12px;
5+
font-size: 1em;
6+
background: none;
7+
border: none;
8+
outline: none;
9+
box-shadow: none;
10+
padding: 10px;
11+
12+
}
13+
.show{
14+
display: inline-block;
15+
}
16+
.mic-button :hover{
17+
background-color: rgb(219, 214, 214);
18+
border-radius: 100%;
19+
padding: 15px;
20+
height: 10px;
21+
width: 10px;
22+
cursor: pointer;
23+
transition: background-color 0.3s ease;
24+
display: flex;
25+
align-items: center;
26+
justify-content: center;
27+
28+
}
29+
#animated-voice{
30+
display: none;
31+
}
32+
.listening{
33+
34+
background-color: rgb(8, 8, 8);
35+
padding: 20px;
36+
border-radius: 100%;
37+
height: 10px;
38+
width: 10px;
39+
40+
}
41+
.listening >i{
42+
display: none;
43+
}
44+
.listening #animated-voice{
45+
display: block;
46+
position: relative;
47+
right: 9.5px;
48+
bottom: 7px;
49+
font-size: 1.25em;
50+
51+
}

0 commit comments

Comments
 (0)