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
/
main.js
564 lines (483 loc) · 23.8 KB
/
main.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
var show_update_text = false;
var update_text = "Version 1.3.4 is here. Lots of bug fixes and changes, a full list of which can be found in the <a id='stats-page-link' href='https://forums.plex.tv/index.php/topic/99209-transmogrify-for-plex-a-browser-extension-that-adds-features-to-plexweb' target='_blank'>forum thread</a>"
var settings;
var global_plex_token;
var global_server_addresses;
function checkIfUpdated() {
var last_version = settings["last_version"];
var version = utils.getExtensionVersion();
if (last_version != version && show_update_text) {
showUpdatePopup();
settings["last_version"] = version;
utils.storage_set("last_version", version);
}
}
function showUpdatePopup() {
var options_url = utils.getOptionsURL();
var stats_url = utils.getStatsURL();
var formatted_update_text = update_text.replace("%OPTIONSURL%", options_url).replace("%STATSPAGEURL%", stats_url);
showPopup("New update! - " + formatted_update_text);
}
function closePopup() {
var popup_container = document.getElementById("update-box");
popup_container.parentNode.removeChild(popup_container);
var overlay = document.getElementById("overlay");
overlay.style.display = "none";
overlay.removeEventListener("click", closePopup, false);
}
function showPopup(messsage) {
var overlay = utils.insertOverlay();
overlay.style.display = "block";
var popup_container = document.createElement("div");
popup_container.setAttribute("class", "update-box");
popup_container.setAttribute("id", "update-box")
var logo = document.createElement("img");
logo.setAttribute("src", utils.getResourcePath("icon_transparent.png"));
var message = document.createElement("p");
message.innerHTML = messsage;
popup_container.appendChild(logo);
popup_container.appendChild(message);
overlay.appendChild(popup_container);
var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
if (is_firefox) {
try {
document.getElementById("options-page-link").addEventListener("click", utils.openOptionsPage, false);
}
catch(e) {
}
try {
document.getElementById("stats-page-link").addEventListener("click", utils.openStatsPage, false);
}
catch(e) {
}
}
overlay.addEventListener("click", closePopup, false);
}
function runOnReady() {
utils.debug("runOnReady called. Starting watch");
var page_url = document.URL;
var interval = window.setInterval(function() {
if (document.URL != page_url) {
window.clearInterval(interval);
}
if ((/index\.html\#?$/.test(document.URL)) || (/http:\/\/plex\.tv\/web\/app\#?$/.test(document.URL))) {
if (document.getElementsByTagName("h2").length > 0) {
utils.debug("Instance of h2 tag detected. Page is ready");
window.clearInterval(interval);
main();
}
}
// page is ready when certain elements exist.
// check if on library section
else if (/\/section\/\d+$/.test(document.URL)) {
if (document.getElementsByClassName("media-poster").length > 0) {
utils.debug("Instance of .media-poster detected. Page is ready");
window.clearInterval(interval);
main();
}
}
// check if on movie/tv show details page
else if (/\/details\/%2Flibrary%2Fmetadata%2F(\d+)$/.test(document.URL)) {
if (document.getElementsByClassName("item-title").length > 0 || document.getElementsByClassName("show-title").length > 0) {
utils.debug("Instance of .item-title or .show-title detected. Page is ready");
window.clearInterval(interval);
main();
}
}
else {
utils.debug("runOnReady not on recognized page");
window.clearInterval(interval);
}
}, 0);
}
function insertPlexToken() {
var plex_token = PLEXWEB.myPlexAccessToken;
if (plex_token) {
document.body.setAttribute("data-plextoken", plex_token);
}
}
function getPlexToken() {
if (global_plex_token) {
utils.debug("plex_token is cached - " + global_plex_token);
return global_plex_token;
}
else if (localStorage["myPlexAccessToken"]) {
global_plex_token = localStorage["myPlexAccessToken"];
utils.debug("plex_token fetched from localStorage - " + localStorage["myPlexAccessToken"]);
return localStorage["myPlexAccessToken"];
}
else {
var plex_token = document.body.getAttribute("data-plextoken");
if (plex_token === null) {
// remove existing script if run before
if (document.getElementById("plex-token-script")) {
document.body.removeChild(document.getElementById("plex-token-script"));
}
utils.debug("Inserting plex_token into document body");
var script = document.createElement("script");
script.setAttribute("id", "plex-token-script")
script.appendChild(document.createTextNode("("+ insertPlexToken +")();"));
document.body.appendChild(script);
plex_token = document.body.getAttribute("data-plextoken");
}
global_plex_token = plex_token;
utils.debug("plex_token fetched from document body - " + plex_token);
return plex_token;
}
}
function insertLoadingIcon() {
var nav_bar_right = document.body.getElementsByClassName("nav-bar-right")[0];
var list_element = document.createElement("li");
list_element.setAttribute("id", "loading-extension");
var img = document.createElement("img");
img.setAttribute("src", utils.getResourcePath("loading_extension.gif"));
list_element.appendChild(img);
nav_bar_right.insertBefore(list_element, nav_bar_right.firstChild);
}
function removeLoadingIcon() {
var loading_icon = document.getElementById("loading-extension");
if (loading_icon) {
loading_icon.parentNode.removeChild(loading_icon);
}
}
function getServerAddresses(requests_url, plex_token, callback) {
if (global_server_addresses) {
utils.debug("Server addresses are already cached");
utils.debug(global_server_addresses);
callback(global_server_addresses);
} else {
utils.debug("Fetching server addresses");
if (!document.getElementById("loading-extension"))
insertLoadingIcon();
var xml_lookup_tag_name, request_path;
// Lookups are different if we are going to plex.tv as opposed to a local IP address
if (plex_token) {
xml_lookup_tag_name = "Device";
request_path = "/resources?includeHttps=1";
} else {
xml_lookup_tag_name = "Server";
request_path = "/servers?includeLite=1";
}
utils.getXML(requests_url + request_path + "&X-Plex-Token=" + plex_token, function(servers_xml) {
var devices = servers_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName(xml_lookup_tag_name);
var task_counter = 0;
var task_completed = function() {
utils.debug("Server ping task finished");
task_counter--;
// Check if all async tasks are finished
if (task_counter === 0) {
utils.debug("All server ping tasks finished");
utils.debug("Server addresses fetched");
for (var machine_identifier in server_addresses) {
utils.debug(server_addresses[machine_identifier]);
}
// Remove offline servers, which return a blank address from plex.tv
for (var machine_identifier in server_addresses) {
if (server_addresses[machine_identifier]["uri"] === "") {
utils.debug("Removing offline server - " + machine_identifier);
delete server_addresses[machine_identifier];
}
}
// Pass server addresses to background for stats page
utils.background_storage_set("server_addresses", server_addresses);
// Set global_server_addresses so results are cached
global_server_addresses = server_addresses;
removeLoadingIcon();
callback(server_addresses);
}
};
var server_addresses = {};
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
var connections = device.hasAttribute("address") ? [device] : device.getElementsByTagName("Connection");
var name = device.getAttribute("name");
var machine_identifier = device.hasAttribute("machineIdentifier") ? device.getAttribute("machineIdentifier") : device.getAttribute("clientIdentifier");
var access_token = device.getAttribute("accessToken");
for (var j = 0; j < connections.length; j++) {
var connection = connections[j];
var uri = connection.hasAttribute("uri") ? connection.getAttribute("uri") : window.location.protocol + "//" + connection.getAttribute("address") + ":" + connection.getAttribute("port");
var local = !connection.hasAttribute("uri") || connection.getAttribute("local") == 1;
task_counter += 1;
(function (machine_identifier, name, uri, access_token, local) {
utils.getXMLWithTimeout(uri + "?X-Plex-Token=" + access_token, 2000, function(server_xml) {
// Use address if we can reach it
if (server_xml && server_xml != "Unauthorized" && server_xml.getElementsByTagName("MediaContainer")[0].getAttribute("machineIdentifier") === machine_identifier) {
utils.debug("Using address for " + machine_identifier + " - " + uri);
if (server_addresses[machine_identifier] && local) {
// Local devices should override any remote
utils.debug("Using local address for " + machine_identifier + " - " + uri);
server_addresses[machine_identifier]["uri"] = uri;
} else if (!server_addresses[machine_identifier]) {
// We found our first match!
utils.debug("Using address for " + machine_identifier + " - " + uri);
server_addresses[machine_identifier] = {
"name": name,
"machine_identifier": machine_identifier,
"access_token": access_token,
"uri": uri
};
}
} else {
utils.debug("Failed to ping address for " + machine_identifier + " - " + uri);
}
task_completed();
});
}(machine_identifier, name, uri, access_token, local));
}
}
});
}
}
function processLibrarySections(sections_xml) {
var directories = sections_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory");
var dir_metadata = {};
for (var i = 0; i < directories.length; i++) {
var type = directories[i].getAttribute("type");
var section_num = directories[i].getAttribute("path").match(/\/(\d+)$/)[1];
var machine_identifier = directories[i].getAttribute("machineIdentifier");
if (machine_identifier in dir_metadata) {
dir_metadata[machine_identifier][section_num] = {"type": type, "section_num": section_num};
}
else {
dir_metadata[machine_identifier] = {};
dir_metadata[machine_identifier][section_num] = {"type": type, "section_num": section_num};
}
}
utils.debug("Parsed library sections");
utils.debug(dir_metadata);
return dir_metadata;
}
function main() {
utils.debug("Running main()");
// show popup if updated
checkIfUpdated();
var page_url = document.URL;
var plex_token = getPlexToken();
// add observer for fast user switching functionality, to reload token and server addresses
var observer = new MutationObserver(function(mutations) {
observer.disconnect();
utils.debug("User switched");
global_server_addresses = null;
global_plex_token = null;
runOnReady();
});
observer.observe(document.getElementsByClassName("dropdown-poster-container")[0], {subtree: true, childList: true});
// use plex.tv for API requests if we have plex token, otherwise use server URL
// as user is on local server and not signed in
var requests_url;
if (plex_token) {
requests_url = "https://plex.tv/pms";
}
else {
var url_matches = page_url.match(/^https?\:\/\/(.+):(\d+)\/web\/.+/);
requests_url = window.location.protocol + "//" + url_matches[1] + ":" + url_matches[2];
}
utils.debug("requests_url set as " + requests_url);
getServerAddresses(requests_url, plex_token, function(server_addresses) {
// insert stats page link
if (settings["stats_link"] === "on") {
utils.debug("stats plugin is enabled");
stats.init();
}
else {
utils.debug("stats plugin is disabled");
}
// check if on dashboard page
if ((/index\.html\#?$/.test(page_url)) || (/http:\/\/plex\.tv\/web\/app\#?$/.test(page_url))) {
utils.debug("main detected we are on dashboard page");
// only purge caches when viewing main page
utils.purgeStaleCaches();
}
// check if on library section
else if (/\/section\/\d+$/.test(page_url)) {
utils.debug("main detected we are in library section");
var page_identifier = page_url.match(/\/server\/(.[^\/]+)\/section\/(\d+)$/);
var machine_identifier = page_identifier[1];
var section_num = page_identifier[2];
utils.debug("machine identifier - " + machine_identifier);
utils.debug("library section - " + section_num);
// get library sections xml
var library_sections_url = requests_url + "/system/library/sections?X-Plex-Token=" + plex_token;
utils.getXML(library_sections_url, function(sections_xml) {
var library_sections = processLibrarySections(sections_xml);
var server;
if (server_addresses) {
server = server_addresses[machine_identifier];
}
else {
server = {};
}
var section = library_sections[machine_identifier][section_num];
// override server address if defined in settings
if (settings["plex_server_uri"] != "") {
utils.debug("Plex server manual override");
server["uri"] = settings["plex_server_uri"];
}
if (settings["random_picker"] === "on") {
utils.debug("random_picker plugin is enabled");
random_picker.init(server, section, settings["random_picker_only_unwatched"]);
}
else {
utils.debug("random_picker plugin is disabled");
}
});
}
// check if on movie/tv show details page
else if (/\/details\/%2Flibrary%2Fmetadata%2F(\d+)$/.test(page_url)) {
utils.debug("main detected we are on movie/tv show details page");
var page_identifier = page_url.match(/\/server\/(.[^\/]+)\/details\/%2Flibrary%2Fmetadata%2F(\d+)$/);
var machine_identifier = page_identifier[1];
var parent_item_id = page_identifier[2];
utils.debug("metadata id - " + parent_item_id);
var server = server_addresses[machine_identifier];
// override server address if defined in settings
if (settings["plex_server_uri"] != "") {
utils.debug("Plex server manual override");
server["uri"] = settings["plex_server_uri"];
}
// construct metadata xml link
utils.debug("Fetching metadata for id - " + parent_item_id);
var metadata_xml_url = server["uri"] + "/library/metadata/" + parent_item_id + "?X-Plex-Token=" + server["access_token"];
// fetch metadata xml asynchronously
utils.getXML(metadata_xml_url, function(metadata_xml) {
if (metadata_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory").length > 0) {
// we're on a tv show page
utils.debug("main detected we are on tv show index page");
if (metadata_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory")[0].getAttribute("type") === "show") {
// we're on the root show page
utils.debug("main detected we are on root show page");
// create trakt link
if (settings["trakt_shows"] === "on") {
utils.debug("trakt plugin is enabled");
trakt.init(metadata_xml, "show", server);
}
else {
utils.debug("trakt plugin is disabled");
}
// create tvdb link
if (settings["tvdb_link"] === "on") {
utils.debug("tvdb plugin is enabled");
tvdb.init(metadata_xml);
}
else {
utils.debug("tvdb plugin is disabled");
}
// insert missing seasons
if (settings["missing_episodes"] === "on") {
utils.debug("missing_episodes plugin is enabled");
missing_episodes.init(metadata_xml, server, "seasons");
}
else {
utils.debug("missing_episodes plugin is disabled");
}
}
else if (metadata_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Directory")[0].getAttribute("type") === "season") {
// we're on the season page
utils.debug("main detected we are on a season page");
// insert missing episodes
if (settings["missing_episodes"] === "on") {
utils.debug("missing_episodes plugin is enabled");
missing_episodes.init(metadata_xml, server, "episodes");
}
else {
utils.debug("missing_episodes plugin is disabled");
}
}
}
else if (metadata_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Video")[0].getAttribute("type") === "movie") {
// we're on a movie page
utils.debug("main detected we are on a movie page");
// insert canistreamit widget
if (settings["canistreamit"] === "on") {
utils.debug("canistreamit plugin is enabled");
canistreamit.init(metadata_xml);
}
else {
utils.debug("canistreamit plugin is disabled");
}
// create letterboxd link
if (settings["letterboxd_link"] === "on") {
utils.debug("letterboxd_link plugin is enabled");
letterboxd.init(metadata_xml);
}
else {
utils.debug("letterboxd_link plugin is disabled");
}
// insert themoviedb link
if (settings["themoviedb_link"] === "on") {
utils.debug("themoviedb plugin is enabled");
themoviedb.init(metadata_xml);
}
else {
utils.debug("themoviedb plugin is disabled");
}
// insert imdb link
if (settings["imdb_link"] === "on") {
utils.debug("imdb plugin is enabled");
imdb.init(metadata_xml);
}
else {
utils.debug("imdb plugin is disabled");
}
// create youtube trailer button
if (settings["movie_trailers"] === "on") {
utils.debug("youtube_trailer plugin is enabled");
youtube_trailer.init(metadata_xml);
}
else {
utils.debug("youtube_trailer plugin is disabled");
}
// create rotten tomatoes link
if (settings["rotten_tomatoes_link"] === "on") {
utils.debug("rotten_tomatoes_link plugin is enabled");
rotten_tomatoes.init(metadata_xml, settings["rotten_tomatoes_citizen"], settings["rotten_tomatoes_audience"]);
}
else {
utils.debug("rotten_tomatoes_link plugin is disabled");
}
// create trakt link
if (settings["trakt_movies"] === "on") {
utils.debug("trakt plugin is enabled");
trakt.init(metadata_xml, "movie", server);
}
else {
utils.debug("trakt plugin is disabled");
}
// create actors profiles
if (settings["actor_profiles"] === "on") {
utils.debug("actor_profiles plugin is enabled");
actor_profiles.init(metadata_xml);
}
else {
utils.debug("actor_profiles plugin is disabled");
}
}
else if (metadata_xml.getElementsByTagName("MediaContainer")[0].getElementsByTagName("Video")[0].getAttribute("type") === "episode") {
// we're on an episode page
// create trakt link
if (settings["trakt_shows"] === "on") {
utils.debug("trakt plugin is enabled");
trakt.init(metadata_xml, "episode", server);
}
else {
utils.debug("trakt plugin is disabled");
}
}
});
}
});
}
// set the default options for extension
utils.setDefaultOptions(function(stored_settings) {
settings = stored_settings;
// Plex/Web uses a lot of JS to manipulate the DOM so the only way to tell when
// plex's JS has finished is to check for the existance of certain elements.
runOnReady();
});
// because Plex/Web uses JS to change pages Chrome extensions don't run on every
// page load as expected. To fix this we run the script every time the window
// url hash changes.
window.onhashchange = function() {
utils.debug("Page change detected");
runOnReady();
}