-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
content_script.js
486 lines (397 loc) · 14.4 KB
/
content_script.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
// ==UserScript==
// @name Debrid Media Manager
// @namespace https://debridmediamanager.com
// @version 1.6.2
// @description Add accessible DMM buttons to IMDB, MDBList, AniDB, TraktTV, and Bittorrent sites with magnet links
// @author Ben Adrian Sarmiento <[email protected]>
// @license MIT
// @match *://*/*
// @grant none
// ==/UserScript==
(function () {
"use strict";
const DMM_HOST = "https://debridmediamanager.com";
const X_DMM_HOST = "https://x.debridmediamanager.com";
const SEARCH_BTN_LABEL = "DMM🔎";
function createButton(text, url) {
const button = document.createElement("button");
// button.tabIndex = 0;
// button.disabled = false;
button.textContent = text;
button.style.fontFamily = "'Roboto', sans-serif";
button.style.marginLeft = "5px";
button.style.padding = "3px 5px";
button.style.border = "none";
button.style.borderRadius = "3px";
button.style.background = "#00A0B0"; // Teal color
button.style.color = "#ECF0F1"; // Off-white color
button.style.cursor = "pointer";
button.style.transition = "background-color 0.3s, transform 0.1s";
// Hover effect
button.onmouseover = function () {
this.style.background = "#EDC951"; // Yellow color
};
// Revert hover effect
button.onmouseout = function () {
this.style.background = "#00A0B0"; // Teal color
};
// Click effect
button.onmousedown = function () {
this.style.transform = "scale(0.95)";
this.style.background = "#CC333F"; // Dark red color
};
// Revert click effect
button.onmouseup = function () {
this.style.transform = "scale(1)";
this.style.background = "#EDC951"; // Yellow color (same as hover effect)
};
// Open new tab on click
button.onclick = (event) => {
event.preventDefault();
window.open(url, "_blank");
};
return button;
}
function createLink(text, url) {
const link = document.createElement("a");
link.textContent = text;
link.href = url;
link.target = "_blank"; // Opens the link in a new tab
link.style.display = "inline-block"; // Display as inline-block to style like a button
link.style.fontFamily = "'Roboto', sans-serif";
link.style.marginLeft = "5px";
link.style.padding = "3px 5px";
link.style.border = "none";
link.style.borderRadius = "3px";
link.style.background = "#00A0B0"; // Teal color
link.style.color = "#ECF0F1"; // Off-white color
link.style.textDecoration = "none"; // Remove underline from the link
link.style.cursor = "pointer";
return link;
}
function addButtonToElement(element, text, url) {
const button = createButton(text, url);
element.appendChild(button);
}
function addLinkToElement(element, text, url) {
const button = createLink(text, url);
element.appendChild(button);
}
function getInfoHashFromMagnetLink(magnetLink) {
const urlParams = new URLSearchParams(magnetLink.substring(magnetLink.indexOf('?')));
const xt = urlParams.get('xt');
if (xt && xt.startsWith('urn:btih:')) {
return xt.substring(9); // Remove 'urn:btih:'
}
return null;
}
// IMDB functions
function addButtonsToIMDBSingleTitle() {
const targetElement = document.querySelector(
"section.ipc-page-background h1 > span"
);
if (targetElement && targetElement.hasAttribute("data-dmm-btn-added"))
return;
targetElement.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${X_DMM_HOST}/${window.location.pathname
.replaceAll("/", "")
.substring(5)}`;
addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
}
function addButtonsToIMDBList() {
const items = Array.from(
document.querySelectorAll(
".lister-item .lister-item-header, .lister-item .media"
)
).filter((item) => !item.hasAttribute("data-dmm-btn-added"));
items.forEach((item) => {
item.setAttribute("data-dmm-btn-added", "true");
let link = item.querySelector('a[href^="/title/"]').href;
let imdbId = link.match(/tt\d+/)?.[0];
if (!imdbId) return;
const searchUrl = `${X_DMM_HOST}/${imdbId}`;
addButtonToElement(item, SEARCH_BTN_LABEL, searchUrl);
});
changeObserver("ul.ipc-metadata-list", addButtonsToIMDBList);
}
function addButtonsToIMDBChart() {
const items = Array.from(document.querySelectorAll(".cli-title")).filter(
(item) =>
item.innerText.match(/\d+\./) &&
!item.hasAttribute("data-dmm-btn-added")
);
items.forEach((item) => {
item.setAttribute("data-dmm-btn-added", "true");
let link = item.querySelector('a[href^="/title/"]').href;
let imdbId = link.match(/tt\d+/)?.[0];
if (!imdbId) return;
const searchUrl = `${X_DMM_HOST}/${imdbId}`;
addButtonToElement(item, SEARCH_BTN_LABEL, searchUrl);
});
changeObserver("ul.ipc-metadata-list", addButtonsToIMDBChart);
}
// MDBList functions
function addButtonsToMDBListSingleTitle() {
const targetElement = document.querySelector(
"#content-desktop-2 > div > div:nth-child(1) > h3"
);
if (targetElement && targetElement.hasAttribute("data-dmm-btn-added"))
return;
targetElement.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${DMM_HOST}${window.location.pathname}`;
addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
}
function addButtonsToMDBListSearchResults() {
const items = Array.from(
document.querySelectorAll("div.ui.centered.cards > div")
).filter((item) => !item.hasAttribute("data-dmm-btn-added"));
items.forEach((item) => {
item.setAttribute("data-dmm-btn-added", "true");
const targetElement = item.querySelector("div.header");
if (targetElement) {
const url = targetElement.parentElement
.querySelector("a")
.href.replace("https://mdblist.com/", "");
const searchUrl = `${DMM_HOST}/${url}`;
addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
}
});
changeObserver("div.ui.centered.cards", addButtonsToMDBListSearchResults);
}
// AniDB functions
function addButtonsToAniDBSingleTitle() {
const targetElement = document.querySelector("#layout-main > h1.anime");
if (targetElement && targetElement.hasAttribute("data-dmm-btn-added"))
return;
targetElement.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${DMM_HOST}/${window.location.pathname
.replaceAll("/", "")
.replace("anime", "anime/anidb-")}`;
addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
}
function addButtonsToAniDBAnyPage() {
const items = Array.from(document.querySelectorAll("a")).filter(
(item) => item.innerText.trim() && /\/anime\/\d+$/.test(item.href) && !item.hasAttribute("data-dmm-btn-added")
);
items.forEach((item) => {
item.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${DMM_HOST}/${item.href
.replace("https://anidb.net/", "")
.replaceAll("/", "")
.replace("anime", "anime/anidb-")}`;
addButtonToElement(item, SEARCH_BTN_LABEL, searchUrl);
});
}
// TraktTV functions
function addButtonsToTraktTVSingleTitle() {
const targetElement = document.querySelector("#summary-wrapper div > h1");
if (targetElement && targetElement.hasAttribute("data-dmm-btn-added"))
return;
// find imdb id in page, <a data-type="imdb">
const imdbId = document
.querySelector("a#external-link-imdb")
?.href?.match(/tt\d+/)?.[0];
if (!imdbId) return;
targetElement.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${X_DMM_HOST}/${imdbId}`;
addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
}
// iCheckMovies functions
function addButtonsToiCheckMoviesSingleTitle() {
const imdbId = document
.querySelector("a.optionIMDB")
?.href?.match(/tt\d+/)?.[0];
if (!imdbId) return;
const targetElement = document.querySelector("#movie > h1");
if (targetElement && targetElement.hasAttribute("data-dmm-btn-added"))
return;
targetElement.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${X_DMM_HOST}/${imdbId}`;
addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
}
function addButtonsToiCheckMoviesBetaSingleTitle() {
const imdbId = document
.querySelector("a.stat-imdb")
?.href?.match(/tt\d+/)?.[0];
if (!imdbId) return;
const targetElement = document.querySelector("h1.title");
if (targetElement && targetElement.hasAttribute("data-dmm-btn-added"))
return;
targetElement.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${X_DMM_HOST}/${imdbId}`;
addLinkToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
}
function addButtonsToiCheckMoviesList() {
const items = Array.from(
document.querySelectorAll("ol#itemListMovies > li")
).filter((item) => !item.hasAttribute("data-dmm-btn-added"));
items.forEach((item) => {
const imdbId = item
.querySelector("a.optionIMDB")
?.href?.match(/tt\d+/)?.[0];
if (!imdbId) return;
const targetElement = item.querySelector("h2 a");
if (!targetElement) return;
item.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${X_DMM_HOST}/${imdbId}`;
addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
});
}
function addButtonsToiCheckMoviesBetaList() {
const items = Array.from(
document.querySelectorAll("div.media-content")
).filter((item) => !item.hasAttribute("data-dmm-btn-added"));
items.forEach((item) => {
const imdbId = item
.querySelector("a.stat-imdb")
?.href?.match(/tt\d+/)?.[0];
if (!imdbId) return;
const targetElement = item.querySelector("h3.title");
if (!targetElement) return;
item.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${X_DMM_HOST}/${imdbId}`;
addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
});
changeObserver(
"#app section.section div.columns",
addButtonsToiCheckMoviesBetaList
);
}
// letterboxd functions
function addButtonsToLetterboxdSingleTitle() {
const imdbId = document
.querySelector("a[data-track-action='IMDb']")
?.href?.match(/tt\d+/)?.[0];
if (!imdbId) return;
const targetElement = document.querySelector("h1.filmtitle");
if (targetElement && targetElement.hasAttribute("data-dmm-btn-added"))
return;
targetElement.setAttribute("data-dmm-btn-added", "true");
const searchUrl = `${X_DMM_HOST}/${imdbId}`;
addButtonToElement(targetElement, SEARCH_BTN_LABEL, searchUrl);
}
// observer utility function
function changeObserver(cssSelector, addBtnFn) {
const targetNode = document.querySelector(cssSelector);
if (!targetNode) return;
const config = { childList: true, subtree: true };
let debounceTimer;
const callback = function (mutationsList, observer) {
if (debounceTimer) {
clearTimeout(debounceTimer);
}
debounceTimer = setTimeout(() => {
// if (!targetNode) return;
observer.disconnect();
addBtnFn();
observer.observe(targetNode, config);
}, 250);
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
}
function addMagnetLinkButtonToElements(elements) {
elements.forEach(function(link) {
const magnetURL = link.href;
const infoHash = getInfoHashFromMagnetLink(magnetURL);
if (infoHash) {
const buttonURL = `https://debridmediamanager.com/library?addMagnet=${infoHash}`;
const button = createButton("DMM🧲", buttonURL);
link.parentNode.insertBefore(button, link.nextSibling);
}
});
}
// Main function
const magnetLinks = document.querySelectorAll('a[href^="magnet:?"]');
addMagnetLinkButtonToElements(magnetLinks);
const hostname = window.location.hostname;
///// IMDB /////
if (hostname === "www.imdb.com") {
const isIMDBSingleTitlePage = /^\/title\//.test(location.pathname);
const isIMDBListPage =
/^\/search\//.test(location.pathname) ||
/^\/list\/ls/.test(location.pathname);
const isIMDBChartPage = /^\/chart\//.test(location.pathname);
if (isIMDBSingleTitlePage) {
addButtonsToIMDBSingleTitle();
} else if (isIMDBListPage) {
addButtonsToIMDBList();
} else if (isIMDBChartPage) {
addButtonsToIMDBChart();
}
///// IMDB MOBILE /////
} else if (hostname === "m.imdb.com") {
const isIMDBSingleTitlePage = /^\/title\//.test(location.pathname);
const isIMDBListPage =
/^\/search\//.test(location.pathname) ||
/^\/list\/ls/.test(location.pathname);
const isIMDBChartPage = /^\/chart\//.test(location.pathname);
if (isIMDBSingleTitlePage) {
addButtonsToIMDBSingleTitle();
} else if (isIMDBListPage) {
addButtonsToIMDBList();
} else if (isIMDBChartPage) {
addButtonsToIMDBChart();
}
///// MDBLIST /////
} else if (hostname === "mdblist.com") {
const isMDBListSingleTitlePage = /^\/(movie|show)\//.test(
location.pathname
);
if (isMDBListSingleTitlePage) {
addButtonsToMDBListSingleTitle();
} else {
addButtonsToMDBListSearchResults();
}
///// ANIDB /////
} else if (hostname === "anidb.net") {
const isAniDBSingleTitlePage = /^\/anime\/\d+/.test(location.pathname);
if (isAniDBSingleTitlePage) {
addButtonsToAniDBSingleTitle();
}
addButtonsToAniDBAnyPage();
///// TRAKT TV /////
} else if (hostname === "trakt.tv") {
const isTraktTVEpisodePage = /\/episodes\/\d/.test(location.pathname);
if (isTraktTVEpisodePage) return;
const isTraktTVSinglePage = /^\/(shows|movies)\/.+/.test(location.pathname);
if (isTraktTVSinglePage) {
addButtonsToTraktTVSingleTitle();
}
///// ICHECKMOVIES /////
} else if (hostname === "www.icheckmovies.com") {
const isiCheckMoviesListPage = /^\/lists\//.test(location.pathname);
if (isiCheckMoviesListPage) {
addButtonsToiCheckMoviesList();
}
const isiCheckMoviesSingleTitlePage = /^\/movies\//.test(location.pathname);
if (isiCheckMoviesSingleTitlePage) {
addButtonsToiCheckMoviesSingleTitle();
}
} else if (hostname === "beta.icheckmovies.com") {
const isiCheckMoviesListPage = /^\/lists\//.test(location.pathname);
if (isiCheckMoviesListPage) {
addButtonsToiCheckMoviesBetaList();
}
const isiCheckMoviesSingleTitlePage = /^\/movies\//.test(location.pathname);
if (isiCheckMoviesSingleTitlePage) {
addButtonsToiCheckMoviesBetaSingleTitle();
}
///// MYANIMELIST /////
} else if (hostname === "myanimelist.net") {
///// KITSU /////
} else if (hostname === "kitsu.io") {
///// LETTERBOXD /////
} else if (hostname === "letterboxd.com") {
const isLetterboxdSingleTitlePage = /^\/film\//.test(location.pathname);
if (isLetterboxdSingleTitlePage) {
addButtonsToLetterboxdSingleTitle();
}
///// JUSTWATCH /////
} else if (hostname === "www.justwatch.com") {
///// TVDB /////
} else if (hostname === "www.thetvdb.com") {
///// TMDB /////
} else if (hostname === "www.themoviedb.org") {
}
})();