mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 18:41:47 +00:00
refactor(plugin): refactor plugin loader and add dynamic loading
This commit is contained in:
@ -1,33 +1,42 @@
|
||||
import fs from 'node:fs';
|
||||
|
||||
const cssToInject = new Map<string, (() => void) | undefined>();
|
||||
const cssToInjectFile = new Map<string, (() => void) | undefined>();
|
||||
export const injectCSS = (webContents: Electron.WebContents, css: string, cb: (() => void) | undefined = undefined) => {
|
||||
if (cssToInject.size === 0 && cssToInjectFile.size === 0) {
|
||||
setupCssInjection(webContents);
|
||||
}
|
||||
type Unregister = () => void;
|
||||
|
||||
cssToInject.set(css, cb);
|
||||
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> => {
|
||||
return new Promise((resolve) => {
|
||||
if (cssToInject.size === 0 && cssToInjectFile.size === 0) {
|
||||
setupCssInjection(webContents);
|
||||
}
|
||||
cssToInject.set(css, resolve);
|
||||
});
|
||||
};
|
||||
|
||||
export const injectCSSAsFile = (webContents: Electron.WebContents, filepath: string, cb: (() => void) | undefined = undefined) => {
|
||||
if (cssToInject.size === 0 && cssToInjectFile.size === 0) {
|
||||
setupCssInjection(webContents);
|
||||
}
|
||||
export const injectCSSAsFile = (webContents: Electron.WebContents, filepath: string): Promise<Unregister> => {
|
||||
return new Promise((resolve) => {
|
||||
if (cssToInject.size === 0 && cssToInjectFile.size === 0) {
|
||||
setupCssInjection(webContents);
|
||||
}
|
||||
|
||||
cssToInjectFile.set(filepath, cb);
|
||||
cssToInjectFile.set(filepath, resolve);
|
||||
});
|
||||
};
|
||||
|
||||
const setupCssInjection = (webContents: Electron.WebContents) => {
|
||||
webContents.on('did-finish-load', () => {
|
||||
cssToInject.forEach(async (callback, css) => {
|
||||
await webContents.insertCSS(css);
|
||||
callback?.();
|
||||
const key = await webContents.insertCSS(css);
|
||||
const remove = async () => await webContents.removeInsertedCSS(key);
|
||||
|
||||
callback?.(remove);
|
||||
});
|
||||
|
||||
cssToInjectFile.forEach(async (callback, filepath) => {
|
||||
await webContents.insertCSS(fs.readFileSync(filepath, 'utf-8'));
|
||||
callback?.();
|
||||
const key = await webContents.insertCSS(fs.readFileSync(filepath, 'utf-8'));
|
||||
const remove = async () => await webContents.removeInsertedCSS(key);
|
||||
|
||||
callback?.(remove);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user