-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (149 loc) · 5.19 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import reddit from "./redditapi.js";
const searchForm = document.getElementById("search-form");
const searchBtn = document.getElementById("search-btn");
const searchInput = document.getElementById("search-input");
const loadMore = document.querySelector("[data-load]");
searchForm.addEventListener("submit", (e) => {
const sortBy = document.querySelector('input[name="sortby"]:checked').value;
const searchLimit = document.getElementById("limit").value;
//console.log(searchLimit);
const searchTerm = searchInput.value;
if (searchTerm == "") {
showMessage("Please add a search term", "alert-danger");
}
document.title = `r/${searchTerm}`
// Search Reddit
rSearch(searchTerm, searchLimit, sortBy);
e.preventDefault();
});
loadMore.addEventListener("click", (e) => load(e));
function rSearch(searchTerm, searchLimit, sortBy) {
reddit.search(searchTerm, searchLimit, sortBy).then((results) => {
let output = '<div class="card-columns">';
//console.log(results);
results.forEach((post) => {
// Check for image
let image = post.preview
? post.preview.images[0].source.url
: "https://cdn.comparitech.com/wp-content/uploads/2017/08/reddit-1.jpg";
var converter = new showdown.Converter();
var md = post.selftext;
let text = converter.makeHtml(md);
//console.log(text)
output += `
<div class="card mb-2">
<img class="card-img-top" src="${image}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">${post.title}</h5>
<p class="card-text">${text}</p>
<a href="${post.url}" target="_blank
" class="btn btn-primary card-btn">Read More</a>
<hr>
<span class="badge badge-secondary">Subreddit: ${
post.subreddit
}</span>
<span class="badge badge-dark">Score: ${post.score}</span>
</div>
</div>
`;
});
output += "</div>";
document.getElementById("results").innerHTML = output;
loadMore.classList.remove("hidden");
});
}
function rLoadMore(searchTerm, searchLimit, sortBy) {
reddit.LoadMore(searchTerm, searchLimit, sortBy).then((results) => {
//console.log(results);
let divs = [];
results.forEach((post) => {
// Check for image
//console.log(post.selftext)
let image = post.preview? post.preview.images[0].source.url: "https://cdn.comparitech.com/wp-content/uploads/2017/08/reddit-1.jpg";
var converter = new showdown.Converter();
var md = post.selftext;
let text = converter.makeHtml(md);
let output = '<img class="card-img-top" ';
output += `
src="${image}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">${post.title}</h5>
<p class="card-text">${text}</p>
<a href="${post.url}" target="_blank
" class="btn btn-primary card-btn">Read More</a>
<hr>
<span class="badge badge-secondary ">Subreddit: ${
post.subreddit
}</span>
<span class="badge badge-dark">Score: ${post.score}</span>
</div>
`;
let div = document.createElement("div");
div.innerHTML = output;
divs.push(div);
});
//console.log(divs);
divs.forEach((div) => {
div.className += "card mb-2";
let isfound = [
...document.querySelectorAll("div.card-columns > div"),
].some((e) => {
return e == div;
});
console.log(isfound);
if (!isfound) document.querySelector("div.card-columns").appendChild(div);
});
// let div = document.createElement("div");
// div.classList.add("card mb-2");
// div.innerHTML= output;
});
}
function load(e) {
const sortBy = document.querySelector('input[name="sortby"]:checked').value;
const searchLimit = document.getElementById("limit").value;
const searchTerm = searchInput.value;
if (searchTerm == "") {
showMessage("Please add a search term", "alert-danger");
}
// Search Reddit
rLoadMore(searchTerm, searchLimit, sortBy);
//if (e !== undefined) e.preventDefault();console.log(e);
}
const delay = 500; // anti-rebound for 500ms
let lastExecution = 0;
const infinity = function (e) {
if (lastExecution + delay < Date.now()) {
if (
window.pageYOffset + window.innerHeight >
document.body.offsetHeight - 3000
) {
load();
console.log("loaded");
}
lastExecution = Date.now();
}
};
window.onscroll = infinity;
function showMessage(message, className) {
// Create div
const div = document.createElement("div");
// Add classes
div.className = `alert ${className}`;
// Add text
div.appendChild(document.createTextNode(message));
// Get parent
const searchContainer = document.getElementById("search-container");
// Get form
const search = document.getElementById("search");
// Insert alert
searchContainer.insertBefore(div, search);
// Timeout after 3 sec
setTimeout(function () {
document.querySelector(".alert").remove();
}, 3000);
}
function truncateString(myString, limit) {
const shortened = myString.indexOf(" ", limit);
if (shortened == -1) return myString;
return myString.substring(0, shortened);
}