2018-03-26 09:29:35 +08:00
|
|
|
import ScriptContext from "./script_context.js";
|
2018-03-27 10:29:14 +08:00
|
|
|
import server from "./server.js";
|
2019-10-20 16:00:18 +08:00
|
|
|
import toastService from "./toast.js";
|
2018-03-26 09:16:57 +08:00
|
|
|
|
2018-08-10 19:30:20 +08:00
|
|
|
async function getAndExecuteBundle(noteId, originEntity = null) {
|
2018-07-29 22:06:13 +08:00
|
|
|
const bundle = await server.get('script/bundle/' + noteId);
|
|
|
|
|
2019-08-17 17:28:36 +08:00
|
|
|
return await executeBundle(bundle, originEntity);
|
2018-07-29 22:06:13 +08:00
|
|
|
}
|
|
|
|
|
2020-02-26 02:19:10 +08:00
|
|
|
async function executeBundle(bundle, originEntity, $container) {
|
|
|
|
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);
|
2018-03-26 09:16:57 +08:00
|
|
|
|
2018-08-17 17:31:42 +08:00
|
|
|
try {
|
|
|
|
return await (function () {
|
|
|
|
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
|
|
|
|
}.call(apiContext));
|
|
|
|
}
|
|
|
|
catch (e) {
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.showAndLogError(`Execution of ${bundle.noteId} failed with error: ${e.message}`);
|
2018-08-17 17:31:42 +08:00
|
|
|
}
|
2018-03-26 09:16:57 +08:00
|
|
|
}
|
|
|
|
|
2018-03-27 10:29:14 +08:00
|
|
|
async function executeStartupBundles() {
|
|
|
|
const scriptBundles = await server.get("script/startup");
|
|
|
|
|
|
|
|
for (const bundle of scriptBundles) {
|
|
|
|
await executeBundle(bundle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
export default {
|
2018-03-27 10:29:14 +08:00
|
|
|
executeBundle,
|
2018-07-29 22:06:13 +08:00
|
|
|
getAndExecuteBundle,
|
2020-02-04 04:16:33 +08:00
|
|
|
executeStartupBundles
|
2018-03-26 09:16:57 +08:00
|
|
|
}
|