-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
100 lines (82 loc) · 2.51 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
93
94
95
96
97
98
99
const quoteContainer = document.getElementById('container');
const quoteText = document.getElementById('quote');
const auteurText = document.getElementById('auteur');
const twitterBtn = document.getElementById('btn-twiit');
const Newq = document.getElementById('neaveaux');
const another=document.getElementById('another');
const loader = document.getElementById('loader');
function loadingPage() {
loader.hidden = false;
quoteContainer.hidden = true;
}
function removeLoading() {
if (!loader.hidden) {
quoteContainer.hidden = false;
loader.hidden = true;
}
}
async function getQuoteFromapi() {
let i=0;
loadingPage();
const proxyUrl = 'https://whispering-tor-04671.herokuapp.com/';
const apiUrl = 'http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json';
try {
const response = await fetch(proxyUrl + apiUrl);
const data= await response.json();
if (!data.quoteAuthor){
auteurText.innerText="Unknown";
}else{
auteurText.innerText=data.quoteAuthor;
}
another.innerText=`Another ${data.quoteAuthor} quotes`;
if (data.quoteText.length > 120) {
quoteText.classList.add('longe-text');
} else {
quoteText.classList.remove('longe-text');
}
quoteText.innerText=data.quoteText;
removeLoading();
} catch (error) {
if (i < 10){
getQuoteFromapi();
i = i + 1;
}
else{
alert("oops something is wrong with this page. Please try again .")
}
}
}
//get quotes from the same author
async function anotherquote(){
let i=0;
const proxy = 'https://whispering-tor-04671.herokuapp.com/';
const api = 'http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json';
try {
const response = await fetch(proxy + api);
const data= await response.json();
if (data.quoteAuthor=auteurText.textContent ){
quoteText.innerText=data.quoteText;
}else{
quoteText.innerText="none";
}
} catch (error) {
if (i < 10){
anotherquote();
i = i + 1;
}
else{
alert("oops something is wrong with this page. Please try again .")
}
}
}
//pub in twitter
function tweetQuote() {
const twitterUrl = `https://twitter.com/intent/tweet?text=${quoteText.textContent}-${auteurText.textContent}`;
window.open(twitterUrl, '_blank');
}
// Event Listeners
Newq.addEventListener('click', getQuoteFromapi);
another.addEventListener('click',anotherquote);
twitterBtn.addEventListener('click',tweetQuote);
// On Load
getQuoteFromapi();