-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
executable file
·92 lines (84 loc) · 2.85 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
const quoteContainer = document.getElementById('quote-container');
const quoteText = document.getElementById('quote');
const authorText = document.getElementById('author');
const twitterBtn = document.getElementById('twitter');
const newQuoteBtn = document.getElementById('new-quote');
const movieQuoteBtn = document.getElementById('movie-quote');
const hackerQuoteBtn = document.getElementById('hacker-quote');
const janeQuoteBtn = document.getElementById('jane-quote');
const loader = document.getElementById('loader');
const quotes = document.getElementById('quote-text-container');
const showLoadingSpinner = () => {
loader.hidden = false;
quoteContainer.hidden = true;
};
const removeLoadingSpinner = () => {
if (!loader.hidden) {
quoteContainer.hidden = false;
loader.hidden = true;
}
};
const normalizeQuoteDataFromApis = (data) => {
let quoteText;
return (quoteText = data.quotes
? data.quotes[0]
: data.quoteText
? data.quoteText
: data.paragraphs[0]
? data.paragraphs[0].replace(/[<p>][<\/p>]/g, '')
: 'end');
};
const normalizeAuthorDataFromApis = (data) => {
let authorText;
return (authorText = data.quotes
? ' - Hacker'
: data.source
? `${data.source} - in character`
: data.quoteAuthor === ''
? 'Unknown'
: data.quoteAuthor);
};
const getNewQuote = async (apiURL) => {
showLoadingSpinner();
const proxyUrl = 'https://cors-anywhere.herokuapp.com/';
try {
quotes.classList.remove('remove');
const response = await fetch(proxyUrl + apiURL);
const data = await response.json();
authorText.innerText = normalizeAuthorDataFromApis(data);
quoteText.innerText = normalizeQuoteDataFromApis(data);
removeLoadingSpinner();
} catch (error) {
authorText.innerText = 'Oops, there is a problem! Try again';
quotes.classList.add('remove');
removeLoadingSpinner();
console.log(error);
}
};
const getWiseQuote = async () => {
getNewQuote('https://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json');
};
const getMovieQuote = async () => {
getNewQuote('https://devlorem.kovah.de/api/1');
};
const getHackerQuote = async () => {
getNewQuote('https://hackerman.wtf/api');
};
const getJaneAustinQuote = () => {
console.log(generate());
quoteText.classList.add('long-quote');
quoteText.innerHTML = generate();
authorText.innerText = 'Jane Austin';
};
const tweetQuote = () => {
const quote = quoteText.innerText;
const author = authorText.innerText;
const twitterUrl = `https://twitter.com/intent/tweet?text=${quote} - ${author}`;
window.open(twitterUrl, '_blank');
};
getWiseQuote();
newQuoteBtn.addEventListener('click', getWiseQuote);
movieQuoteBtn.addEventListener('click', getMovieQuote);
hackerQuoteBtn.addEventListener('click', getHackerQuote);
janeQuoteBtn.addEventListener('click', getJaneAustinQuote);
twitterBtn.addEventListener('click', tweetQuote);