-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
81 lines (63 loc) · 3.1 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
const nextQuoteBtn = document.getElementById('next-button');
const quoteDes = document.getElementById('quote-container__quote');
const quoteAuthor = document.getElementById('author');
const twitterBtn = document.getElementById('twitter-button');
const loaderMain = document.getElementById('loader-main');
const quoteContainer = document.getElementById('quote-container');
const randomQuoteHandler = event => {
loaderMain.style.display = 'block';
quoteContainer.style.display = 'none';
const xhr = new XMLHttpRequest();
const quoteRequest = new Promise((resolve, reject) => {
xhr.open('GET', 'https://type.fit/api/quotes');
xhr.send();
xhr.onload = event => {
return resolve(xhr.response);
}
xhr.onerror = errEvent => {
return reject('Something went wrong, Check your Api or internet connection and then try again !');
}
});
quoteRequest.then(res => {
const quotes = JSON.parse(res);
const quotesLength = quotes.length;
const randomQuoteIndex = Math.floor(Math.random() * quotesLength);
const selectedRandomQuote = quotes[randomQuoteIndex];
quoteDes.textContent = selectedRandomQuote.text;
quoteAuthor.textContent = selectedRandomQuote.author || 'Unknown';
loaderMain.style.display = 'none';
quoteContainer.style.display = 'block';
})
// COVERED CORS ERRORS + JSON PARESE ERROR AND ANTOHER WAY TO GET DATA USING SIDE EFFECT THROGH fetch() api this time !
// .catch(err => {
// console.log(err);
// loaderMain.style.display = 'none';
// quoteContainer.style.display = 'block';
// });
// const getDynamicQuoteHandler = async () => {
// const corsUrl = 'https://cors-anywhere.herokuapp.com/'; // cors-violation solution, if not worked then open this link once in your browser atleast !
// const dynQuoteUrl = 'http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json';
// const response = await fetch(corsUrl + dynQuoteUrl);
// try {
// if(!response.ok) {
// const errRes = await response.json();
// throw new Error(errRes);
// }
// const responseData = await response.json();
// console.log(responseData);
// }catch(err) {
// console.log('Oops, Failed To fetch qoute from api, Try again: ', err + ' ...!');
// getDynamicQuoteHandler();
// }
// }
// getDynamicQuoteHandler();
}
const tweetHandler = async event => {
const quote = quoteDes.textContent;
const author = quoteAuthor.textContent || 'Unknown';
const url = encodeURI(`https://twitter.com/intent/tweet?text=${quote}\r\r\r~${author}`);
open(url.trim(), '_blank');
// location.assign(url.trim()); // for same page redirection.....
}
nextQuoteBtn.addEventListener('click', randomQuoteHandler);
twitterBtn.addEventListener('click', tweetHandler);