This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils.js
375 lines (320 loc) · 11.7 KB
/
utils.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
var global_settings;
utils = {
debug: function(output) {
if (global_settings["debug"] === "on") {
if (typeof output === "string") {
if (global_settings["debug_unfiltered"] === "off") {
if (typeof global_plex_token != "undefined") {
output = output.replace(global_plex_token, "XXXXXXXXXXXXXXXXXXXX");
}
output = output.replace(/X-Plex-Token=[\w\d]{20}/, "X-Plex-Token=XXXXXXXXXXXXXXXXXXXX");
output = output.replace(/\d+\.\d+\.\d+\.\d+/, "XXX.XXX.X.XX");
}
console.log("Transmogrify for Plex log: " + output);
}
else {
// don't filter xml, use nodeType attribute to detect
if (global_settings["debug_unfiltered"] === "off" && !("nodeType" in output)) {
// clone object so we can filter out values
var output_ = {};
for (var key in output) {
if (output.hasOwnProperty(key)) {
output_[key] = output[key];
}
}
if ("access_token" in output_) {
output_["access_token"] = "XXXXXXXXXXXXXXXXXXXX";
}
if ("uri" in output_) {
output_["uri"] = "XXX.XXX.X.XX";
}
console.log(output_);
}
else {
console.log(output);
}
}
}
},
getExtensionVersion: function() {
var version = chrome.runtime.getManifest()["version"];
return version;
},
getOptionsURL: function() {
var options_url = chrome.extension.getURL("options.html");
return options_url;
},
getStatsURL: function() {
var stats_url = chrome.extension.getURL("stats.html");
return stats_url;
},
insertOverlay: function() {
// don't run if overlay exists on page
utils.debug("Checking if overlay already exists before creating");
var existing_overlay = document.getElementById("overlay");
if (existing_overlay) {
utils.debug("Overlay already exists. Passing");
return existing_overlay;
}
var overlay = document.createElement("div");
overlay.setAttribute("id", "overlay");
document.body.appendChild(overlay);
utils.debug("Inserted overlay");
return overlay;
},
background_storage_set: function(key, value) {
chrome.runtime.sendMessage({"type": "set", "key": key, "value": value});
},
background_storage_get: function(key, callback) {
chrome.runtime.sendMessage({"type": "get", "key": key}, function(results) {
callback(results);
});
},
storage_set: function(key, value) {
var hash = {};
hash[key] = value;
chrome.storage.sync.set(hash);
},
storage_get: function(key, callback) {
chrome.storage.sync.get(key, function(result) {
var value = result[key];
callback(value);
});
},
storage_get_all: function(callback) {
chrome.storage.sync.get(function(results) {
global_settings = results;
callback(results);
});
},
local_storage_set: function(key, value) {
var hash = {};
hash[key] = value;
chrome.storage.local.set(hash);
},
local_storage_get: function(key, callback) {
chrome.storage.local.get(key, function(result) {
var value = result[key];
callback(value);
});
},
local_storage_remove: function(key) {
chrome.storage.local.remove(key);
},
cache_set: function(key, data) {
utils.local_storage_get("cache_keys", function(cache_keys) {
// check if cache keys don't exist yet
if (!cache_keys) {
cache_keys = {};
}
// store cached url keys with timestamps
cache_keys[key] = {"timestamp": new Date().getTime()};
utils.local_storage_set("cache_keys", cache_keys);
// store cached data with url key
utils.local_storage_set(key, data);
});
},
cache_get: function(key, callback) {
utils.local_storage_get(key, function(result) {
if (result) {
utils.debug("Cache hit");
callback(result);
}
else {
utils.debug("Cache miss");
callback(null);
}
});
},
purgeStaleCaches: function(force) {
utils.local_storage_get("cache_keys", function(cache_keys) {
// check if there is any cached data yet
if (!cache_keys) {
utils.debug("No cached data, skipping cache purge");
return;
}
var time_now = new Date().getTime();
// iterate over cache keys and check if stale
for (var key in cache_keys) {
var timestamp = cache_keys[key]["timestamp"];
// 3 day cache
if (time_now - timestamp > 259200000 || force) {
utils.debug("Found stale data, removing " + key);
utils.local_storage_remove(key);
delete cache_keys[key];
utils.local_storage_set("cache_keys", cache_keys);
}
}
});
},
getResourcePath: function(resource) {
return chrome.extension.getURL("resources/" + resource);
},
getApiKey: function(api_name) {
var file_path = utils.getResourcePath("api_keys/" + api_name + ".txt");
var text;
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file_path, false);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
text = rawFile.responseText;
}
}
}
rawFile.send(null);
return text;
},
getXML: function(url, callback) {
utils.debug("Fetching XML from " + url);
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function(e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
utils.debug("Recieved XML response");
utils.debug(xhr.responseXML);
callback(xhr.responseXML);
}
else {
callback(xhr.statusText);
}
}
};
xhr.onerror = function() {
callback(xhr.statusText);
};
xhr.send();
},
getXMLWithTimeout: function(url, timeout, callback) {
utils.debug("Fetching XML from " + url);
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function(e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
utils.debug("Recieved XML response");
utils.debug(xhr.responseXML);
callback(xhr.responseXML);
}
else {
callback(xhr.statusText);
}
}
};
xhr.onerror = function() {
callback(xhr.statusText);
};
xhr.timeout = timeout;
xhr.ontimeout = function() {
callback(xhr.statusText);
};
xhr.send();
},
getJSONWithCache: function(url, callback, custom_headers) {
utils.debug("Fetching JSON from " + url);
utils.cache_get("cache-" + url, function(result) {
if (result) {
callback(result);
}
else {
// cache missed or stale, grabbing new data
utils.getJSON(url, function(result) {
utils.cache_set("cache-" + url, result);
callback(result);
}, custom_headers);
}
});
},
getJSON: function(url, callback, custom_headers) {
utils.debug("Fetching JSON from " + url);
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
for (var header_name in custom_headers) {
xhr.setRequestHeader(header_name, custom_headers[header_name])
}
xhr.onload = function(e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
utils.debug("Recieved JSON response");
utils.debug(xhr.responseText);
callback(JSON.parse(xhr.responseText));
}
else {
callback({"error": xhr.statusText});
}
}
};
xhr.onerror = function() {
callback({"error": xhr.statusText});
};
xhr.send();
},
setDefaultOptions: function(callback) {
utils.storage_get_all(function(settings) {
if (!("movie_trailers" in settings)) {
utils.storage_set("movie_trailers", "on");
}
if (!("letterboxd_link" in settings)) {
utils.storage_set("letterboxd_link", "on");
}
if (!("random_picker" in settings)) {
utils.storage_set("random_picker", "on");
}
if (!("random_picker_only_unwatched" in settings)) {
utils.storage_set("random_picker_only_unwatched", "off");
}
if (!("missing_episodes" in settings)) {
utils.storage_set("missing_episodes", "on");
}
if (!("rotten_tomatoes_link" in settings)) {
utils.storage_set("rotten_tomatoes_link", "off");
}
if (!("rotten_tomatoes_audience" in settings)) {
utils.storage_set("rotten_tomatoes_audience", "on");
}
if (!("rotten_tomatoes_citizen" in settings)) {
utils.storage_set("rotten_tomatoes_citizen", "non_us");
}
if (!("trakt_movies" in settings)) {
utils.storage_set("trakt_movies", "on");
}
if (!("trakt_shows" in settings)) {
utils.storage_set("trakt_shows", "on");
}
if (!("plex_server_uri" in settings)) {
utils.storage_set("plex_server_uri", "");
}
if (!("canistreamit" in settings)) {
utils.storage_set("canistreamit", "off");
}
if (!("imdb_link" in settings)) {
utils.storage_set("imdb_link", "on");
}
if (!("themoviedb_link" in settings)) {
utils.storage_set("themoviedb_link", "on");
}
if (!("tvdb_link" in settings)) {
utils.storage_set("tvdb_link", "off");
}
if (!("actor_profiles" in settings)) {
utils.storage_set("actor_profiles", "on");
}
if (!("stats_link" in settings)) {
utils.storage_set("stats_link", "on");
}
if (!("last_version" in settings)) {
utils.storage_set("last_version", "");
}
if (!("debug" in settings)) {
utils.storage_set("debug", "off");
}
if (!("debug_unfiltered" in settings)) {
utils.storage_set("debug_unfiltered", "off");
}
if (callback) {
callback(settings);
}
});
}
}