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:
27
preload.js
27
preload.js
@ -10,6 +10,8 @@ const setupSongInfo = require("./providers/song-info-front");
|
|||||||
|
|
||||||
const plugins = config.plugins.getEnabled();
|
const plugins = config.plugins.getEnabled();
|
||||||
|
|
||||||
|
let api;
|
||||||
|
|
||||||
plugins.forEach(([plugin, options]) => {
|
plugins.forEach(([plugin, options]) => {
|
||||||
const preloadPath = path.join(__dirname, "plugins", plugin, "preload.js");
|
const preloadPath = path.join(__dirname, "plugins", plugin, "preload.js");
|
||||||
fileExists(preloadPath, () => {
|
fileExists(preloadPath, () => {
|
||||||
@ -38,6 +40,9 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// wait for complete load of youtube api
|
||||||
|
listenForApiLoad();
|
||||||
|
|
||||||
// inject song-info provider
|
// inject song-info provider
|
||||||
setupSongInfo();
|
setupSongInfo();
|
||||||
|
|
||||||
@ -51,3 +56,25 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
global.reload = () =>
|
global.reload = () =>
|
||||||
remote.getCurrentWindow().webContents.loadURL(config.get("url"));
|
remote.getCurrentWindow().webContents.loadURL(config.get("url"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function listenForApiLoad() {
|
||||||
|
api = document.querySelector('#movie_player');
|
||||||
|
if (api) {
|
||||||
|
onApiLoaded();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const observer = new MutationObserver(() => {
|
||||||
|
api = document.querySelector('#movie_player');
|
||||||
|
if (api) {
|
||||||
|
observer.disconnect();
|
||||||
|
onApiLoaded();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
observer.observe(document.documentElement, { childList: true, subtree: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
function onApiLoaded() {
|
||||||
|
document.dispatchEvent(new CustomEvent('apiLoaded', { detail: api }));
|
||||||
|
}
|
||||||
|
|||||||
@ -4,35 +4,16 @@ const { getImage } = require("./song-info");
|
|||||||
|
|
||||||
global.songInfo = {};
|
global.songInfo = {};
|
||||||
|
|
||||||
let api = document.querySelector('#movie_player');
|
|
||||||
|
|
||||||
ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
|
ipcRenderer.on("update-song-info", async (_, extractedSongInfo) => {
|
||||||
global.songInfo = JSON.parse(extractedSongInfo);
|
global.songInfo = JSON.parse(extractedSongInfo);
|
||||||
global.songInfo.image = await getImage(global.songInfo.imageSrc);
|
global.songInfo.image = await getImage(global.songInfo.imageSrc);
|
||||||
});
|
});
|
||||||
|
|
||||||
function setup() {
|
module.exports = () => {
|
||||||
if (api) {
|
document.addEventListener('apiLoaded', e => {
|
||||||
injectListener();
|
document.querySelector('video').addEventListener('loadeddata', () => {
|
||||||
return;
|
const data = e.detail.getPlayerResponse();
|
||||||
}
|
ipcRenderer.send("song-info-request", JSON.stringify(data));
|
||||||
|
});
|
||||||
const observer = new MutationObserver(() => {
|
|
||||||
api = document.querySelector('#movie_player');
|
|
||||||
if (api) {
|
|
||||||
observer.disconnect();
|
|
||||||
injectListener();
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
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