-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpic.html
139 lines (120 loc) · 3.66 KB
/
pic.html
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
<html>
<head>
<title>Random imgur image</title>
<style type="text/css">
img {
max-width: 100%;
height: auto;
}
#topbar {
width: 100%;
}
#link {
display: inline;
}
#title {
float: left;
padding-right: 15px;
font-weight: bold;
}
#rightbar {
float: right;
}
#input_max_cache {
width: 3em;
}
</style>
<script language="javascript" type="text/javascript">
var foundimgs = [];
var historyimgs = [];
var imgObject = new Image();
var numTries = 1;
var img_loading = false;
var cache_length = 25;
imgObject.onload = function() {
if (this.width == 161 && this.height == 81) {
this.src = "http://i.imgur.com/" + randomString(5) + ".jpg";
numTries++;
return;
}
foundimgs.push([numTries, this.src]);
numTries = 1;
img_loading = false;
if (historyimgs.length == 0) {
img_next();
// img_next() will cause loading of another img
return;
}
document.getElementById("cache").innerHTML = foundimgs.length;
// if less than cache_length, search for more
if (foundimgs.length < cache_length)
getImages();
};
imgObject.onerror = function() { img_loading = false; getImages(); };
function randomString(string_length) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz";
var output = "";
var index;
for (var i = 0; i < string_length; i++) {
var index = Math.floor(Math.random() * chars.length);
output += chars.substring(index, index + 1);
}
return output;
}
function getImages() {
if (img_loading) return;
img_loading = true;
imgObject.src = "http://i.imgur.com/" + randomString(5) + ".jpg";
document.getElementById("input_max_cache").value = cache_length;
}
function img_next() {
// load next image
if (foundimgs.length == 0) return;
var img_data = foundimgs.shift();
historyimgs.push(img_data);
img_show(img_data);
document.getElementById("cache").innerHTML = foundimgs.length;
if (foundimgs.length < 25)
getImages();
}
function img_prev() {
// go one image back
if (historyimgs.length <= 1) return; // Can't go back
foundimgs.unshift(historyimgs.pop());
document.getElementById("cache").innerHTML = foundimgs.length;
img_show(historyimgs[historyimgs.length - 1]);
}
function img_show(data) {
var controls = "Tried "+data[0]+" times. ";
if (historyimgs.length > 1)
controls += "<a href=\"javascript:img_prev();\"><<<</a> ";
controls += "<a href=\"javascript:img_next();\">>>></a>";
var link = "<a href=\"" + data[1] + "\">" + data[1];
var html = "<img src=\"" + data[1] + "\" /></a>";
document.getElementById('a').innerHTML = html;
document.getElementById('controls').innerHTML = controls;
document.getElementById('link').innerHTML = link;
}
function updateCacheLength() {
cache_length = document.getElementById('input_max_cache').value;
getImages();
}
document.onkeydown = function(e) {
e = e || window.event;
if (e.keyCode == 37) {
img_prev();
} else if (e.keyCode == 39) {
img_next();
}
};
</script>
</head>
<body onLoad="getImages();">
<div id="topbar">
<div id="title">Random Imgur Image</div>
<div id="link"></div>
<div id="rightbar"><span id="controls"></span> Cache status: <span id="cache">0</span>/<input id="input_max_cache" name="max_cache" onChange="updateCacheLength();"></input></div>
</div>
<div id="a">Please wait...</div>
</body>
</html>