-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKittyWales_background.js
130 lines (119 loc) · 4.49 KB
/
KittyWales_background.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
var bg = chrome.extension.getBackgroundPage();
bg.prefetchedRenderData = undefined;
bg.previousFlickrResult = undefined;
var init = function(action) {
var unconditional;
if (action == undefined) {
action = function(result) {
bg.previousFlickrResult = result;
// Passing this to withFlickrResult below will ensure randomness of the first image.
// 1st invocation: get number of photos, 2nd invocation: get random photo
withFlickrResult(update);
};
} else {
unconditional = true;
}
reset_opts(unconditional);
withFlickrResult(action);
};
var reset_opts = function(unconditional) {
if (localStorage['opt_searchterms'] == undefined || (unconditional !== undefined && unconditional)) {
localStorage['opt_searchterms'] = 'lolcat';
}
if (localStorage['opt_logic_op'] == undefined || (unconditional !== undefined && unconditional)) {
localStorage['opt_logic_op'] = 'all';
}
};
var update = function(flickrResult) {
bg.previousFlickrResult = flickrResult;
flickrToRenderable(flickrResult, storeRenderable);
};
var storeRenderable = function(renderable) {
bg.prefetchedRenderData = renderable;
}
var withFlickrResult = function(callback) {
var req = new XMLHttpRequest();
var cnt = bg.previousFlickrResult == undefined || bg.previousFlickrResult.photos == undefined ? 1 : bg.previousFlickrResult.photos.total;
cnt = cnt > 6000 ? 6000 : cnt; // 6000 because the max. admissible page number in the query is limited
var rnd = Math.ceil(Math.random() * cnt * 0.3); // 0.3 for sorting out less relevant photos
if (rnd > 0) {
var url = flickrUrl() + '&page=' + rnd;
req.open('GET', url);
req.onreadystatechange = function() {
if (req.readyState === 4 && req.status === 200) {
var res = JSON.parse(req.responseText);
var photo = res.photos.photo[0];
if (photo !== undefined) {
callback(res);
} else callback({});
}
};
req.send();
}
};
var flickrUrl = function() {
return 'http://api.flickr.com/services/rest/'
+ '?format=json&nojsoncallback=1'
+ '&sort=relevance'
+ '&per_page=1'
+ '&method=flickr.photos.search'
+ '&tags=' + encodeURIComponent(localStorage['opt_searchterms'])
+ '&tag_mode=' + encodeURIComponent(localStorage['opt_logic_op'])
+ '&api_key=8810bb1a272496e1973c19a8861dc084';
};
var flickrToRenderable = function(flickrResult, callback) {
if (flickrResult.photos !== undefined) {
var photo = flickrResult.photos.photo[0];
if (photo !== undefined) {
var imgUrl = 'http://farm' + photo.farm + '.static.flickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_m.jpg';
image = new Image();
image.onload = function() {
var height = this.naturalHeight + 'px';
callback( { count: flickrResult.photos.total,
imgUrl: imgUrl,
height: height,
searchterms: localStorage['opt_searchterms'],
searchmode: localStorage['opt_logic_op']
});
}
image.src = imgUrl;
}
} else {
callback( { count: 0,
imgUrl: 'http://upload.wikimedia.org/wikipedia/commons/thumb/3/37/Wikipedia-lolcat.jpg/220px-Wikipedia-lolcat.jpg',
height: 176,
searchterms: localStorage['opt_searchterms'],
searchmode: localStorage['opt_logic_op']
})
}
}
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.action == "reset") {
reset_opts(true);
init(function(result) {
bg.previousFlickrResult = result;
withFlickrResult(function(result2) {
update(result2);
flickrToRenderable(result2, sendResponse);
});
});
} else if (request.action == "getopt") {
var savedData = bg.prefetchedRenderData;
withFlickrResult(update);
sendResponse(savedData);
} else if (request.action == "setopt") {
withFlickrResult(function(result) {
update(result);
flickrToRenderable(result, sendResponse);
});
}
}
);
chrome.tabs.onUpdated.addListener(
function(tabId, changeInfo, tab) {
if ( changeInfo.status == 'complete' && tab.url.match(/https?:\/\/(\w+\.)?wikipedia\.org/) ) {
chrome.pageAction.show(tabId);
}
}
);