Skip to content

Commit 30e94d1

Browse files
committed
Refactor videoElement getter into a provider with callback
1 parent b8c6ebf commit 30e94d1

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

plugins/disable-autoplay/front.js

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
1-
let videoElement = null;
2-
3-
const observer = new MutationObserver((mutations, observer) => {
4-
if (!videoElement) {
5-
videoElement = document.querySelector("video");
6-
}
7-
8-
if (videoElement) {
9-
videoElement.ontimeupdate = () => {
10-
if (videoElement.currentTime === 0 && videoElement.duration !== NaN) {
11-
// auto-confirm-when-paused plugin can interfere here if not disabled!
12-
videoElement.pause();
13-
}
14-
};
15-
}
16-
});
17-
18-
function observeVideoElement() {
19-
observer.observe(document, {
20-
childList: true,
21-
subtree: true,
1+
const { ontimeupdate } = require("../../providers/video-element");
2+
3+
module.exports = () => {
4+
ontimeupdate((videoElement) => {
5+
if (videoElement.currentTime === 0 && videoElement.duration !== NaN) {
6+
// auto-confirm-when-paused plugin can interfere here if not disabled!
7+
videoElement.pause();
8+
}
229
});
23-
}
24-
25-
module.exports = observeVideoElement;
10+
};

providers/video-element.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
let videoElement = null;
2+
3+
module.exports.ontimeupdate = (cb) => {
4+
const observer = new MutationObserver((mutations, observer) => {
5+
if (!videoElement) {
6+
videoElement = document.querySelector("video");
7+
if (videoElement) {
8+
observer.disconnect();
9+
videoElement.ontimeupdate = () => cb(videoElement);
10+
}
11+
}
12+
});
13+
14+
if (!videoElement) {
15+
observer.observe(document, {
16+
childList: true,
17+
subtree: true,
18+
});
19+
} else {
20+
videoElement.ontimeupdate = () => cb(videoElement);
21+
}
22+
};

0 commit comments

Comments
 (0)