-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (54 loc) · 2.13 KB
/
main.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
// DOM Manipulation
const quoteContainer = document.getElementById('quote-container');
const quoteTextTam01 = document.getElementById('quotetamil-1');
const quoteTextTam02 = document.getElementById('quotetamil-2');
const quoteTextTrn = document.getElementById('quote-text-trn');
const twitterBtn = document.getElementById('twitter');
const nextKural = document.getElementById('next-kural');
// Loader functions loading() and complete()
//
// Loader function - Display Loader hide quote container
function loading(){
loader.hidden = false; // This will display loader
quoteContainer.hidden = true; // This will hide quote container
}
// Loader complete function - Display Quote container and hide loader
function complete(){
if (!loader.hidden){
quoteContainer.hidden = false;
loader.hidden = true;
}
}
// End of loader functions.
// async method and await
async function getKural(){
loading();
const appId = 'eenoesvbwbika';
let apiUrl = 'https://getthirukkural.appspot.com/api/2.0/kural/rnd?appid={appid}&format=json';
apiUrl = apiUrl.replace("{appid}", appId);
try{
const response = await fetch (apiUrl);
const data = await response.json();
quoteTextTam01.innerText = data.KuralSet.Kural[0].Line1;
quoteTextTam02.innerText = data.KuralSet.Kural[0].Line2;
quoteTextTrn.innerText = data.KuralSet.Kural[0].Translation;
//console.log(data.KuralSet.Kural[0].Line1);
complete();
}catch(error){
console.log('Error message ' ,error);
getKural();
}
}
// Twitter posting
function twitterPost(){
const kuralTam01 = quoteTextTam01.innerText;
const kuralTam02 = quoteTextTam02.innerText;
const kuraltrn = quoteTextTrn.innerText;
const twitterUrl = `https://twitter.com/intent/tweet?text=${kuralTam01}%0A${kuralTam02}%0A - Translation:${kuraltrn}`;
window.open(twitterUrl, '_blank');
}
// Adding event Listeners
nextKural.addEventListener('click',getKural);
twitterBtn.addEventListener('click', twitterPost);
//Call getKural function
getKural();