-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
125 lines (118 loc) · 4.74 KB
/
scripts.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
var lastPost = "";
var queued = false;
var sorting = decodeURIComponent(window.location.hash.slice(1)).split("?");
var end = false;
var isUriImage = function(uri) { //stolen from https://stackoverflow.com/a/19395606
//make sure we remove any nasty GET params
uri = uri.split('?')[0];
//moving on, split the uri into parts that had dots before them
var parts = uri.split('.');
//get the last part ( should be the extension )
var extension = parts[parts.length-1];
//define some image types to test against
var imageTypes = ['jpg','jpeg','tiff','png','gif','bmp', 'gifv'];
//check if the extension matches anything in the list.
if(imageTypes.indexOf(extension) !== -1) {
return true;
}
};
function chooseSort() {
window.location.hash = document.getElementById("sort").value;
location.reload();
}
function chooseTime() {
window.location.hash = document.getElementById("sort").value + "?" + document.getElementById("timeSelect").value;
location.reload();
}
function evalPost(postData) {
var link = document.createElement("a");
link.href = "https://reddit.com" + postData.permalink;
if(postData.url != "https://i.redd.it/n1oie6joe0vx.jpg") {
if(postData.url.startsWith("https://v.redd.it/")) {
var video = document.createElement("video");
video.autoplay = "true";
video.muted = "true";
video.loop = "true";
video.playsinline = "true";
video.src = postData.media.reddit_video.fallback_url + 'mp4';
link.appendChild(video);
} else {
if(isUriImage(postData.url) === true) {
var img = document.createElement("img");
img.src = postData.url;
img.alt = postData.title;
link.appendChild(img);
} else {
return;
}
}
lastPost = postData.name;
document.getElementById("container").appendChild(link);
}
}
function createPosts(sort, time) {
if(end === false) {
if(time != undefined) {
reddit[sort]("upvoteexeggutor").t(time).limit(20).after(lastPost).fetch(function (res) { //suprisingly it does not care if you provide an empty string as last post
for (var i = 0; i < res.data.children.length; i++) {
evalPost(res.data.children[i].data);
}
if(res.data.children.length < 19) {
var img = document.createElement("img");
img.src = "https://b.thumbs.redditmedia.com/B0Ekc6wMDxg-GjCcx-lLLKRJ05ujSm1zNN-EFhRJppE.png";
img.alt = "Legs";
document.getElementById("container").appendChild(img);
end = true;
}
queued = false;
});
} else {
reddit[sort]("upvoteexeggutor").limit(20).after(lastPost).fetch(function (res) {
for (var i = 0; i < res.data.children.length; i++) {
evalPost(res.data.children[i].data);
}
if(res.data.children.length < 19) {
var img = document.createElement("img");
img.src = "https://b.thumbs.redditmedia.com/B0Ekc6wMDxg-GjCcx-lLLKRJ05ujSm1zNN-EFhRJppE.png";
img.alt = "Legs";
document.getElementById("container").appendChild(img);
end = true;
}
queued = false;
});
}
}
}
function morePosts() {
if(sorting[0] != "") {
if(sorting[0] === "controversial" || sorting[0] === "top") {
document.getElementById("time").style.display = "revert";
if(sorting[1] != undefined) {
document.getElementById("sort").value = sorting[0];
document.getElementById("timeSelect").value = sorting[1];
createPosts(sorting[0], sorting[1]);
} else {
document.getElementById("sort").value = sorting[0];
document.getElementById("timeSelect").value = "all";
createPosts(sorting[0], document.getElementById("timeSelect").value);
}
} else {
createPosts(sorting[0]);
document.getElementById("sort").value = sorting[0];
}
} else {
createPosts(document.getElementById("sort").value);
}
}
(function () {
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
morePosts();
window.addEventListener("scroll",function(){
if((Math.max(document.body.offsetHeight - (window.pageYOffset + window.innerHeight), 0) < 5000) && queued === false) {
morePosts();
queued = true;
}
});
})();