feat(plugin): support dynamic plugin load / unload

This commit is contained in:
Su-Yong
2023-11-12 01:16:34 +09:00
parent 9c59f56aac
commit 2097f42efb
8 changed files with 87 additions and 15 deletions

View File

@ -11,12 +11,12 @@ export default builder.createRenderer(async ({ getConfig }) => {
let opacity = initConfigData.opacity;
let isFullscreen = initConfigData.fullscreen;
let unregister: (() => void) | null = null;
let update: (() => void) | null = null;
let observer: MutationObserver;
return {
onLoad() {
let unregister: (() => void) | null = null;
const injectBlurVideo = (): (() => void) | null => {
const songVideo = document.querySelector<HTMLDivElement>('#song-video');
const video = document.querySelector<HTMLVideoElement>('#song-video .html5-video-container > video');
@ -84,7 +84,6 @@ export default builder.createRenderer(async ({ getConfig }) => {
blurCanvas.style.setProperty('--top', `${-1 * topOffset}px`);
blurCanvas.style.setProperty('--blur', `${blur}px`);
blurCanvas.style.setProperty('--opacity', `${opacity}`);
console.log('updated!!!');
};
update = applyVideoAttributes;
@ -140,6 +139,12 @@ export default builder.createRenderer(async ({ getConfig }) => {
const playerPage = document.querySelector<HTMLElement>('#player-page');
const ytmusicAppLayout = document.querySelector<HTMLElement>('#layout');
const isPageOpen = ytmusicAppLayout?.hasAttribute('player-page-open');
if (isPageOpen) {
unregister?.();
unregister = injectBlurVideo() ?? null;
}
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'attributes') {
@ -170,5 +175,10 @@ export default builder.createRenderer(async ({ getConfig }) => {
update?.();
},
onUnload() {
observer?.disconnect();
update = null;
unregister?.();
}
};
});

View File

@ -2,9 +2,16 @@ import fs from 'node:fs';
type Unregister = () => void;
let isLoaded = false;
const cssToInject = new Map<string, ((unregister: Unregister) => void) | undefined>();
const cssToInjectFile = new Map<string, ((unregister: Unregister) => void) | undefined>();
export const injectCSS = (webContents: Electron.WebContents, css: string): Promise<Unregister> => {
export const injectCSS = async (webContents: Electron.WebContents, css: string): Promise<Unregister> => {
if (isLoaded) {
const key = await webContents.insertCSS(css);
return async () => await webContents.removeInsertedCSS(key);
}
return new Promise((resolve) => {
if (cssToInject.size === 0 && cssToInjectFile.size === 0) {
setupCssInjection(webContents);
@ -13,7 +20,12 @@ export const injectCSS = (webContents: Electron.WebContents, css: string): Promi
});
};
export const injectCSSAsFile = (webContents: Electron.WebContents, filepath: string): Promise<Unregister> => {
export const injectCSSAsFile = async (webContents: Electron.WebContents, filepath: string): Promise<Unregister> => {
if (isLoaded) {
const key = await webContents.insertCSS(fs.readFileSync(filepath, 'utf-8'));
return async () => await webContents.removeInsertedCSS(key);
}
return new Promise((resolve) => {
if (cssToInject.size === 0 && cssToInjectFile.size === 0) {
setupCssInjection(webContents);
@ -25,6 +37,8 @@ export const injectCSSAsFile = (webContents: Electron.WebContents, filepath: str
const setupCssInjection = (webContents: Electron.WebContents) => {
webContents.on('did-finish-load', () => {
isLoaded = true;
cssToInject.forEach(async (callback, css) => {
const key = await webContents.insertCSS(css);
const remove = async () => await webContents.removeInsertedCSS(key);