-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
40 lines (34 loc) · 1.57 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
const quoteText = document.querySelector(".quote"),
authorName = document.querySelector(".author .name"),
quoteBtn = document.querySelector("button"),
soundBtn = document.querySelector(".sound"),
copyBtn = document.querySelector(".copy"),
twitterBtn = document.querySelector(".twitter");
//random quote function
function randomQuote() {
quoteBtn.classList.add("loading")
quoteBtn.innerText = "Loading Quote..."
//fetching random quotes/data from API and parsing it into JavaScript Object
fetch("https://api.quotable.io/random").then(res=> res.json()).then(result => {
console.log(result)
quoteText.innerText = result.content;
authorName.innerText = result.author;
quoteBtn.innerText = "New Quote";
quoteBtn.classList.remove("loading");
});
}
soundBtn.addEventListener("click",()=> {
// the SpeechSynthesisUtterance is a web speech api 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 cpyBtn click
//writeText() property writes the specified text string to the system clipboard
navigator.clipboard.writeText(quoteText.innerText);
});
twitterBtn.addEventListener("click",()=> {
const 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);