-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (76 loc) · 3 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
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
const searchForm = document.getElementById("search-form");
const searchInput = document.getElementById("search-input");
const loader = document.getElementById("loader");
const searchText = document.getElementById("search-text");
searchForm.addEventListener("submit", async (e) => {
e.preventDefault();
const searchTerm = searchInput.value;
const sortBy = document.querySelector('input[name="sortby"]:checked').value;
const searchLimit = document.getElementById("limit").value;
if (searchTerm === "") {
showMessage("Please add a search term", "alert-danger");
loader.style.display = "none";
return;
}
loader.style.display = "flex";
searchText.style.display = "none";
const results = await searchReddit(searchTerm, searchLimit, sortBy);
loader.style.display = "none";
searchText.style.display = "flex";
showResults(results);
});
async function searchReddit(searchTerm, searchLimit, sortBy) {
try {
const response = await fetch(
`https://www.reddit.com/search.json?q=${searchTerm}&sort=${sortBy}&limit=${searchLimit}&include_facets=false&restrict_sr=on&type=comment`
);
const data = await response.json();
return data.data.children.map((data) => data.data);
} catch (error) {
console.log(error);
}
}
function showResults(results) {
let output = '<div class="card-columns">';
results.forEach((post) => {
output += `<div class="card">
<div class="card-body">
<h5 class="card-title">${post.title}</h5>
<a class="subreddit-link" href="https://www.reddit.com/r/${post.subreddit}" target="_blank">
<p class="card-text">r/${post.subreddit}</p>
</a>`;
if (post.is_video) {
output += `<video width="100%" height="auto" controls>
<source src="${post.media.reddit_video.fallback_url}" type="video/mp4">
</video>`;
} else if (post.thumbnail && post.thumbnail != "self") {
const preview = post.preview;
if (preview && preview.images && preview.images.length > 0) {
const thumbnail = preview.images[0].source.url.replace(/&/g, "&");
output += `<a href="${post.url}" target="_blank">
<img class="thumbnail" src="${thumbnail}" alt="">
</a>`;
} else {
output += `<a href="${post.url}" target="_blank">
<img class="thumbnail" src="${post.thumbnail}" alt="">
</a>`;
}
}
output += `<p class="card-text">${truncateText(post.selftext, 100)}</p>
<a class="subreddit-link" href="https://www.reddit.com/u/${
post.author
}" target="_blank"> <p class="card-text">u/${post.author}</p></a>
<a href="${
post.url
}" target="_blank" class="btn btn-dark">Read Post</a>
</div>
</div>`;
});
output += "</div>";
document.getElementById("results").innerHTML = output;
}
function truncateText(text, limit) {
const shortened = text.indexOf("", limit);
if (shortened == -1) return text;
return text.substring(0, shortened);
}