mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-14 11:51:47 +00:00
use $('video') srcChanged event instead of loadeddata/metadata
This commit is contained in:
@ -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"] })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user