Fix SPA video element recycling resilience

This commit is contained in:
Timo
2026-04-22 10:55:54 +02:00
parent 15afcccd0a
commit 4f2fe3045c
+21 -6
View File
@@ -162,13 +162,24 @@
});
}
const handlePlay = () => reportEvent('play');
const handlePause = () => reportEvent('pause');
const handleSeeked = () => reportEvent('seek');
let lastVideoSrc = null;
function setupListeners() {
const video = findVideo();
if (video && !video.dataset.koalaAttached) {
video.addEventListener('play', () => reportEvent('play'));
video.addEventListener('pause', () => reportEvent('pause'));
video.addEventListener('seeked', () => reportEvent('seek'));
if (video) {
video.removeEventListener('play', handlePlay);
video.removeEventListener('pause', handlePause);
video.removeEventListener('seeked', handleSeeked);
video.addEventListener('play', handlePlay);
video.addEventListener('pause', handlePause);
video.addEventListener('seeked', handleSeeked);
video.dataset.koalaAttached = 'true';
lastVideoSrc = video.currentSrc || video.src;
}
}
@@ -179,8 +190,12 @@
function checkVideo() {
lastMutate = Date.now();
const video = findVideo();
if (video && !video.dataset.koalaAttached) {
console.log('KoalaSync: New video detected via navigation.');
if (!video) return;
const currentSrc = video.currentSrc || video.src;
if (!video.dataset.koalaAttached || (lastVideoSrc && currentSrc && lastVideoSrc !== currentSrc)) {
console.log('KoalaSync: Video element attached or recycled.');
setupListeners();
}
}