2023-10-22 10:33:40 +02:00
|
|
|
import { inputText } from "@/os";
|
2023-11-16 21:18:19 +01:00
|
|
|
import { createAiScriptEnv } from "@/scripts/aiscript/api";
|
2023-01-13 05:40:33 +01:00
|
|
|
import {
|
|
|
|
noteActions,
|
|
|
|
notePostInterruptors,
|
|
|
|
noteViewInterruptors,
|
|
|
|
postFormActions,
|
|
|
|
userActions,
|
|
|
|
} from "@/store";
|
2023-11-26 21:27:02 +01:00
|
|
|
import { Interpreter, Parser, utils, values } from "@syuilo/aiscript";
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-11-16 21:04:05 +01:00
|
|
|
const parser = new Parser();
|
|
|
|
const pluginContexts = new Map<string, Interpreter>();
|
2020-10-17 13:12:00 +02:00
|
|
|
|
|
|
|
export function install(plugin) {
|
2023-01-13 05:40:33 +01:00
|
|
|
console.info("Plugin installed:", plugin.name, `v${plugin.version}`);
|
2020-10-17 13:12:00 +02:00
|
|
|
|
2023-11-16 21:04:05 +01:00
|
|
|
const aiscript = new Interpreter(
|
2023-01-13 05:40:33 +01:00
|
|
|
createPluginEnv({
|
2023-09-02 01:27:33 +02:00
|
|
|
plugin,
|
2023-01-13 05:40:33 +01:00
|
|
|
storageKey: `plugins:${plugin.id}`,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
in: (q) => {
|
|
|
|
return new Promise((ok) => {
|
|
|
|
inputText({
|
|
|
|
title: q,
|
|
|
|
}).then(({ canceled, result: a }) => {
|
|
|
|
ok(a);
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
});
|
2023-01-13 05:40:33 +01:00
|
|
|
},
|
|
|
|
out: (value) => {
|
|
|
|
console.log(value);
|
|
|
|
},
|
|
|
|
log: (type, params) => {},
|
2020-10-17 13:12:00 +02:00
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
);
|
2020-10-17 13:12:00 +02:00
|
|
|
|
|
|
|
initPlugin({ plugin, aiscript });
|
|
|
|
|
2023-11-16 21:04:05 +01:00
|
|
|
aiscript.exec(parser.parse(plugin.src));
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createPluginEnv(opts) {
|
2023-11-16 21:04:05 +01:00
|
|
|
const config = new Map<string, values.Value>();
|
|
|
|
for (const [k, v] of Object.entries(opts.plugin.config ?? {})) {
|
2023-01-13 05:40:33 +01:00
|
|
|
config.set(
|
|
|
|
k,
|
2023-11-16 21:04:05 +01:00
|
|
|
utils.jsToVal(
|
2023-01-13 05:40:33 +01:00
|
|
|
typeof opts.plugin.configData[k] !== "undefined"
|
|
|
|
? opts.plugin.configData[k]
|
|
|
|
: v.default,
|
|
|
|
),
|
|
|
|
);
|
2020-10-17 13:12:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...createAiScriptEnv({ ...opts, token: opts.plugin.token }),
|
2023-09-02 01:27:33 +02:00
|
|
|
// #region Deprecated
|
2023-01-13 05:40:33 +01:00
|
|
|
"Mk:register_post_form_action": values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerPostFormAction({
|
|
|
|
pluginId: opts.plugin.id,
|
|
|
|
title: title.value,
|
|
|
|
handler,
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
}),
|
2023-01-13 05:40:33 +01:00
|
|
|
"Mk:register_user_action": values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerUserAction({
|
|
|
|
pluginId: opts.plugin.id,
|
|
|
|
title: title.value,
|
|
|
|
handler,
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
}),
|
2023-01-13 05:40:33 +01:00
|
|
|
"Mk:register_note_action": values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerNoteAction({
|
|
|
|
pluginId: opts.plugin.id,
|
|
|
|
title: title.value,
|
|
|
|
handler,
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
}),
|
2023-09-02 01:27:33 +02:00
|
|
|
// #endregion
|
2023-01-13 05:40:33 +01:00
|
|
|
"Plugin:register_post_form_action": values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerPostFormAction({
|
|
|
|
pluginId: opts.plugin.id,
|
|
|
|
title: title.value,
|
|
|
|
handler,
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
}),
|
2023-01-13 05:40:33 +01:00
|
|
|
"Plugin:register_user_action": values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerUserAction({
|
|
|
|
pluginId: opts.plugin.id,
|
|
|
|
title: title.value,
|
|
|
|
handler,
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
}),
|
2023-01-13 05:40:33 +01:00
|
|
|
"Plugin:register_note_action": values.FN_NATIVE(([title, handler]) => {
|
|
|
|
registerNoteAction({
|
|
|
|
pluginId: opts.plugin.id,
|
|
|
|
title: title.value,
|
|
|
|
handler,
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
}),
|
2023-01-13 05:40:33 +01:00
|
|
|
"Plugin:register_note_view_interruptor": values.FN_NATIVE(([handler]) => {
|
2023-09-02 01:27:33 +02:00
|
|
|
registerNoteViewInterruptor({
|
|
|
|
pluginId: opts.plugin.id,
|
|
|
|
handler,
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
}),
|
2023-01-13 05:40:33 +01:00
|
|
|
"Plugin:register_note_post_interruptor": values.FN_NATIVE(([handler]) => {
|
2023-09-02 01:27:33 +02:00
|
|
|
registerNotePostInterruptor({
|
|
|
|
pluginId: opts.plugin.id,
|
|
|
|
handler,
|
|
|
|
});
|
2020-10-17 13:12:00 +02:00
|
|
|
}),
|
2023-11-16 21:04:05 +01:00
|
|
|
"Plugin:register_page_view_interruptor": values.FN_NATIVE(([handler]) => {
|
2023-11-16 21:18:19 +01:00
|
|
|
registerPageViewInterruptor({
|
|
|
|
pluginId: opts.plugin.id,
|
|
|
|
handler,
|
|
|
|
});
|
2023-11-16 21:04:05 +01:00
|
|
|
}),
|
2023-01-13 05:40:33 +01:00
|
|
|
"Plugin:open_url": values.FN_NATIVE(([url]) => {
|
|
|
|
window.open(url.value, "_blank");
|
2020-10-17 13:12:00 +02:00
|
|
|
}),
|
2023-01-13 05:40:33 +01:00
|
|
|
"Plugin:config": values.OBJ(config),
|
2020-10-17 13:12:00 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function initPlugin({ plugin, aiscript }) {
|
|
|
|
pluginContexts.set(plugin.id, aiscript);
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerPostFormAction({ pluginId, title, handler }) {
|
|
|
|
postFormActions.push({
|
2023-01-13 05:40:33 +01:00
|
|
|
title,
|
|
|
|
handler: (form, update) => {
|
2023-11-16 21:04:05 +01:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginContext.execFn(handler, [
|
2023-01-13 05:40:33 +01:00
|
|
|
utils.jsToVal(form),
|
|
|
|
values.FN_NATIVE(([key, value]) => {
|
2023-11-16 21:04:05 +01:00
|
|
|
if (!key || !value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
update(utils.valToJs(key), utils.valToJs(value));
|
2023-01-13 05:40:33 +01:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
},
|
2020-10-17 13:12:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerUserAction({ pluginId, title, handler }) {
|
|
|
|
userActions.push({
|
2023-01-13 05:40:33 +01:00
|
|
|
title,
|
|
|
|
handler: (user) => {
|
2023-11-16 21:04:05 +01:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginContext.execFn(handler, [utils.jsToVal(user)]);
|
2023-01-13 05:40:33 +01:00
|
|
|
},
|
2020-10-17 13:12:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerNoteAction({ pluginId, title, handler }) {
|
|
|
|
noteActions.push({
|
2023-01-13 05:40:33 +01:00
|
|
|
title,
|
|
|
|
handler: (note) => {
|
2023-11-16 21:04:05 +01:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pluginContext.execFn(handler, [utils.jsToVal(user)]);
|
2023-01-13 05:40:33 +01:00
|
|
|
},
|
2020-10-17 13:12:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerNoteViewInterruptor({ pluginId, handler }) {
|
|
|
|
noteViewInterruptors.push({
|
|
|
|
handler: async (note) => {
|
2023-11-16 21:04:05 +01:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-13 05:40:33 +01:00
|
|
|
return utils.valToJs(
|
2023-11-16 21:04:05 +01:00
|
|
|
await pluginContext.execFn(handler, [utils.jsToVal(note)]),
|
2023-01-13 05:40:33 +01:00
|
|
|
);
|
|
|
|
},
|
2020-10-17 13:12:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerNotePostInterruptor({ pluginId, handler }) {
|
|
|
|
notePostInterruptors.push({
|
|
|
|
handler: async (note) => {
|
2023-11-16 21:04:05 +01:00
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return utils.valToJs(
|
|
|
|
await pluginContext.execFn(handler, [utils.jsToVal(note)]),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registerPageViewInterruptor({ pluginId, handler }): void {
|
|
|
|
pageViewInterruptors.push({
|
|
|
|
handler: async (page) => {
|
|
|
|
const pluginContext = pluginContexts.get(pluginId);
|
|
|
|
if (!pluginContext) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-13 05:40:33 +01:00
|
|
|
return utils.valToJs(
|
2023-11-16 21:04:05 +01:00
|
|
|
await pluginContext.execFn(handler, [utils.jsToVal(page)]),
|
2023-01-13 05:40:33 +01:00
|
|
|
);
|
|
|
|
},
|
2020-10-17 13:12:00 +02:00
|
|
|
});
|
|
|
|
}
|