@@ -65,7 +65,7 @@ function filterComponents() {
65
65
66
66
// Voice command in search bar feature
67
67
const searchBar = document . querySelector ( "#searchBar" ) ;
68
- const searchBarInput = searchBar . querySelector ( "input" ) ;
68
+ const searchBarInput = searchBar ? searchBar . querySelector ( "input" ) : null ;
69
69
70
70
// The speech recognition interface lives on the browser’s window object
71
71
const SpeechRecognition = window . SpeechRecognition || window . webkitSpeechRecognition ;
@@ -76,49 +76,53 @@ if (SpeechRecognition) {
76
76
const recognition = new SpeechRecognition ( ) ;
77
77
recognition . continuous = true ;
78
78
79
- searchBar . insertAdjacentHTML ( "beforeend" , '<button type="button"><i class="fas fa-microphone"></i></button>' ) ;
80
- searchBarInput . style . paddingRight = "50px" ;
79
+ if ( searchBar && searchBarInput ) {
80
+ searchBar . insertAdjacentHTML ( "beforeend" , '<button type="button"><i class="fas fa-microphone"></i></button>' ) ;
81
+ searchBarInput . style . paddingRight = "50px" ;
81
82
82
- const micBtn = searchBar . querySelector ( "button" ) ;
83
- const micIcon = micBtn . firstElementChild ;
83
+ const micBtn = searchBar . querySelector ( "button" ) ;
84
+ const micIcon = micBtn . firstElementChild ;
84
85
85
- micBtn . addEventListener ( "click" , micBtnClick ) ;
86
- function micBtnClick ( ) {
87
- if ( micIcon . classList . contains ( "fa-microphone" ) ) { // Start Voice Recognition
88
- recognition . start ( ) ; // First time you have to allow access to mic!
86
+ micBtn . addEventListener ( "click" , micBtnClick ) ;
87
+
88
+ function micBtnClick ( ) {
89
+ if ( micIcon . classList . contains ( "fa-microphone" ) ) { // Start Voice Recognition
90
+ recognition . start ( ) ; // First time you have to allow access to mic!
91
+ } else {
92
+ recognition . stop ( ) ;
93
+ }
89
94
}
90
- else {
91
- recognition . stop ( ) ;
95
+
96
+ recognition . addEventListener ( "start" , startSpeechRecognition ) ;
97
+
98
+ function startSpeechRecognition ( ) {
99
+ micIcon . classList . remove ( "fa-microphone" ) ;
100
+ micIcon . classList . add ( "fa-microphone-slash" ) ;
101
+ searchBarInput . focus ( ) ;
102
+ console . log ( "Voice activated, SPEAK" ) ;
92
103
}
93
- }
94
104
95
- recognition . addEventListener ( "start" , startSpeechRecognition ) ;
96
- function startSpeechRecognition ( ) {
97
- micIcon . classList . remove ( "fa-microphone" ) ;
98
- micIcon . classList . add ( "fa-microphone-slash" ) ;
99
- searchFormInput . focus ( ) ;
100
- console . log ( "Voice activated, SPEAK" ) ;
101
- }
105
+ recognition . addEventListener ( "end" , endSpeechRecognition ) ;
102
106
103
- recognition . addEventListener ( "end" , endSpeechRecognition ) ;
104
- function endSpeechRecognition ( ) {
105
- micIcon . classList . remove ( "fa-microphone-slash" ) ;
106
- micIcon . classList . add ( "fa-microphone" ) ;
107
- searchBarInput . focus ( ) ;
108
- console . log ( "Speech recognition service disconnected" ) ;
109
- }
107
+ function endSpeechRecognition ( ) {
108
+ micIcon . classList . remove ( "fa-microphone-slash" ) ;
109
+ micIcon . classList . add ( "fa-microphone" ) ;
110
+ searchBarInput . focus ( ) ;
111
+ console . log ( "Speech recognition service disconnected" ) ;
112
+ }
113
+
114
+ recognition . addEventListener ( "result" , resultOfSpeechRecognition ) ;
110
115
111
- recognition . addEventListener ( "result" , resultOfSpeechRecognition ) ;
112
- function resultOfSpeechRecognition ( event ) {
113
- const current = event . resultIndex ;
114
- const transcript = event . results [ current ] [ 0 ] . transcript ;
115
- newtranscript = transcript . endsWith ( '.' ) ? transcript . slice ( 0 , - 1 ) : transcript ;
116
- console . log ( newtranscript )
117
- searchBarInput . value = newtranscript ;
118
- filterComponents ( ) ;
116
+ function resultOfSpeechRecognition ( event ) {
117
+ const current = event . resultIndex ;
118
+ const transcript = event . results [ current ] [ 0 ] . transcript ;
119
+ newtranscript = transcript . endsWith ( '.' ) ? transcript . slice ( 0 , - 1 ) : transcript ;
120
+ console . log ( newtranscript )
121
+ searchBarInput . value = newtranscript ;
122
+ filterComponents ( ) ;
123
+ }
119
124
}
120
- }
121
- else {
125
+ } else {
122
126
console . log ( "Your Browser does not support speech Recognition" ) ;
123
127
info . textContent = "Your Browser does not support Speech Recognition" ;
124
128
}
@@ -128,28 +132,38 @@ document.addEventListener("DOMContentLoaded", function () {
128
132
const homeLink = document . querySelector ( '.nav-menu a[href="#home"]' ) ;
129
133
const componentsLink = document . querySelector ( '.nav-menu a[href="#components"]' ) ;
130
134
135
+ // Check if elements exist before adding event listeners
131
136
const searchBarSection = document . getElementById ( 'searchBar' ) ;
132
137
const componentsSection = document . getElementById ( 'components' ) ;
133
138
134
- componentsLink . addEventListener ( 'click' , ( event ) => {
135
- event . preventDefault ( ) ;
136
- componentsSection . scrollIntoView ( { behavior : 'smooth' } ) ;
137
- componentsLink . style . color = 'red' ;
138
- homeLink . style . color = '' ;
139
- } ) ;
140
-
141
- window . addEventListener ( 'scroll' , ( ) => {
142
- const currentScroll = window . pageYOffset ;
143
-
144
- // Adjust the offset accordingly
145
- const componentsSectionTop = componentsSection . offsetTop - 50 ;
146
-
147
- if ( currentScroll >= componentsSectionTop ) {
139
+ if ( homeLink && componentsLink && componentsSection ) {
140
+ componentsLink . addEventListener ( 'click' , ( event ) => {
141
+ event . preventDefault ( ) ;
142
+ componentsSection . scrollIntoView ( {
143
+ behavior : 'smooth'
144
+ } ) ;
148
145
componentsLink . style . color = 'red' ;
149
146
homeLink . style . color = '' ;
150
- } else {
151
- componentsLink . style . color = '' ;
152
- homeLink . style . color = 'red' ;
153
- }
154
- } ) ;
147
+ } ) ;
148
+
149
+ window . addEventListener ( 'scroll' , ( ) => {
150
+ const currentScroll = window . pageYOffset ;
151
+
152
+ // Adjust the offset accordingly
153
+ const componentsSectionTop = componentsSection . offsetTop - 50 ;
154
+
155
+ if ( currentScroll >= componentsSectionTop ) {
156
+ componentsLink . style . color = 'red' ;
157
+ homeLink . style . color = '' ;
158
+ } else {
159
+ componentsLink . style . color = '' ;
160
+ homeLink . style . color = 'red' ;
161
+ }
162
+ } ) ;
163
+ } else {
164
+ // If any of the navbar elements are missing, log a warning but don't stop the script
165
+ if ( ! homeLink ) console . warn ( "Home link not found" ) ;
166
+ if ( ! componentsLink ) console . warn ( "Components link not found" ) ;
167
+ if ( ! componentsSection ) console . warn ( "Components section not found" ) ;
168
+ }
155
169
} ) ;
0 commit comments