-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
101 lines (92 loc) · 3.24 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
100
101
//Thanks to https://github.com/D3vd/Meme_Api
let redditURL = 'https://www.reddit.com/subreddits/search.json?q=memes';
let apiURL = "https://meme-api.com/gimme/";
let reditSub;
let defaultSub = "wholesomememes";
let memeImg, memeTitle, memeBtn, subredditSearch, dataList;
function init()
{
memeImg = document.getElementById('memeImg');
memeTitle = document.getElementById('memeTitle');
memeBtn = document.getElementById("memeBtn");
subredditSearch = document.getElementById("subredditSearch");
dataList = document.getElementById('subreddits');
//otherwise won't load on page load
reditSub = defaultSub;
//events
memeBtn.addEventListener("click", function ()
{
fetchMeme();
});
subredditSearch.addEventListener("input", function (event)
{
reditSub = sanitizeInput(event.target.value);
// redditSub = event.target.value;
// console.log(sanitizeInput(event.target.value));
});
fetchSubreddits();
fetchMeme();
}
function fetchSubreddits()
{
// Fetch the list of meme subreddits from the Reddit API
console.log("fetching:" + redditURL);
fetch(redditURL)
.then(response => response.json())
.then(data =>
{
const subredditList = data.data.children; // Access the list of subreddits
// Loop through each subreddit and add it to the datalist if it matches the keywords
subredditList.forEach(sub =>
{
const option = document.createElement('option');
option.value = sub.data.display_name; // Set the display name as the value
dataList.appendChild(option); // Add option to the datalist
console.log("appended");
});
})
.catch(error => console.error('Error fetching subreddits:', error));
}
function fetchMeme()
{
console.log("Fetching:" + apiURL + reditSub);
fetch(apiURL + reditSub)
.then(response => response.json())
.then(data =>
{
if (!data || !data.url)
{ // Check if 'data' is null/undefined or if it doesn't have the 'url' property
memeTitle.textContent = 'No meme found'; // Update the title to reflect the error
memeImg.src = "";
} else
{
// Check if the meme is safe for work
if (!data.nsfw)
{
memeImg.src = data.url;
memeTitle.textContent = data.title; // Set the title
memeImg.style.display = 'block'; // Make the image visible
memeTitle.style.display = 'block'; // Make the title visible
} else
{
console.warn('NSFW content detected. Skipping this meme.');
fetchMeme(); // Try to fetch another meme if NSFW is true
}
}
})
.catch(error => console.error('Error fetching meme:', error));
}
function sanitizeInput(input)
{
return input.replace(/[&<>"']/g, (match) =>
{
const escapeMap = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
};
return escapeMap[match];
});
}