mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 02:31:45 +00:00
Refactor videoElement getter into a provider with callback
This commit is contained in:
@ -1,25 +1,10 @@
|
||||
let videoElement = null;
|
||||
const { ontimeupdate } = require("../../providers/video-element");
|
||||
|
||||
const observer = new MutationObserver((mutations, observer) => {
|
||||
if (!videoElement) {
|
||||
videoElement = document.querySelector("video");
|
||||
}
|
||||
|
||||
if (videoElement) {
|
||||
videoElement.ontimeupdate = () => {
|
||||
if (videoElement.currentTime === 0 && videoElement.duration !== NaN) {
|
||||
// auto-confirm-when-paused plugin can interfere here if not disabled!
|
||||
videoElement.pause();
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
function observeVideoElement() {
|
||||
observer.observe(document, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
module.exports = () => {
|
||||
ontimeupdate((videoElement) => {
|
||||
if (videoElement.currentTime === 0 && videoElement.duration !== NaN) {
|
||||
// auto-confirm-when-paused plugin can interfere here if not disabled!
|
||||
videoElement.pause();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = observeVideoElement;
|
||||
};
|
||||
|
||||
22
providers/video-element.js
Normal file
22
providers/video-element.js
Normal file
@ -0,0 +1,22 @@
|
||||
let videoElement = null;
|
||||
|
||||
module.exports.ontimeupdate = (cb) => {
|
||||
const observer = new MutationObserver((mutations, observer) => {
|
||||
if (!videoElement) {
|
||||
videoElement = document.querySelector("video");
|
||||
if (videoElement) {
|
||||
observer.disconnect();
|
||||
videoElement.ontimeupdate = () => cb(videoElement);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!videoElement) {
|
||||
observer.observe(document, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
} else {
|
||||
videoElement.ontimeupdate = () => cb(videoElement);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user