2024-02-19 12:29:37 +02:00
|
|
|
import { LogMessageCallback, ContentScriptData } from '../../types';
|
2023-12-13 21:45:02 +02:00
|
|
|
import CodeMirrorControl from '../CodeMirrorControl';
|
|
|
|
import codeMirrorRequire from './codeMirrorRequire';
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
let pluginScriptIdCounter = 0;
|
2023-12-13 21:45:02 +02:00
|
|
|
let pluginLoaderCounter = 0;
|
2023-09-21 10:12:40 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-09-21 10:12:40 +02:00
|
|
|
type OnScriptLoadCallback = (exports: any)=> void;
|
|
|
|
type OnPluginRemovedCallback = ()=> void;
|
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
const contentScriptToId = (contentScript: ContentScriptData) => `${contentScript.pluginId}--${contentScript.contentScriptId}`;
|
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
export default class PluginLoader {
|
|
|
|
private pluginScriptsContainer: HTMLElement;
|
2024-02-19 12:29:37 +02:00
|
|
|
private loadedContentScriptIds: string[] = [];
|
2023-09-21 10:12:40 +02:00
|
|
|
private pluginRemovalCallbacks: Record<string, OnPluginRemovedCallback> = {};
|
2023-12-13 21:45:02 +02:00
|
|
|
private pluginLoaderId: number;
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
public constructor(private editor: CodeMirrorControl, private logMessage: LogMessageCallback) {
|
|
|
|
this.pluginScriptsContainer = document.createElement('div');
|
|
|
|
this.pluginScriptsContainer.style.display = 'none';
|
|
|
|
|
|
|
|
// For testing
|
|
|
|
this.pluginScriptsContainer.id = 'joplin-plugin-scripts-container';
|
|
|
|
|
|
|
|
document.body.appendChild(this.pluginScriptsContainer);
|
|
|
|
|
2023-12-13 21:45:02 +02:00
|
|
|
// addPlugin works by creating <script> elements with the plugin's content. To pass
|
|
|
|
// information to this <script>, we use global objects:
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-12-13 21:45:02 +02:00
|
|
|
(window as any).__pluginLoaderScriptLoadCallbacks ??= Object.create(null);
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-12-13 21:45:02 +02:00
|
|
|
(window as any).__pluginLoaderRequireFunctions ??= Object.create(null);
|
|
|
|
|
|
|
|
this.pluginLoaderId = pluginLoaderCounter++;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-12-13 21:45:02 +02:00
|
|
|
(window as any).__pluginLoaderRequireFunctions[this.pluginLoaderId] = codeMirrorRequire;
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
public async setPlugins(contentScripts: ContentScriptData[]) {
|
|
|
|
for (const contentScript of contentScripts) {
|
|
|
|
const id = contentScriptToId(contentScript);
|
|
|
|
if (!this.loadedContentScriptIds.includes(id)) {
|
|
|
|
this.addPlugin(contentScript);
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove old plugins
|
2024-02-19 12:29:37 +02:00
|
|
|
const contentScriptIds = contentScripts.map(contentScriptToId);
|
|
|
|
const removedIds = this.loadedContentScriptIds
|
|
|
|
.filter(id => !contentScriptIds.includes(id));
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
for (const id of removedIds) {
|
|
|
|
if (id in this.pluginRemovalCallbacks) {
|
|
|
|
this.pluginRemovalCallbacks[id]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
private addPlugin(plugin: ContentScriptData) {
|
2023-09-21 10:12:40 +02:00
|
|
|
const onRemoveCallbacks: OnPluginRemovedCallback[] = [];
|
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
this.logMessage(`Loading plugin ${plugin.pluginId}, content script ${plugin.contentScriptId}`);
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
const addScript = (onLoad: OnScriptLoadCallback) => {
|
|
|
|
const scriptElement = document.createElement('script');
|
|
|
|
|
|
|
|
onRemoveCallbacks.push(() => {
|
|
|
|
scriptElement.remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
void (async () => {
|
|
|
|
const scriptId = pluginScriptIdCounter++;
|
|
|
|
const js = await plugin.contentScriptJs();
|
|
|
|
|
|
|
|
// Stop if cancelled
|
2024-02-19 12:29:37 +02:00
|
|
|
if (!this.loadedContentScriptIds.includes(contentScriptToId(plugin))) {
|
2023-09-21 10:12:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-22 17:36:10 +02:00
|
|
|
scriptElement.appendChild(document.createTextNode(`
|
2023-09-21 10:12:40 +02:00
|
|
|
(async () => {
|
|
|
|
const exports = {};
|
2024-07-23 21:10:59 +02:00
|
|
|
const module = { exports: exports };
|
2023-12-13 21:45:02 +02:00
|
|
|
const require = window.__pluginLoaderRequireFunctions[${JSON.stringify(this.pluginLoaderId)}];
|
|
|
|
const joplin = {
|
|
|
|
require,
|
|
|
|
};
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
${js};
|
|
|
|
|
2024-07-23 21:10:59 +02:00
|
|
|
window.__pluginLoaderScriptLoadCallbacks[${JSON.stringify(scriptId)}](module.exports);
|
2023-09-21 10:12:40 +02:00
|
|
|
})();
|
2024-02-22 17:36:10 +02:00
|
|
|
`));
|
2023-09-21 10:12:40 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-12-13 21:45:02 +02:00
|
|
|
(window as any).__pluginLoaderScriptLoadCallbacks[scriptId] = onLoad;
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
this.pluginScriptsContainer.appendChild(scriptElement);
|
|
|
|
})();
|
|
|
|
};
|
|
|
|
|
|
|
|
const addStyles = (cssStrings: string[]) => {
|
|
|
|
// A container for style elements
|
|
|
|
const styleContainer = document.createElement('div');
|
|
|
|
|
|
|
|
onRemoveCallbacks.push(() => {
|
|
|
|
styleContainer.remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const cssText of cssStrings) {
|
|
|
|
const style = document.createElement('style');
|
|
|
|
style.innerText = cssText;
|
|
|
|
styleContainer.appendChild(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pluginScriptsContainer.appendChild(styleContainer);
|
|
|
|
};
|
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
this.pluginRemovalCallbacks[contentScriptToId(plugin)] = () => {
|
2023-09-21 10:12:40 +02:00
|
|
|
for (const callback of onRemoveCallbacks) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
this.loadedContentScriptIds = this.loadedContentScriptIds.filter(id => {
|
|
|
|
return id !== contentScriptToId(plugin);
|
2023-09-21 10:12:40 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-01-22 17:27:42 +02:00
|
|
|
addScript(async exports => {
|
2023-09-21 10:12:40 +02:00
|
|
|
if (!exports?.default || !(typeof exports.default === 'function')) {
|
|
|
|
throw new Error('All plugins must have a function default export');
|
|
|
|
}
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
postMessage: plugin.postMessageHandler,
|
|
|
|
pluginId: plugin.pluginId,
|
|
|
|
contentScriptId: plugin.contentScriptId,
|
|
|
|
};
|
2024-03-09 12:48:22 +02:00
|
|
|
const loadedPlugin = exports.default(context) ?? {};
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
loadedPlugin.plugin?.(this.editor);
|
|
|
|
|
|
|
|
if (loadedPlugin.codeMirrorOptions) {
|
|
|
|
for (const key in loadedPlugin.codeMirrorOptions) {
|
|
|
|
this.editor.setOption(key, loadedPlugin.codeMirrorOptions[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loadedPlugin.assets) {
|
|
|
|
const cssStrings = [];
|
|
|
|
|
|
|
|
for (const asset of loadedPlugin.assets()) {
|
2024-01-22 17:27:42 +02:00
|
|
|
let assetText: string = asset.text;
|
|
|
|
let assetMime: string = asset.mime;
|
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
if (!asset.inline) {
|
2024-01-22 17:27:42 +02:00
|
|
|
if (!asset.name) {
|
|
|
|
throw new Error('Non-inline asset missing required property "name"');
|
|
|
|
}
|
|
|
|
if (assetMime !== 'text/css' && !asset.name.endsWith('.css')) {
|
|
|
|
throw new Error(
|
|
|
|
`Non-css assets are not supported by the CodeMirror 6 editor. (Asset path: ${asset.name})`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assetText = await plugin.loadCssAsset(asset.name);
|
|
|
|
assetMime = 'text/css';
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
2024-01-22 17:27:42 +02:00
|
|
|
if (assetMime !== 'text/css') {
|
|
|
|
throw new Error(
|
|
|
|
'Plugin assets must have property "mime" set to "text/css" or have a filename ending with ".css"',
|
|
|
|
);
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
2024-01-22 17:27:42 +02:00
|
|
|
cssStrings.push(assetText);
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addStyles(cssStrings);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
this.loadedContentScriptIds.push(contentScriptToId(plugin));
|
2023-09-21 10:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public remove() {
|
|
|
|
this.pluginScriptsContainer.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|