-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
43 lines (33 loc) · 1.53 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
const quoteText = document.querySelector('.quote'),
authorName = document.querySelector('.name'),
quoteBtn = document.querySelector('button'),
soundBtn = document.querySelector('.sound'),
copyBtn = document.querySelector('.copy'),
twitterBtn = document.querySelector('.twitter')
function randomQuote() {
quoteBtn.classList.add('loading')
quoteBtn.innerText = 'Loading Quote...'
fetch('https://api.quotable.io/random')
.then((res) => res.json())
.then((result) => {
quoteText.innerText = result.content;
authorName.innerText = result.author;
quoteBtn.innerText = 'New Quote';
quoteBtn.classList.remove('loading');
})
}
soundBtn.addEventListener('click', () => {
// SpeechSynthesisUtterance is a web speech spi that represents a speech request
let utterance = new SpeechSynthesisUtterance(`${quoteText.innerText} by ${authorName.innerText}`);
speechSynthesis.speak(utterance); // speak method of speechsynthesis speaks the utterance
});
copyBtn.addEventListener('click', () => {
// copying the quote text on copyBtn click
// writeText() property writes the specified text string to the system clipboard
navigator.clipboard.writeText(quoteText.innerText);
});
twitterBtn.addEventListener('click', () => {
let tweetUrl = `https://twitter.com/intent/tweet?url=${quoteText.innerText}`;
window.open(tweetUrl, "_blank"); //opening a new twitter tab with passing quote in the url
});
quoteBtn.addEventListener('click', randomQuote);