-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
78 lines (70 loc) · 2.35 KB
/
Copy pathcontent.js
File metadata and controls
78 lines (70 loc) · 2.35 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
(() => {
const state = { enabled: true, speed: 2, lastClick: 0 };
const COOLDOWN_MS = 800;
const NEXT_SEL =
'#nav-controls button#next, section#bottom-bar button#next, button#next';
chrome.storage.sync.get({ enabled: true, speed: 2 }, (v) => {
state.enabled = v.enabled;
state.speed = Number(v.speed) || 1;
});
chrome.storage.onChanged.addListener((ch) => {
if (ch.enabled) state.enabled = ch.enabled.newValue;
if (ch.speed) state.speed = Number(ch.speed.newValue) || 1;
});
const visible = (el) => {
if (!el?.getBoundingClientRect) return false;
if (el.style.display === 'none') return false;
if (el.classList.contains('hidden')) return false;
const r = el.getBoundingClientRect();
return r.width > 0 && r.height > 0;
};
const clickable = (el) => {
if (!visible(el)) return false;
if (el.getAttribute('aria-disabled') === 'true' || el.disabled) return false;
if (el.classList.contains('disabled')) return false;
return true;
};
const isQuizSlide = () => {
// Only treat as quiz when Submit is actually shown (Storyline keeps it in DOM hidden).
for (const sel of ['#submit', '#nav-controls button#submit']) {
const el = document.querySelector(sel);
if (visible(el)) return true;
}
return !!document.querySelector('[data-model-id][class*="quiz"], .quiz-slide');
};
const clickNext = () => {
const btn = document.querySelector(NEXT_SEL);
if (!clickable(btn)) return;
const now = Date.now();
if (now - state.lastClick < COOLDOWN_MS) return;
state.lastClick = now;
try {
btn.focus();
btn.click();
} catch (_) {}
};
const setSpeed = (target) => {
const want = String(target);
for (const el of document.querySelectorAll(
'#playback-speed [role="menuitemcheckbox"][data-speed]'
)) {
if (el.getAttribute('data-speed') === want && el.getAttribute('aria-checked') !== 'true') {
el.click();
return;
}
}
};
const tick = () => {
if (!state.enabled) return;
if (state.speed !== 1) setSpeed(state.speed);
if (!isQuizSlide()) clickNext();
};
new MutationObserver(tick).observe(document.documentElement, {
subtree: true,
childList: true,
attributes: true,
attributeFilter: ['aria-disabled', 'style', 'class', 'disabled'],
});
setInterval(tick, 1000);
tick();
})();