-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
82 lines (67 loc) · 2.47 KB
/
popup.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
// Development key, try to limit api calls as much as possible,
// because we're limited to only 250 api calls on this key
var api_key = "SRITA3jTGnQB9rbSfKwjXEcqKUkgqO"
var base_url = "http://api-public.guidebox.com/v1.43/US/" + api_key;
document.getElementById("search-button").addEventListener("click", function() {
searchShows(document.getElementById('search-text').value);
});
function searchShows(searchText) {
var query = base_url + "/search/title/" +
encodeURIComponent(searchText) + "/fuzzy";
var request = new XMLHttpRequest();
request.open( "GET", query, true);
request.onreadystatechange = function () {
if (request.readyState !== XMLHttpRequest.DONE) {
return;
}
if (request.status !== 200) {
console.log("There was an issue: " + request.status);
}
var json = JSON.parse(request.responseText);
var results = json.results;
console.log(results);
listResults(results);
};
request.send();
}
function listResults(results) {
var resultsDiv = document.getElementById('results');
while (resultsDiv.firstChild) {
resultsDiv.removeChild(resultsDiv.firstChild);
}
for (i in results) {
var result = results[i];
var img = document.createElement('img');
img.className = 'result-image';
img.setAttribute('src', result.artwork_208x117);
img.setAttribute('title', result.title);
// Refer to:
// http://stackoverflow.com/questions/8909652/adding-click-event-listeners-in-loop
(function (_result) {
img.addEventListener('click', function(){
// Hardcoded source for now
// goToShowPage(_result.id, "hulu_plus");
console.log(_result.title);
});
})(result);
resultsDiv.appendChild(img);
}
}
//episodes/all/0/25/all/all
function goToShowPage(id, source) {
var query = base_url + "/show/" + id + "/episodes/all/0/100/" + source + "/all"
var request = new XMLHttpRequest();
request.open( "GET", query, true);
request.onreadystatechange = function () {
if (request.readyState !== XMLHttpRequest.DONE) {
return;
}
if (request.status !== 200) {
console.log("There was an issue: " + request.status);
}
var json = JSON.parse(request.responseText);
var results = json.results;
console.log(results);
};
request.send();
}