mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 10:31:47 +00:00
use apiLoad event
This commit is contained in:
@ -4,35 +4,16 @@ const { getImage } = require("./song-info");
|
||||
|
||||
global.songInfo = {};
|
||||
|
||||
let api = document.querySelector('#movie_player');
|
||||
|
||||
ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
|
||||
global.songInfo = JSON.parse(extractedSongInfo);
|
||||
global.songInfo.image = await getImage(global.songInfo.imageSrc);
|
||||
});
|
||||
|
||||
function setup() {
|
||||
if (api) {
|
||||
injectListener();
|
||||
return;
|
||||
}
|
||||
|
||||
const observer = new MutationObserver(() => {
|
||||
api = document.querySelector('#movie_player');
|
||||
if (api) {
|
||||
observer.disconnect();
|
||||
injectListener();
|
||||
}
|
||||
module.exports = () => {
|
||||
document.addEventListener('apiLoaded', e => {
|
||||
document.querySelector('video').addEventListener('loadeddata', () => {
|
||||
const data = e.detail.getPlayerResponse();
|
||||
ipcRenderer.send("song-info-request", JSON.stringify(data));
|
||||
});
|
||||
})
|
||||
|
||||
observer.observe(document.documentElement, { childList: true, subtree: true });
|
||||
}
|
||||
|
||||
function injectListener() {
|
||||
document.querySelector('video').addEventListener('loadeddata', () => {
|
||||
const data = api.getPlayerResponse();
|
||||
ipcRenderer.send("song-info-request", JSON.stringify(data));
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = setup;
|
||||
|
||||
Reference in New Issue
Block a user