use $('video') srcChanged event instead of loadeddata/metadata

This commit is contained in:
Araxeus
2021-11-09 13:29:41 +02:00
parent f40ed04899
commit 65ce62adc1
4 changed files with 23 additions and 11 deletions

View File

@ -10,10 +10,22 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
});
module.exports = () => {
document.addEventListener('apiLoaded', e => {
document.querySelector('video').addEventListener('loadedmetadata', () => {
const data = e.detail.getPlayerResponse();
ipcRenderer.send("song-info-request", JSON.stringify(data));
});
}, { once: true, passive: true })
document.addEventListener('apiLoaded', e => observeSrcChange(e.detail), { once: true, passive: true });
};
// used because 'loadeddata' or 'loadedmetadata' weren't firing on song start for some users (https://github.com/th-ch/youtube-music/issues/473)
function observeSrcChange(api) {
const srcChangedEvent = new CustomEvent('srcChanged');
const video = document.querySelector('video');
const playbackModeObserver = new MutationObserver((mutations) => {
mutations.forEach(mutation => {
if (mutation.target.src) { // in first mutation src is usually an empty string (loading)
video.dispatchEvent(srcChangedEvent);
ipcRenderer.send("song-info-request", JSON.stringify(api.getPlayerResponse()));
}
})
});
playbackModeObserver.observe(video, { attributeFilter: ["src"] })
}