文件
mynode/packages/lib/services/plugins/utils/loadContentScripts.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

80 行
3.2 KiB
TypeScript

import { PluginStates } from '../reducer';
import { ContentScriptType, ContentScriptContext, PostMessageHandler, ContentScriptModule } from '../api/types';
import { dirname } from '@joplin/utils/path';
import shim from '../../../shim';
import Logger from '@joplin/utils/Logger';
import PluginService from '../PluginService';
const logger = Logger.create('loadContentScripts');
export interface ExtraContentScript {
id: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- module is a ContentScriptModule extended at runtime with CodeMirror v5/v6/markdown-it specific fields (codeMirrorResources, codeMirrorOptions, plugin); narrowing forces casts at every consumer
module: any;
assetPath: string;
pluginId: string;
}
function postMessageHandler(pluginId: string, scriptType: ContentScriptType, contentScriptId: string): PostMessageHandler {
return async (message: unknown) => {
if (scriptType === ContentScriptType.MarkdownItPlugin) {
logger.error('context.postMessage is not available to renderer content scripts');
return undefined;
} else {
const plugin = PluginService.instance().pluginById(pluginId);
return plugin.emitContentScriptMessage(contentScriptId, message);
}
};
}
export function contentScriptsToRendererRules(plugins: PluginStates): ExtraContentScript[] {
return loadContentScripts(plugins, ContentScriptType.MarkdownItPlugin);
}
export function contentScriptsToCodeMirrorPlugin(plugins: PluginStates): ExtraContentScript[] {
return loadContentScripts(plugins, ContentScriptType.CodeMirrorPlugin);
}
function loadContentScripts(plugins: PluginStates, scriptType: ContentScriptType): ExtraContentScript[] {
if (!plugins) return null;
const output: ExtraContentScript[] = [];
for (const pluginId in plugins) {
const plugin = plugins[pluginId];
const contentScripts = plugin.contentScripts[scriptType];
if (!contentScripts) continue;
for (const contentScript of contentScripts) {
try {
const module = shim.requireDynamic(contentScript.path);
if (!module.default || typeof module.default !== 'function') throw new Error(`Content script must export a function under the "default" key: Plugin: ${pluginId}: Script: ${contentScript.id}`);
const context: ContentScriptContext = {
pluginId,
contentScriptId: contentScript.id,
postMessage: postMessageHandler(pluginId, scriptType, contentScript.id),
};
const loadedModule = module.default(context) as ContentScriptModule & { codeMirrorResources?: unknown; codeMirrorOptions?: unknown };
if (!loadedModule.plugin && !loadedModule.codeMirrorResources && !loadedModule.codeMirrorOptions) throw new Error(`Content script must export a "plugin" key or a list of CodeMirror assets or define a CodeMirror option: Plugin: ${pluginId}: Script: ${contentScript.id}`);
output.push({
id: contentScript.id,
module: loadedModule,
assetPath: dirname(contentScript.path),
pluginId,
});
} catch (error) {
// This function must not throw as doing so would crash the
// application, which we want to avoid for plugins. Instead log
// the error, and continue loading the other content scripts.
logger.error(error.message);
}
}
}
return output;
}