mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 10:31:47 +00:00
use $('video') srcChanged event instead of loadeddata/metadata
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
document.addEventListener('apiLoaded', () => {
|
document.addEventListener('apiLoaded', () => {
|
||||||
document.querySelector('video').addEventListener('loadeddata', e => {
|
document.querySelector('video').addEventListener('srcChanged', e => {
|
||||||
e.target.pause();
|
e.target.pause();
|
||||||
})
|
})
|
||||||
}, { once: true, passive: true })
|
}, { once: true, passive: true })
|
||||||
|
|||||||
@ -49,7 +49,7 @@ const observePopupContainer = () => {
|
|||||||
|
|
||||||
const observeVideo = () => {
|
const observeVideo = () => {
|
||||||
$('video').addEventListener('ratechange', forcePlaybackRate)
|
$('video').addEventListener('ratechange', forcePlaybackRate)
|
||||||
$('video').addEventListener('loadeddata', forcePlaybackRate)
|
$('video').addEventListener('srcChanged', forcePlaybackRate)
|
||||||
}
|
}
|
||||||
|
|
||||||
const setupWheelListener = () => {
|
const setupWheelListener = () => {
|
||||||
|
|||||||
@ -35,13 +35,13 @@ function setup() {
|
|||||||
setOptions("video-toggle", options);
|
setOptions("video-toggle", options);
|
||||||
})
|
})
|
||||||
|
|
||||||
$('video').addEventListener('loadedmetadata', videoStarted);
|
$('video').addEventListener('srcChanged', videoStarted);
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeDisplay(showVideo) {
|
function changeDisplay(showVideo) {
|
||||||
if (!showVideo && $('ytmusic-player').getAttribute('playback-mode') !== "ATV_PREFERRED") {
|
if (!showVideo) {
|
||||||
$('video').style.top = "0";
|
$('video').style.top = "0";
|
||||||
$('ytmusic-player').style.margin = "auto 21.5px";
|
$('ytmusic-player').style.margin = "auto 0px";
|
||||||
$('ytmusic-player').setAttribute('playback-mode', "ATV_PREFERRED");
|
$('ytmusic-player').setAttribute('playback-mode', "ATV_PREFERRED");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,10 +10,22 @@ ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
module.exports = () => {
|
module.exports = () => {
|
||||||
document.addEventListener('apiLoaded', e => {
|
document.addEventListener('apiLoaded', e => observeSrcChange(e.detail), { once: true, passive: true });
|
||||||
document.querySelector('video').addEventListener('loadedmetadata', () => {
|
|
||||||
const data = e.detail.getPlayerResponse();
|
|
||||||
ipcRenderer.send("song-info-request", JSON.stringify(data));
|
|
||||||
});
|
|
||||||
}, { 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