-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathscript.js
More file actions
47 lines (42 loc) · 1.31 KB
/
script.js
File metadata and controls
47 lines (42 loc) · 1.31 KB
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
const memeImage = document.getElementById('meme-image');
const newMemeButton = document.getElementById('new-meme');
const downloadMemeButton = document.getElementById('download-meme');
const shareMemeButton = document.getElementById('share-meme');
// Fetch random meme from the API
async function fetchMeme() {
try {
const response = await fetch('https://api.imgflip.com/get_memes');
const data = await response.json();
const { memes } = data.data;
const randomMeme = memes[Math.floor(Math.random() * memes.length)];
memeImage.src = randomMeme.url;
} catch (error) {
console.error('Error fetching meme:', error);
}
}
// Download the meme
const downloadMeme = () => {
const memeUrl = memeImage.src;
if (memeUrl) {
const a = document.createElement('a');
a.href = memeUrl;
a.download = 'meme.jpg';
a.click();
}
};
// Share the meme
const shareMeme = () => {
const memeUrl = memeImage.src;
if (memeUrl) {
const shareUrl = `https://twitter.com/intent/tweet?url=${encodeURIComponent(
memeUrl,
)}`;
window.open(shareUrl, '_blank');
}
};
// Event listeners
newMemeButton.addEventListener('click', fetchMeme);
downloadMemeButton.addEventListener('click', downloadMeme);
shareMemeButton.addEventListener('click', shareMeme);
// Load an initial meme on page load
fetchMeme();