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
153
154
155
156
157
|
<html>
<body>
<script>
var titleUrls = new Array();
var alreadySeen = new Array();
var storedSkipped = window.localStorage.getItem("skippedUrls");
if (storedSkipped == null)
skipped = new Array();
else
skipped = JSON.parse(storedSkipped);
var nothingForLast = false;
function getLevel()
{
var stored = window.localStorage.getItem("level");
if (stored == null)
return 1;
else
return parseInt(stored);
}
function setLevel(level)
{
if (level < 1 || level > 3)
return;
window.localStorage.setItem("level", level.toString());
titleUrls = new Array();
}
function getRequestObject()
{
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://anyclipmomentselector.appspot.com/request");
xhr.setRequestHeader("Content-type", "application/json");
return xhr;
}
function submitMoment(request, callback)
{
var xhr = getRequestObject();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4)
callback(xhr.status == 200 && xhr.responseText == "success");
};
xhr.send(JSON.stringify(request));
}
function popTitle()
{
var next = titleUrls.pop();
if (next != undefined)
alreadySeen.push(next);
return next;
}
function nextTitleUrl(callback)
{
if (titleUrls.length > 0 && callback != null)
callback(popTitle());
var xhr = getRequestObject();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText.length > 0) {
var doCallBack = titleUrls.length == 0;
var newTitles = JSON.parse(xhr.responseText);
if (newTitles.length == 0 && doCallBack) {
if (getLevel() == 1) {
alert("No titles left for level 1 :-(");
return;
}
setLevel(getLevel() - 1);
loadQuotePage();
alert("There are no titles available for level " + (getLevel() + 1).toString() + ", so I'm lowering you to level " + getLevel().toString() + ".");
return;
}
nothingForLast = newTitles.length == 0;
titleUrls = titleUrls.concat(newTitles);
if (callback != null && doCallBack)
callback(popTitle());
}
};
if (titleUrls.length == 0 && nothingForLast) {
nothingForLast = false;
return;
}
xhr.send(JSON.stringify(
{
"action": "getUrls",
"count": titleUrls.length == 0 ? 10 : 1,
"doNotSend": alreadySeen.concat(skipped).concat(titleUrls),
"level" : getLevel()
}));
}
//Populate list onload
nextTitleUrl(null);
function getAlreadySubmitted(request, callback)
{
var xhr = getRequestObject();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText.length > 0)
callback(JSON.parse(xhr.responseText));
};
xhr.send(JSON.stringify(request));
}
function unknownMovie(request)
{
if (skipped.indexOf(request.url) != -1)
return;
getRequestObject().send(JSON.stringify(request));
skipped.push(request.url);
var otherListIndex = alreadySeen.indexOf(request.url);
if (otherListIndex != -1)
alreadySeen.splice(otherListIndex, 1);
window.localStorage.setItem("skippedUrls", JSON.stringify(skipped));
}
chrome.extension.onRequest.addListener(function(request, sender, callback) {
if (request.action == "submitMoment")
submitMoment(request, callback);
else if (request.action == "nextTitleUrl")
nextTitleUrl(callback);
else if (request.action == "unknownMovie")
unknownMovie(request);
else if (request.action == "getAlreadySubmitted")
getAlreadySubmitted(request, callback);
else if (request.action == "setLevel") {
setLevel(request.level);
if (callback != null)
callback();
}
else if (request.action == "getLevel")
callback(getLevel());
});
function loadQuotePage()
{
nextTitleUrl(function(nextUrl) {
chrome.tabs.getAllInWindow(null, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
if (/http:\/\/.*imdb.com\/.*/.test(tabs[i].url)) {
chrome.tabs.update(tabs[i].id,
{
"url": nextUrl,
"selected": true
});
return;
}
}
chrome.tabs.create({"url": nextUrl});
});
});
}
chrome.browserAction.onClicked.addListener(loadQuotePage);
</script>
</body>
</html>
|