mirror of
https://github.com/Shik3i/KoalaSync.git
synced 2026-08-31 13:08:15 +00:00
Fix SPA video element recycling resilience
This commit is contained in:
+21
-6
@@ -162,13 +162,24 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handlePlay = () => reportEvent('play');
|
||||||
|
const handlePause = () => reportEvent('pause');
|
||||||
|
const handleSeeked = () => reportEvent('seek');
|
||||||
|
|
||||||
|
let lastVideoSrc = null;
|
||||||
|
|
||||||
function setupListeners() {
|
function setupListeners() {
|
||||||
const video = findVideo();
|
const video = findVideo();
|
||||||
if (video && !video.dataset.koalaAttached) {
|
if (video) {
|
||||||
video.addEventListener('play', () => reportEvent('play'));
|
video.removeEventListener('play', handlePlay);
|
||||||
video.addEventListener('pause', () => reportEvent('pause'));
|
video.removeEventListener('pause', handlePause);
|
||||||
video.addEventListener('seeked', () => reportEvent('seek'));
|
video.removeEventListener('seeked', handleSeeked);
|
||||||
|
|
||||||
|
video.addEventListener('play', handlePlay);
|
||||||
|
video.addEventListener('pause', handlePause);
|
||||||
|
video.addEventListener('seeked', handleSeeked);
|
||||||
video.dataset.koalaAttached = 'true';
|
video.dataset.koalaAttached = 'true';
|
||||||
|
lastVideoSrc = video.currentSrc || video.src;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,8 +190,12 @@
|
|||||||
function checkVideo() {
|
function checkVideo() {
|
||||||
lastMutate = Date.now();
|
lastMutate = Date.now();
|
||||||
const video = findVideo();
|
const video = findVideo();
|
||||||
if (video && !video.dataset.koalaAttached) {
|
if (!video) return;
|
||||||
console.log('KoalaSync: New video detected via navigation.');
|
|
||||||
|
const currentSrc = video.currentSrc || video.src;
|
||||||
|
|
||||||
|
if (!video.dataset.koalaAttached || (lastVideoSrc && currentSrc && lastVideoSrc !== currentSrc)) {
|
||||||
|
console.log('KoalaSync: Video element attached or recycled.');
|
||||||
setupListeners();
|
setupListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user