-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventPage.js
227 lines (189 loc) · 6.17 KB
/
eventPage.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*jslint browser: true*/
/*global $, DOMParser*/
/*global $, chrome*/
/*chrome $, extension*/
/*chrome.extension $, onMessage*/
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25154311-1']);
_gaq.push(['_trackPageview']);
(function () {
var ga, s;
ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
}());
function trackEvent(arg) {
if (arg.value !== undefined) {
_gaq.push(['_trackEvent', arg.category, arg.action, arg.label, arg.value]);
} else {
_gaq.push(['_trackEvent', arg.category, arg.action, arg.label]);
}
}
// Manifest info object
//noinspection JSUnresolvedVariable
window.chrome.manifest = (function () {
var manifestObject = "";
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
manifestObject = JSON.parse(xhr.responseText);
}
};
xhr.open("GET", window.chrome.extension.getURL('/manifest.json'), false);
try {
xhr.send();
} catch (e) {
console.log('Couldn\'t load manifest.json');
}
return manifestObject;
}());
function performHTTPGETRequest(url, auth, callback) {
var http = new window.XMLHttpRequest();
http.onreadystatechange = function () {
if (http.readyState === 4) {
if (http.status === 200) {
callback({success: true, value: http.responseText});
} else {
console.log("Failure (" + http.status + "): " + url);
callback({success: false, value: http.status});
}
}
};
http.open('GET', url, true);
if (auth !== null && auth !== "") {
http.setRequestHeader('Authorization', auth);
}
http.send();
}
function getURL(pageType, title) {
return "https://myanimelist.net/api/" + pageType + "/search.xml?q=" + encodeURIComponent(title);
}
function searchMAL(pageType, title, callback) {
var auth = "Basic QW5pbWVSYXRpbmdzOkNocm9tZTI=";
var url = getURL(pageType, title);
performHTTPGETRequest(url, auth, function (response) {
callback(response);
});
}
function setLocalStorage(key, value) {
try {
localStorage.setItem(key, value);
} catch (exc) {
console.log("Setting localStorage failed. Reason: " + exc);
console.log("Clearing localStorage.");
localStorage.clear();
}
}
function getEpochSeconds() {
return Math.floor(new Date().getTime() / 1000);
}
function getWeekCounter() {
return Math.floor(getEpochSeconds() / (7 * 24 * 3600));
}
function getCacheKey(pageType, title) {
return "version: " + window.chrome.manifest.version + ", age: " + getWeekCounter() + ", page_type: " + pageType + ", title: " + title;
}
function getMalQueryInfo(callback) {
var url = "http://stacked-crooked.googlecode.com/svn/trunk/Playground/AnimeRatings/ChromePlugin/config/malQuery.json";
var key = JSON.stringify({
name: "MalQueryInfo",
week: getWeekCounter(),
version: window.chrome.manifest.version
});
var value = localStorage.getItem(key);
if (value === null) {
console.log("Key '" + key + "' not in cache => perform API call.");
performHTTPGETRequest(url, "", function (response) {
setLocalStorage(key, JSON.stringify(response));
callback(response);
});
} else {
callback(JSON.parse(value));
}
}
function getInnerText(node) {
if (node.childNodes.length === 0) {
return "";
}
return node.childNodes[0].nodeValue;
}
function getMalInfo(arg, callback) {
var cacheValue = localStorage.getItem(getCacheKey(arg.pageType, arg.title));
if (cacheValue !== null) {
var result = JSON.parse(cacheValue);
callback(result);
return;
}
searchMAL(arg.pageType, arg.title, function (response) {
var result = {
success: false,
entries: []
};
if (response.success === false) {
callback(result);
return;
}
var i, parser, xmlText, doc, entries;
parser = new DOMParser();
xmlText = response.value;
xmlText = xmlText.replace(/&/g, "%26");
doc = parser.parseFromString(xmlText, "text/xml");
entries = doc.getElementsByTagName("entry");
for (i = 0; i < entries.length; i += 1) {
var node = entries[i];
// Get english title or original title.
var titles = node.getElementsByTagName("english");
// If there is no english title, use the original title.
if (titles.length === 0 || getInnerText(titles[0]).replace(/ /g, "") === "") {
titles = node.getElementsByTagName("title");
}
var scores = node.getElementsByTagName("score");
var ids = node.getElementsByTagName("id");
var start_dates = node.getElementsByTagName("start_date");
var end_dates = node.getElementsByTagName("end_date");
var types = node.getElementsByTagName("type");
if (titles.length === 1 && scores.length === 1 && ids.length === 1 &&
start_dates.length === 1 && end_dates.length === 1 && types.length === 1) {
var entry = {
pageType: arg.pageType,
title: getInnerText(titles[0]).replace(/%26/g, "&"),
score: getInnerText(scores[0]),
id: getInnerText(ids[0]),
start_date: getInnerText(start_dates[0]),
end_date: getInnerText(end_dates[0]),
type: getInnerText(types[0])
};
result.entries.push(entry);
} else {
console.log(arg.title + ": failed to parse xml!");
console.log(xmlText);
}
}
if (result.entries.length !== 0) {
result.success = true;
} else {
result.reason = "Failed to parse XML response.";
result.success = false;
}
setLocalStorage(getCacheKey(arg.pageType, arg.title), JSON.stringify(result));
callback(result);
});
}
chrome.extension.onMessage.addListener(function (request, sender, callback) {
if (request.action === "log") {
console.log(request.arg);
} else if (request.action === "getMalQueryInfo") {
getMalQueryInfo(callback);
} else if (request.action === "getMalInfo") {
getMalInfo(request.arg, callback);
} else if (request.action === "trackEvent") {
console.log("Tracking event: " + JSON.stringify(request.arg));
trackEvent(request.arg);
} else {
return false;
}
return true;
});