feat: refactor plugin utils (#1391)

This commit is contained in:
JellyBrick
2023-11-09 11:06:06 +09:00
committed by GitHub
parent 269352af97
commit 47cccbce7c
31 changed files with 68 additions and 65 deletions

View 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?.();
});
});
};

View 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();
});
};

View File

@ -0,0 +1,3 @@
export * from './css';
export * from './fs';
export * from './plugin';

View 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;
});
};

View 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;
};

View File

@ -0,0 +1 @@
export * from './html';