mirror of
https://github.com/zadam/trilium.git
synced 2024-09-30 12:35:59 +08:00
36 lines
1 KiB
JavaScript
36 lines
1 KiB
JavaScript
import ScriptContext from "./script_context.js";
|
|
import server from "./server.js";
|
|
import toastService from "./toast.js";
|
|
|
|
async function getAndExecuteBundle(noteId, originEntity = null) {
|
|
const bundle = await server.get('script/bundle/' + noteId);
|
|
|
|
return await executeBundle(bundle, originEntity);
|
|
}
|
|
|
|
async function executeBundle(bundle, originEntity, $container) {
|
|
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);
|
|
|
|
try {
|
|
return await (function () {
|
|
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
|
|
}.call(apiContext));
|
|
}
|
|
catch (e) {
|
|
toastService.showAndLogError(`Execution of ${bundle.noteId} failed with error: ${e.message}`);
|
|
}
|
|
}
|
|
|
|
async function executeStartupBundles() {
|
|
const scriptBundles = await server.get("script/startup");
|
|
|
|
for (const bundle of scriptBundles) {
|
|
await executeBundle(bundle);
|
|
}
|
|
}
|
|
|
|
export default {
|
|
executeBundle,
|
|
getAndExecuteBundle,
|
|
executeStartupBundles
|
|
} |