mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 02:31:45 +00:00
use apiLoad event
This commit is contained in:
@ -3,26 +3,15 @@ const { ipcRenderer, remote } = require("electron");
|
|||||||
const { setOptions } = require("../../config/plugins");
|
const { setOptions } = require("../../config/plugins");
|
||||||
|
|
||||||
function $(selector) { return document.querySelector(selector); }
|
function $(selector) { return document.querySelector(selector); }
|
||||||
let api = $('#movie_player');
|
let api;
|
||||||
|
|
||||||
module.exports = (options) => {
|
module.exports = (options) => {
|
||||||
if (api) {
|
document.addEventListener('apiLoaded', e => {
|
||||||
|
api = e.detail;
|
||||||
firstRun(options);
|
firstRun(options);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const observer = new MutationObserver(() => {
|
|
||||||
api = $('#movie_player');
|
|
||||||
if (api) {
|
|
||||||
observer.disconnect();
|
|
||||||
firstRun(options);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
observer.observe(document.documentElement, { childList: true, subtree: true });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/** Restore saved volume and setup tooltip */
|
/** Restore saved volume and setup tooltip */
|
||||||
function firstRun(options) {
|
function firstRun(options) {
|
||||||
if (typeof options.savedVolume === "number") {
|
if (typeof options.savedVolume === "number") {
|
||||||
@ -93,10 +82,6 @@ function setupVideoPlayerOnwheel(options) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function toPercent(volume) {
|
|
||||||
return Math.round(Number.parseFloat(volume) * 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
function saveVolume(volume, options) {
|
function saveVolume(volume, options) {
|
||||||
options.savedVolume = volume;
|
options.savedVolume = volume;
|
||||||
writeOptions(options);
|
writeOptions(options);
|
||||||
|
|||||||
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 }));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user