use apiLoad event

This commit is contained in:
Araxeus
2021-10-23 18:27:35 +03:00
parent bb2e1bd616
commit df75e480a6
2 changed files with 30 additions and 18 deletions

View File

@ -10,6 +10,8 @@ const setupSongInfo = require("./providers/song-info-front");
const plugins = config.plugins.getEnabled();
let api;
plugins.forEach(([plugin, options]) => {
const preloadPath = path.join(__dirname, "plugins", plugin, "preload.js");
fileExists(preloadPath, () => {
@ -38,6 +40,9 @@ document.addEventListener("DOMContentLoaded", () => {
});
});
// wait for complete load of youtube api
listenForApiLoad();
// inject song-info provider
setupSongInfo();
@ -51,3 +56,25 @@ document.addEventListener("DOMContentLoaded", () => {
global.reload = () =>
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 }));
}