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

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