-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
153 lines (123 loc) · 4.56 KB
/
main.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
//gets a random number between two bounds
function randomNumberFromRange(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
};
//for dank memes
var dankImage;
function loadImageDank() {
if(document.getElementById("images") != null) {
$("#images").empty();
}
var memeArray = ["https://www.reddit.com/r/Dank/.json", "https://www.reddit.com/r/dank_gifs/.json",
"https://www.reddit.com/r/dank_meme/.json", "https://www.reddit.com/r/dankmemes/.json"];
var randomMemeLink = randomNumberFromRange(0, memeArray.length);
$.getJSON(memeArray[randomMemeLink], function (data) {
var randomNumber = randomNumberFromRange(0, data.data.children.length);
try {
var imageUrl = data.data.children[randomNumber].data.url;
}catch(e){
//alert(e);//throws alert of error
loadImageDank();
}
//alert(imageUrl);
dankImage = imageUrl;
if(dankImage.includes("jpg") || dankImage.includes("jpeg") || dankImage.includes("png") || dankImage.includes("gif")) {
IsValidImageUrl(imageUrl,function(exists) {
$('<img/>').attr('src', data.data.children[randomNumber].data.url)
.width(500)
.appendTo('#images');
if(!exists){
alert("hello, invalid type!");
}
});
} else {
loadImageDank();
}
});
};
//for dark memes BEWAREEEE
var darkImage;
function loadImageDark(){
if(document.getElementById("images") != null) {
$("#images").empty();
}
var memeArray = ["https://www.reddit.com/r/CoffeeWithJesus/.json", "https://www.reddit.com/r/dark_humor/.json",
"https://www.reddit.com/r/Demotivational.json"];
var randomMemeLink = randomNumberFromRange(0, memeArray.length);
$.getJSON(memeArray[randomMemeLink], function (data) {
var randomNumber = randomNumberFromRange(0, data.data.children.length);
try {
var imageUrl = data.data.children[randomNumber].data.url;
}catch(e){
//alert(e);//throws alert of error
loadImageDark();
}
darkImage = imageUrl;
if(darkImage.includes("jpg") || darkImage.includes("jpeg") || darkImage.includes("png") || darkImage.includes("gif")) {
IsValidImageUrl(imageUrl,function(exists) {
while(exists.length == null || !exists) {
alert("hello, invalid type!");
loadImageDark();
}
$('<img/>').attr('src', data.data.children[randomNumber].data.url)
.width(500)
.appendTo('#images');
});
} else {
loadImageDark();
}
});
};
// for wholesome images
var wholesomeImage;
function loadImageWholesome(){
if(document.getElementById("images") != null) {
$("#images").empty();
}
var memeArray = ["https://www.reddit.com/r/Eyebleach/.json", "https://www.reddit.com/r/ShittyWholesomeMemes/.json",
"https://www.reddit.com/r/wholesomebestof/.json", "https://www.reddit.com/r/wholesomedogmemes/.json", "https://www.reddit.com/r/wholesomemes/.json"];
var randomMemeLink = randomNumberFromRange(0, memeArray.length);
$.getJSON(memeArray[randomMemeLink], function (data) {
var randomNumber = randomNumberFromRange(0, data.data.children.length);
try {
var imageUrl = data.data.children[randomNumber].data.url;
} catch(e) {
//alert(e);//throws alert of error
loadImageWholesome();
}
wholesomeImage = imageUrl;
if(wholesomeImage.includes("jpg") || wholesomeImage.includes("jpeg") || wholesomeImage.includes("png") ||
wholesomeImage.includes("gif")) {
IsValidImageUrl(imageUrl,function(exists) {
while(exists.length == null || !exists) {
alert("hello, invalid type!");
loadImageWholesome();
}
$('<img/>').attr('src', data.data.children[randomNumber].data.url)
.width(500)
.appendTo('#images');
});
} else {
loadImageWholesome();
}
});
};
//fix later doesnt return anything
function getURL(){
return image;
}
//Check Validity of URL
function IsValidImageUrl(url, callback) {
var img = new Image();
img.onerror = function () {
callback(url, false);
};
img.onload = function () {
callback(url, true);
};
img.src = url;
};
//Reload
function reloadPG(){
window.location.reload(true);
}