-
Notifications
You must be signed in to change notification settings - Fork 9
/
background.html
109 lines (97 loc) · 3.19 KB
/
background.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
<script>
var API_KEY = "YOUR_KEY_HERE";
var VISIVO_SET = "72157623563233239";
var PER_PAGE = "500";
var shortTimeout = 500;
var longTimeout = 5 * 60 * 1000;
var DEBUG = false;
function log (o) {
if (DEBUG) console.log(o);
}
// CONFIG OPTIONS
localStorage["useSmallerImages"] = true;
localStorage["showTitle"] = false;
//localStorage["showOwner"] = true;
function fetchPool(poolId, callback) {
var req = new XMLHttpRequest();
req.open(
"GET",
"http://api.flickr.com/services/rest/?" +
"method=flickr.groups.pools.getPhotos&" +
"api_key=" + API_KEY + "&" +
"group_id=" + poolId + "&" +
"extras=url_o&" +
"per_page=" + PER_PAGE,
true);
req.onload = function () { callback(req) };
req.send(null);
}
function fetchSet(photosetId, callback) {
var req = new XMLHttpRequest();
req.open(
"GET",
"http://api.flickr.com/services/rest/?" +
"method=flickr.photosets.getPhotos&" +
"api_key=" + API_KEY + "&" +
"photoset_id=" + photosetId + "&" +
"extras=url_o&" +
"per_page=" + PER_PAGE,
true);
req.onload = function () { callback(req) };
req.send(null);
}
function fillCache(req) {
var photoset = req.responseXML.getElementsByTagName("photoset");
var photos = req.responseXML.getElementsByTagName("photo");
log(photos);
// start adding some new ones
function againAndAgain() {
var photoCache = JSON.parse(localStorage["photoCache"])
addOnePhoto(photos);
if (photoCache.length < 30) {
window.setTimeout(againAndAgain, shortTimeout);
} else {
removeOnePhoto();
window.setTimeout(againAndAgain, longTimeout);
}
}
localStorage["photoCache"] = JSON.stringify(new Array());
againAndAgain();
}
function addOnePhoto(aPhotos) {
var whichPhoto = Math.round(Math.random() * (aPhotos.length - 1));
var photo = aPhotos[whichPhoto];
log("adding photo");
var img = document.createElement("image");
if (localStorage["useSmallerImages"]
&& photo.getAttribute("width_o") > 1280) {
img.src = constructImageUrl(photo);
} else {
img.src = photo.getAttribute("url_o");
}
document.body.appendChild(img);
var photoCache = JSON.parse(localStorage["photoCache"]);
var cached = {"src": img.src,
"title": photo.getAttribute("title"),
"id": photo.getAttribute("id")
}
cached.ownername = 'visivo';
cached.owner = 'visivo';
photoCache.push(cached);
localStorage["photoCache"] = JSON.stringify(photoCache);
}
function removeOnePhoto() {
var photoCache = JSON.parse(localStorage["photoCache"]);
photoCache.shift();
localStorage["photoCache"] = JSON.stringify(photoCache);
}
// See: http://www.flickr.com/services/api/misc.urls.html
function constructImageUrl(photo) {
return "http://farm" + photo.getAttribute("farm") +
".static.flickr.com/" + photo.getAttribute("server") +
"/" + photo.getAttribute("id") +
"_" + photo.getAttribute("secret") +
"_b.jpg";
}
fetchSet(VISIVO_SET, fillCache);
</script>