-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll.js
48 lines (42 loc) · 1.31 KB
/
scroll.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
function waitUntil(selector) {
return new Promise((resolve) => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver((mutations) => {
if (document.querySelector(selector)) {
observer.disconnect();
resolve(document.querySelector(selector));
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
});
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "executeScript") {
waitUntil(".YtPlayerProgressBarDragContainer").then((scrubber) => {
console.log(`ShortScroller: auto scroll for ${message.url}`);
let observer;
document.addEventListener("keydown", (event) => {
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
observer.disconnect();
}
});
const s = new Set();
observer = new MutationObserver(function () {
const step = scrubber.getAttribute("aria-valuenow");
if (s.has(step)) {
document.querySelector("#navigation-button-down button").click();
observer.disconnect();
}
s.add(step);
});
observer.observe(scrubber, {
attributes: true,
});
});
}
});