mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 11:01:45 +00:00
feat: reimplement inject css, fix types
This commit is contained in:
@ -1,11 +1,9 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
import { BrowserWindow, ipcMain } from 'electron';
|
||||
|
||||
import { deepmerge } from 'deepmerge-ts';
|
||||
import { mainPlugins } from 'virtual:plugins';
|
||||
|
||||
import { PluginDef } from '@/types/plugins';
|
||||
import { PluginConfig, PluginDef } from '@/types/plugins';
|
||||
import { BackendContext } from '@/types/contexts';
|
||||
import config from '@/config';
|
||||
import { startPlugin, stopPlugin } from '@/utils';
|
||||
@ -14,35 +12,37 @@ const loadedPluginMap: Record<string, PluginDef> = {};
|
||||
|
||||
const createContext = (id: string, win: BrowserWindow): BackendContext => ({
|
||||
getConfig: () =>
|
||||
// @ts-expect-error ts dum dum
|
||||
deepmerge(
|
||||
mainPlugins[id].config,
|
||||
config.get(`plugins.${id}`) ?? { enabled: false },
|
||||
),
|
||||
) as PluginConfig,
|
||||
setConfig: (newConfig) => {
|
||||
config.setPartial(`plugins.${id}`, newConfig);
|
||||
},
|
||||
|
||||
send: (event: string, ...args: unknown[]) => {
|
||||
win.webContents.send(event, ...args);
|
||||
},
|
||||
// @ts-expect-error ts dum dum
|
||||
handle: (event: string, listener) => {
|
||||
// @ts-expect-error ts dum dum
|
||||
ipcMain.handle(event, (_, ...args) => listener(...(args as never)));
|
||||
},
|
||||
// @ts-expect-error ts dum dum
|
||||
on: (event: string, listener) => {
|
||||
// @ts-expect-error ts dum dum
|
||||
ipcMain.on(event, (_, ...args) => listener(...(args as never)));
|
||||
ipc: {
|
||||
send: (event: string, ...args: unknown[]) => {
|
||||
win.webContents.send(event, ...args);
|
||||
},
|
||||
handle: (event: string, listener: CallableFunction) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
ipcMain.handle(event, (_, ...args: unknown[]) => listener(...args));
|
||||
},
|
||||
on: (event: string, listener: CallableFunction) => {
|
||||
ipcMain.on(event, (_, ...args: unknown[]) => {
|
||||
listener(...args);
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
window: win,
|
||||
});
|
||||
|
||||
export const forceUnloadMainPlugin = async (
|
||||
id: string,
|
||||
win: BrowserWindow,
|
||||
): Promise<void> => {
|
||||
const plugin = loadedPluginMap[id]!;
|
||||
const plugin = loadedPluginMap[id];
|
||||
if (!plugin) return;
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
|
||||
@ -15,6 +15,17 @@ const createContext = (id: string): RendererContext => ({
|
||||
setConfig: async (newConfig) => {
|
||||
await window.ipcRenderer.invoke('set-config', id, newConfig);
|
||||
},
|
||||
ipc: {
|
||||
send: (event: string, ...args: unknown[]) => {
|
||||
window.ipcRenderer.send(event, ...args);
|
||||
},
|
||||
invoke: (event: string, ...args: unknown[]) => window.ipcRenderer.invoke(event, ...args),
|
||||
on: (event: string, listener: CallableFunction) => {
|
||||
window.ipcRenderer.on(event, (_, ...args: unknown[]) => {
|
||||
listener(...args);
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const forceUnloadRendererPlugin = (id: string) => {
|
||||
@ -27,7 +38,7 @@ export const forceUnloadRendererPlugin = (id: string) => {
|
||||
if (!plugin) return;
|
||||
|
||||
stopPlugin(id, plugin, { ctx: 'renderer', context: createContext(id) });
|
||||
if (plugin.renderer?.stylesheet)
|
||||
if (plugin?.stylesheets)
|
||||
document.querySelector(`style#plugin-${id}`)?.remove();
|
||||
|
||||
console.log('[YTMusic]', `"${id}" plugin is unloaded`);
|
||||
@ -42,14 +53,14 @@ export const forceLoadRendererPlugin = (id: string) => {
|
||||
context: createContext(id),
|
||||
});
|
||||
|
||||
if (hasEvaled || plugin.renderer?.stylesheet) {
|
||||
if (hasEvaled || plugin?.stylesheets) {
|
||||
loadedPluginMap[id] = plugin;
|
||||
|
||||
if (plugin.renderer?.stylesheet)
|
||||
if (plugin?.stylesheets)
|
||||
document.head.appendChild(
|
||||
Object.assign(document.createElement('style'), {
|
||||
id: `plugin-${id}`,
|
||||
innerHTML: plugin.renderer?.stylesheet ?? '',
|
||||
innerHTML: plugin?.stylesheets ?? '',
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user