mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 02:51:46 +00:00
feat: refactor plugin utils (#1391)
This commit is contained in:
33
src/plugins/utils/main/css.ts
Normal file
33
src/plugins/utils/main/css.ts
Normal file
@ -0,0 +1,33 @@
|
||||
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);
|
||||
}
|
||||
|
||||
cssToInject.set(css, cb);
|
||||
};
|
||||
|
||||
export const injectCSSAsFile = (webContents: Electron.WebContents, filepath: string, cb: (() => void) | undefined = undefined) => {
|
||||
if (cssToInject.size === 0 && cssToInjectFile.size === 0) {
|
||||
setupCssInjection(webContents);
|
||||
}
|
||||
|
||||
cssToInjectFile.set(filepath, cb);
|
||||
};
|
||||
|
||||
const setupCssInjection = (webContents: Electron.WebContents) => {
|
||||
webContents.on('did-finish-load', () => {
|
||||
cssToInject.forEach(async (callback, css) => {
|
||||
await webContents.insertCSS(css);
|
||||
callback?.();
|
||||
});
|
||||
|
||||
cssToInjectFile.forEach(async (callback, filepath) => {
|
||||
await webContents.insertCSS(fs.readFileSync(filepath, 'utf-8'));
|
||||
callback?.();
|
||||
});
|
||||
});
|
||||
};
|
||||
17
src/plugins/utils/main/fs.ts
Normal file
17
src/plugins/utils/main/fs.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import fs from 'node:fs';
|
||||
|
||||
export const fileExists = (
|
||||
path: fs.PathLike,
|
||||
callbackIfExists: { (): void; (): void; (): void; },
|
||||
callbackIfError: (() => void) | undefined = undefined,
|
||||
) => {
|
||||
fs.access(path, fs.constants.F_OK, (error) => {
|
||||
if (error) {
|
||||
callbackIfError?.();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
callbackIfExists();
|
||||
});
|
||||
};
|
||||
3
src/plugins/utils/main/index.ts
Normal file
3
src/plugins/utils/main/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './css';
|
||||
export * from './fs';
|
||||
export * from './plugin';
|
||||
16
src/plugins/utils/main/plugin.ts
Normal file
16
src/plugins/utils/main/plugin.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import is from 'electron-is';
|
||||
|
||||
import defaultConfig from '../../../config/defaults';
|
||||
|
||||
export const getAvailablePluginNames = () => {
|
||||
return Object.keys(defaultConfig.plugins).filter((name) => {
|
||||
if (is.windows() && name === 'touchbar') {
|
||||
return false;
|
||||
} else if (is.macOS() && name === 'taskbar-mediacontrol') {
|
||||
return false;
|
||||
} else if (is.linux() && (name === 'taskbar-mediacontrol' || name === 'touchbar')) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
};
|
||||
8
src/plugins/utils/renderer/html.ts
Normal file
8
src/plugins/utils/renderer/html.ts
Normal file
@ -0,0 +1,8 @@
|
||||
// Creates a DOM element from an HTML string
|
||||
export const ElementFromHtml = (html: string): HTMLElement => {
|
||||
const template = document.createElement('template');
|
||||
html = html.trim(); // Never return a text node of whitespace as the result
|
||||
template.innerHTML = html;
|
||||
|
||||
return template.content.firstElementChild as HTMLElement;
|
||||
};
|
||||
1
src/plugins/utils/renderer/index.ts
Normal file
1
src/plugins/utils/renderer/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './html';
|
||||
Reference in New Issue
Block a user