-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
86 lines (68 loc) · 2.16 KB
/
Copy pathcontent.js
File metadata and controls
86 lines (68 loc) · 2.16 KB
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
(async () => {
const listType = await waitForListPage();
if (!listType) {
return;
}
// AniWorld may render the list asynchronously after the initial page load.
await waitForListContainer();
const entries = extractEntries();
await browser.runtime.sendMessage({ type: "save-list", listType, entries });
})();
async function waitForListPage() {
const deadline = Date.now() + 10_000;
let listType;
while (!(listType = detectListType()) && Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, 250));
}
return listType;
}
async function waitForListContainer() {
const deadline = Date.now() + 10_000;
while (!document.querySelector(".seriesListContainer") && Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, 250));
}
}
function detectListType() {
const path = window.location.pathname.toLocaleLowerCase("en-US");
if (path === "/account/watchlist" || /^\/account\/watchlist\/(asc|desc)$/.test(path)) {
return "watchlist";
}
if (path === "/account/subscribed") {
return "subscriptions";
}
return null;
}
function extractEntries() {
const seen = new Set();
const entries = [];
const list = document.querySelector(".seriesListContainer");
if (!list) {
return entries;
}
for (const link of list.querySelectorAll(':scope > div > a[href^="/anime/stream/"]')) {
const url = new URL(link.href, window.location.origin);
const match = url.pathname.match(/^\/anime\/stream\/([^/]+)/);
if (!match) {
continue;
}
const id = match[1];
if (seen.has(id)) {
continue;
}
const title = cleanText(link.querySelector("h1, h2, h3, h4")?.textContent || link.textContent);
if (!title) {
continue;
}
seen.add(id);
const image = link.querySelector("img")?.dataset.src || link.querySelector("img")?.src || null;
entries.push({
title,
url: url.origin + url.pathname,
image: image ? new URL(image, window.location.origin).href : null
});
}
return entries.sort((left, right) => left.title.localeCompare(right.title, "en-US"));
}
function cleanText(value) {
return value.replace(/\s+/g, " ").trim();
}