-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
44 lines (37 loc) · 1.68 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
const quoteText = document.querySelector(".quote");
const quoteBtn = document.querySelector("button");
const authorName = document.querySelector(".author .name");
const soundBtn = document.querySelector(".sound");
const copyBtn = document.querySelector(".copy");
const twitterBtn = document.querySelector(".twitter");
function randomQuote() {
quoteBtn.classList.add("loading");
quoteBtn.innerText = "Loading Quote...";
// fetching random quotes/data from API and
//parsing it into javascript object
fetch("http://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", () => {
// the SpeechSynthesisUtterance is a web speech
//API that represents a speech request
let utterance = new SpeechSynthesisUtterance(`${quoteText.innerText} by ${authorName.innerText}`);
// speak method of SpeechSynthesis speak the Utterance
speechSynthesis.speak(utterance);
});
copyBtn.addEventListener("click", () => {
// copying the quote text on copyBtn click
// writeText() method writes the especified
//text string to the system clipboard.
navigator.clipboard.writeText(`${quoteText.innerText} \n- ${authorName.innerText}`);
});
twitterBtn.addEventListener("click", () => {
let tweetUrl = `https://twitter.com/intent/tweet?url=${quoteText.innerText} \n- ${authorName.innerText}`;
// Opening a new twitter tab with quote in the url
window.open(tweetUrl, "_blank");
});
quoteBtn.addEventListener('click', randomQuote);