Refactor videoElement getter into a provider with callback

This commit is contained in:
TC
2021-06-03 21:45:28 +02:00
parent b8c6ebfa53
commit 30e94d1d6f
2 changed files with 30 additions and 23 deletions

View File

@ -1,25 +1,10 @@
let videoElement = null; const { ontimeupdate } = require("../../providers/video-element");
const observer = new MutationObserver((mutations, observer) => { module.exports = () => {
if (!videoElement) { ontimeupdate((videoElement) => {
videoElement = document.querySelector("video"); if (videoElement.currentTime === 0 && videoElement.duration !== NaN) {
} // auto-confirm-when-paused plugin can interfere here if not disabled!
videoElement.pause();
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 = observeVideoElement;

View 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);
}
};