-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (43 loc) · 1.66 KB
/
index.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
document.querySelector('#search-btn').addEventListener('click', getQuote);
document.querySelector('#random-btn').addEventListener('click', getRandomQuote);
document.querySelector('.copy-btn').addEventListener('click', copyQuote);
function getQuote() {
let input = document.querySelector('.input').value;
fetch(`http://animechan.melosh.space/random/anime?title=${input}`)
.then(response => response.json())
.then(data => {
console.log(data);
if (!data.quote) {
alert('We cannot find this anime. Please try a different one.');
} else {
document.querySelector('.quote').innerText = `"${data.quote}"`;
document.querySelector('.character-name').innerText = `- ${data.character}`;
document.querySelector('.anime-name').innerText = data.anime;
renderQuote();
}
})
.catch(err => {
console.log(`error ${err}`);
});
}
function getRandomQuote() {
fetch('http://animechan.melosh.space/random')
.then(response => response.json())
.then(data => {
console.log(data);
document.querySelector('.quote').innerText = `"${data.quote}"`;
document.querySelector('.character-name').innerText = `${data.character}`;
document.querySelector('.anime-name').innerText = data.anime;
renderQuote();
})
.catch(err => {
console.log(`error ${err}`);
});
}
function renderQuote() {
document.querySelector('.quote-modal').classList.remove('hidden');
}
function copyQuote() {
navigator.clipboard.writeText(document.querySelector('.quote').textContent);
alert('Quote copied to clipboard');
}