-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent_script.js
More file actions
48 lines (42 loc) · 1.9 KB
/
Copy pathcontent_script.js
File metadata and controls
48 lines (42 loc) · 1.9 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
browser.runtime.onMessage.addListener((message) => {
if (message.id === "msd-start") {
const firstImage = document.querySelector("img[src*=score_]");
const scoreContainer = firstImage?.parentElement?.parentElement;
const pageCount = scoreContainer?.querySelectorAll("." + firstImage?.parentElement?.classList?.[0])?.length;
const urls = [];
if (!firstImage || !scoreContainer) {
return browser.runtime.sendMessage({ // Send to browserAction
id: "msd-log",
content: "Something went wrong. Wasn't able to find score.\nMake sure the page has fully loaded and is maximized."
});
}
else if (isNaN(pageCount) || pageCount === 0) {
pageCount = parseInt(prompt("Page count not found, please set manually:"), 10);
}
(function loop(i=0) {
const MAX_PAGE_COUNT = 200;
if (i >= MAX_PAGE_COUNT) {
return browser.runtime.sendMessage({ // Send to browserAction
id: "msd-log",
content: `Max page count (${MAX_PAGE_COUNT}) reached.`
});
}
scoreContainer.scrollTo(0, i * (scoreContainer.scrollHeight / pageCount));
setTimeout(() => {
const loadedImageURLs = [...scoreContainer.querySelectorAll("img")].map((e) => e.src);
const imageURL = loadedImageURLs.find((e) => e.includes("score_" + i));
if (imageURL) urls.push(imageURL);
else return loop(i);
if (i + 1 < pageCount) return loop(i + 1);
else return finish();
}, 100);
})();
function finish() {
scoreContainer.scrollTo(0, 0);
browser.runtime.sendMessage({ // Send to background_script
id: "msd-create",
urls: urls
});
}
}
});