forked from Rakesh9100/CalcDiverse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
189 lines (158 loc) · 6.43 KB
/
script.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const hamburger = document.querySelector(".hamburger");
const navMenu = document.querySelector(".nav-menu");
document.addEventListener("DOMContentLoaded", function () {
setTimeout(function () {
document.querySelector("body").classList.add("loaded");
}, 500)
});
hamburger.addEventListener("click", mobileMenu);
function mobileMenu() {
hamburger.classList.toggle("active");
navMenu.classList.toggle("active");
}
const navLink = document.querySelectorAll(".nav-link");
navLink.forEach(n => n.addEventListener("click", closeMenu));
function closeMenu() {
hamburger.classList.remove("active");
navMenu.classList.remove("active");
}
const cont = document.getElementById('contributor');
const owner = 'Rakesh9100';
const repoName = 'CalcDiverse';
async function fetchContributors(pageNumber) {
const perPage = 100;
const url = `https://api.github.com/repos/${owner}/${repoName}/contributors?page=${pageNumber}&per_page=${perPage}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch contributors data. Status code: ${response.status}`);
}
const contributorsData = await response.json();
return contributorsData;
}
// Function to fetch all contributors
async function fetchAllContributors() {
let allContributors = [];
let pageNumber = 1;
try {
while (true) {
const contributorsData = await fetchContributors(pageNumber);
if (contributorsData.length === 0) {
break;
}
allContributors = allContributors.concat(contributorsData);
pageNumber++;
}
allContributors.forEach((contributor) => {
const contributorCard = document.createElement('div');
contributorCard.classList.add('contributor-card');
const avatarImg = document.createElement('img');
avatarImg.src = contributor.avatar_url;
avatarImg.alt = `${contributor.login}'s Picture`;
const loginLink = document.createElement('a');
loginLink.href = contributor.html_url;
loginLink.appendChild(avatarImg);
contributorCard.appendChild(loginLink);
cont.appendChild(contributorCard);
});
} catch (error) {
console.error(error);
}
}
fetchAllContributors();
let calcScrollValue = () => {
let scrollProg = document.getElementById("progress");
let pos = document.documentElement.scrollTop;
let calcHeight =
document.documentElement.scrollHeight -
document.documentElement.clientHeight;
let scrollValue = Math.round((pos * 100) / calcHeight);
if (pos > 100) {
scrollProg.style.display = "grid";
} else {
scrollProg.style.display = "none";
}
scrollProg.addEventListener("click", () => {
document.documentElement.scrollTop = 0;
});
scrollProg.style.background = `conic-gradient(#0063ba ${scrollValue}%, #d499de ${scrollValue}%)`;
};
window.addEventListener('scroll', function () {
var scrollToTopButton = document.getElementById('progress');
if (window.pageYOffset > 200) {
scrollToTopButton.style.display = 'block';
} else {
scrollToTopButton.style.display = 'none';
}
});
window.onscroll = calcScrollValue;
window.onload = calcScrollValue;
// Function to filter calculators
function filterCalculators() {
var input, filter, calculators, i;
input = document.getElementById('calculatorSearch');
filter = input.value.toUpperCase();
calculators = document.querySelectorAll('.container .box');
console.log(filter)
console.log(calculators)
for (i = 0; i < calculators.length; i++) {
var calculator = calculators[i];
var h2 = calculator.querySelector('h2');
var calculatorName = h2.textContent || h2.innerText;
if (calculatorName.toUpperCase().indexOf(filter) > -1) {
calculator.style.display = "flex";
} else {
calculator.style.display = "none";
}
}
}
// Voice command in search bar feature
const searchBar = document.querySelector("#searchBar");
const searchBarInput = searchBar.querySelector("input");
// The speech recognition interface lives on the browser’s window object
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; // if none exists -> undefined
if (SpeechRecognition) {
console.log("Your Browser supports speech Recognition");
const recognition = new SpeechRecognition();
recognition.continuous = true;
// recognition.lang = "en-US";
searchBar.insertAdjacentHTML("beforeend", '<button type="button"><i class="fas fa-microphone"></i></button>');
searchBarInput.style.paddingRight = "50px";
const micBtn = searchBar.querySelector("button");
const micIcon = micBtn.firstElementChild;
micBtn.addEventListener("click", micBtnClick);
function micBtnClick() {
if (micIcon.classList.contains("fa-microphone")) { // Start Voice Recognition
recognition.start(); // First time you have to allow access to mic!
}
else {
recognition.stop();
}
}
recognition.addEventListener("start", startSpeechRecognition); // <=> recognition.onstart = function() {...}
function startSpeechRecognition() {
micIcon.classList.remove("fa-microphone");
micIcon.classList.add("fa-microphone-slash");
searchFormInput.focus();
console.log("Voice activated, SPEAK");
}
recognition.addEventListener("end", endSpeechRecognition); // <=> recognition.onend = function() {...}
function endSpeechRecognition() {
micIcon.classList.remove("fa-microphone-slash");
micIcon.classList.add("fa-microphone");
searchBarInput.focus();
console.log("Speech recognition service disconnected");
}
recognition.addEventListener("result", resultOfSpeechRecognition); // <=> recognition.onresult = function(event) {...} - Fires when you stop talking
function resultOfSpeechRecognition(event) {
const current = event.resultIndex;
const transcript = event.results[current][0].transcript;
newtranscript = transcript.endsWith('.') ? transcript.slice(0, -1) : transcript;
console.log(newtranscript)
searchBarInput.value = newtranscript;
filterCalculators();
}
}
else {
console.log("Your Browser does not support speech Recognition");
info.textContent = "Your Browser does not support Speech Recognition";
}