enhance(frontend): improve plugin management

This commit is contained in:
syuilo 2025-03-09 21:23:36 +09:00
parent 6cefabc6b6
commit 0402866b43
8 changed files with 131 additions and 144 deletions

View File

@ -222,7 +222,7 @@ import { isEnabledUrlPreview } from '@/instance.js';
import { focusPrev, focusNext } from '@/utility/focus.js';
import { getAppearNote } from '@/utility/get-appear-note.js';
import { prefer } from '@/preferences.js';
import { noteViewInterruptors } from '@/plugin.js';
import { getPluginHandlers } from '@/plugin.js';
const props = withDefaults(defineProps<{
note: Misskey.entities.Note;
@ -248,6 +248,7 @@ const currentClip = inject<Ref<Misskey.entities.Clip> | null>('currentClip', nul
const note = ref(deepClone(props.note));
// plugin
const noteViewInterruptors = getPluginHandlers('note_view_interruptor');
if (noteViewInterruptors.length > 0) {
onMounted(async () => {
let result: Misskey.entities.Note | null = deepClone(note.value);

View File

@ -254,7 +254,7 @@ import MkButton from '@/components/MkButton.vue';
import { isEnabledUrlPreview } from '@/instance.js';
import { getAppearNote } from '@/utility/get-appear-note.js';
import { prefer } from '@/preferences.js';
import { noteViewInterruptors } from '@/plugin.js';
import { getPluginHandlers } from '@/plugin.js';
const props = withDefaults(defineProps<{
note: Misskey.entities.Note;
@ -268,6 +268,7 @@ const inChannel = inject('inChannel', null);
const note = ref(deepClone(props.note));
// plugin
const noteViewInterruptors = getPluginHandlers('note_view_interruptor');
if (noteViewInterruptors.length > 0) {
onMounted(async () => {
let result: Misskey.entities.Note | null = deepClone(note.value);

View File

@ -136,7 +136,7 @@ import { claimAchievement } from '@/utility/achievements.js';
import { emojiPicker } from '@/utility/emoji-picker.js';
import { mfmFunctionPicker } from '@/utility/mfm-function-picker.js';
import { prefer } from '@/preferences.js';
import { notePostInterruptors, postFormActions } from '@/plugin.js';
import { getPluginHandlers } from '@/plugin.js';
const $i = signinRequired();
@ -197,6 +197,7 @@ const showingOptions = ref(false);
const textAreaReadOnly = ref(false);
const justEndedComposition = ref(false);
const renoteTargetNote: ShallowRef<PostFormProps['renote'] | null> = shallowRef(props.renote);
const postFormActions = getPluginHandlers('post_form_action');
const draftKey = computed((): string => {
let key = props.channel ? `channel:${props.channel.id}` : '';
@ -823,6 +824,7 @@ async function post(ev?: MouseEvent) {
}
// plugin
const notePostInterruptors = getPluginHandlers('note_post_interruptor');
if (notePostInterruptors.length > 0) {
for (const interruptor of notePostInterruptors) {
try {

View File

@ -122,7 +122,7 @@ import { getStaticImageUrl } from '@/utility/media-proxy.js';
import { copyToClipboard } from '@/utility/copy-to-clipboard.js';
import { useRouter } from '@/router/supplier.js';
import { prefer } from '@/preferences.js';
import { pageViewInterruptors } from '@/plugin.js';
import { getPluginHandlers } from '@/plugin.js';
const router = useRouter();
@ -151,6 +151,7 @@ function fetchPage() {
page.value = _page;
// plugin
const pageViewInterruptors = getPluginHandlers('page_view_interruptor');
if (pageViewInterruptors.length > 0) {
let result = deepClone(_page);
for (const interruptor of pageViewInterruptors) {

View File

@ -28,14 +28,14 @@ SPDX-License-Identifier: AGPL-3.0-only
</template>
<template #footer>
<div class="_buttons">
<MkButton v-if="plugin.config" inline @click="config(plugin)"><i class="ti ti-settings"></i> {{ i18n.ts.settings }}</MkButton>
<MkButton inline danger @click="uninstall(plugin)"><i class="ti ti-trash"></i> {{ i18n.ts.uninstall }}</MkButton>
<MkButton :disabled="!plugin.active" @click="reload(plugin)"><i class="ti ti-refresh"></i> {{ i18n.ts.reload }}</MkButton>
<MkButton danger @click="uninstall(plugin)"><i class="ti ti-trash"></i> {{ i18n.ts.uninstall }}</MkButton>
<MkButton v-if="plugin.config" style="margin-left: auto;" @click="config(plugin)"><i class="ti ti-settings"></i> {{ i18n.ts.settings }}</MkButton>
</div>
</template>
<div class="_gaps_m">
<div class="_gaps_s">
<span style="display: flex; align-items: center;"><b>{{ plugin.name }}</b><span style="margin-left: auto;">v{{ plugin.version }}</span></span>
<MkSwitch :modelValue="plugin.active" @update:modelValue="changeActive(plugin, $event)">{{ i18n.ts.makeActive }}</MkSwitch>
</div>
@ -64,10 +64,6 @@ SPDX-License-Identifier: AGPL-3.0-only
<template #label>{{ i18n.ts._plugin.viewLog }}</template>
<div class="_gaps_s">
<div class="_buttons">
<MkButton inline @click="copy(pluginLogs.get(plugin.installId)?.join('\n'))"><i class="ti ti-copy"></i> {{ i18n.ts.copy }}</MkButton>
</div>
<MkCode :code="pluginLogs.get(plugin.installId)?.join('\n') ?? ''"/>
</div>
</MkFolder>
@ -77,10 +73,6 @@ SPDX-License-Identifier: AGPL-3.0-only
<template #label>{{ i18n.ts._plugin.viewSource }}</template>
<div class="_gaps_s">
<div class="_buttons">
<MkButton inline @click="copy(plugin.src)"><i class="ti ti-copy"></i> {{ i18n.ts.copy }}</MkButton>
</div>
<MkCode :code="plugin.src ?? ''" lang="ais"/>
</div>
</MkFolder>
@ -94,6 +86,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<script lang="ts" setup>
import { nextTick, ref, computed } from 'vue';
import type { Plugin } from '@/plugin.js';
import FormLink from '@/components/form/link.vue';
import MkSwitch from '@/components/MkSwitch.vue';
import FormSection from '@/components/form/section.vue';
@ -101,36 +94,30 @@ import MkButton from '@/components/MkButton.vue';
import MkCode from '@/components/MkCode.vue';
import MkFolder from '@/components/MkFolder.vue';
import MkKeyValue from '@/components/MkKeyValue.vue';
import * as os from '@/os.js';
import { copyToClipboard } from '@/utility/copy-to-clipboard.js';
import { unisonReload } from '@/utility/unison-reload.js';
import { i18n } from '@/i18n.js';
import { definePageMetadata } from '@/utility/page-metadata.js';
import { changePluginActive, configPlugin, pluginLogs, uninstallPlugin } from '@/plugin.js';
import { changePluginActive, configPlugin, pluginLogs, uninstallPlugin, reloadPlugin } from '@/plugin.js';
import { prefer } from '@/preferences.js';
const plugins = prefer.r.plugins;
async function uninstall(plugin) {
async function uninstall(plugin: Plugin) {
await uninstallPlugin(plugin);
nextTick(() => {
unisonReload();
});
}
function copy(text) {
copyToClipboard(text ?? '');
os.success();
function reload(plugin: Plugin) {
reloadPlugin(plugin);
}
async function config(plugin) {
async function config(plugin: Plugin) {
await configPlugin(plugin);
nextTick(() => {
location.reload();
});
}
function changeActive(plugin, active) {
function changeActive(plugin: Plugin, active: boolean) {
changePluginActive(plugin, active);
nextTick(() => {
location.reload();

View File

@ -37,40 +37,6 @@ export type AiScriptPluginMeta = {
config?: Record<string, any>;
};
interface PostFormAction {
title: string,
handler: <T>(form: T, update: (key: unknown, value: unknown) => void) => void;
}
interface UserAction {
title: string,
handler: (user: Misskey.entities.UserDetailed) => void;
}
interface NoteAction {
title: string,
handler: (note: Misskey.entities.Note) => void;
}
interface NoteViewInterruptor {
handler: (note: Misskey.entities.Note) => unknown;
}
interface NotePostInterruptor {
handler: (note: FIXME) => unknown;
}
interface PageViewInterruptor {
handler: (page: Misskey.entities.Page) => unknown;
}
export const postFormActions: PostFormAction[] = [];
export const userActions: UserAction[] = [];
export const noteActions: NoteAction[] = [];
export const noteViewInterruptors: NoteViewInterruptor[] = [];
export const notePostInterruptors: NotePostInterruptor[] = [];
export const pageViewInterruptors: PageViewInterruptor[] = [];
const parser = new Parser();
export function isSupportedAiScriptVersion(version: string): boolean {
@ -215,6 +181,42 @@ export function changePluginActive(plugin: Plugin, active: boolean) {
const pluginContexts = new Map<string, Interpreter>();
export const pluginLogs = ref(new Map<string, string[]>());
type HandlerDef = {
post_form_action: {
title: string,
handler: <T>(form: T, update: (key: unknown, value: unknown) => void) => void;
};
user_action: {
title: string,
handler: (user: Misskey.entities.UserDetailed) => void;
};
note_action: {
title: string,
handler: (note: Misskey.entities.Note) => void;
};
note_view_interruptor: {
handler: (note: Misskey.entities.Note) => unknown;
};
note_post_interruptor: {
handler: (note: FIXME) => unknown;
};
page_view_interruptor: {
handler: (page: Misskey.entities.Page) => unknown;
};
};
type PluginHandler<K extends keyof HandlerDef> = {
pluginInstallId: string;
type: K;
ctx: HandlerDef[K];
};
let pluginHandlers: PluginHandler<keyof HandlerDef>[] = [];
function addPluginHandler<K extends keyof HandlerDef>(installId: Plugin['installId'], type: K, ctx: PluginHandler<K>['ctx']) {
pluginHandlers.push({ pluginInstallId: installId, type, ctx });
}
export async function launchPlugin(plugin: Plugin): Promise<void> {
// 後方互換性のため
if (plugin.src == null) return;
@ -252,6 +254,18 @@ export async function launchPlugin(plugin: Plugin): Promise<void> {
);
}
export function reloadPlugin(plugin: Plugin): void {
const pluginContext = pluginContexts.get(plugin.installId);
if (!pluginContext) return;
pluginContext.abort();
pluginContexts.delete(plugin.installId);
pluginLogs.value.delete(plugin.installId);
pluginHandlers = pluginHandlers.filter(x => x.pluginInstallId !== plugin.installId);
launchPlugin(plugin);
}
function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Record<string, values.Value> {
const id = opts.plugin.installId;
@ -260,111 +274,90 @@ function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Record<s
config.set(k, utils.jsToVal(typeof opts.plugin.configData[k] !== 'undefined' ? opts.plugin.configData[k] : v.default));
}
function withContext<T>(fn: (ctx: Interpreter) => T): T {
console.log('withContext', id);
const ctx = pluginContexts.get(id);
if (!ctx) throw new Error('Plugin context not found');
return fn(ctx);
}
return {
...createAiScriptEnv({ ...opts, token: store.state.pluginTokens[id] }),
'Plugin:register_post_form_action': values.FN_NATIVE(([title, handler]) => {
utils.assertString(title);
registerPostFormAction({ pluginId: id, title: title.value, handler });
}),
'Plugin:register_user_action': values.FN_NATIVE(([title, handler]) => {
utils.assertString(title);
registerUserAction({ pluginId: id, title: title.value, handler });
}),
'Plugin:register_note_action': values.FN_NATIVE(([title, handler]) => {
utils.assertString(title);
registerNoteAction({ pluginId: id, title: title.value, handler });
}),
'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => {
registerNoteViewInterruptor({ pluginId: id, handler });
}),
'Plugin:register_note_post_interruptor': values.FN_NATIVE(([handler]) => {
registerNotePostInterruptor({ pluginId: id, handler });
}),
'Plugin:register_page_view_interruptor': values.FN_NATIVE(([handler]) => {
registerPageViewInterruptor({ pluginId: id, handler });
}),
'Plugin:open_url': values.FN_NATIVE(([url]) => {
utils.assertString(url);
window.open(url.value, '_blank', 'noopener');
}),
'Plugin:config': values.OBJ(config),
};
}
function registerPostFormAction({ pluginId, title, handler }): void {
postFormActions.push({
title, handler: (form, update) => {
const pluginContext = pluginContexts.get(pluginId);
if (!pluginContext) {
return;
}
pluginContext.execFn(handler, [utils.jsToVal(form), values.FN_NATIVE(([key, value]) => {
utils.assertFunction(handler);
addPluginHandler(id, 'post_form_action', {
title: title.value,
handler: withContext(ctx => (form, update) => {
ctx.execFn(handler, [utils.jsToVal(form), values.FN_NATIVE(([key, value]) => {
if (!key || !value) {
return;
}
update(utils.valToJs(key), utils.valToJs(value));
})]);
},
}),
});
}),
'Plugin:register_user_action': values.FN_NATIVE(([title, handler]) => {
utils.assertString(title);
utils.assertFunction(handler);
addPluginHandler(id, 'user_action', {
title: title.value,
handler: withContext(ctx => (user) => {
ctx.execFn(handler, [utils.jsToVal(user)]);
}),
});
}),
'Plugin:register_note_action': values.FN_NATIVE(([title, handler]) => {
utils.assertString(title);
utils.assertFunction(handler);
addPluginHandler(id, 'note_action', {
title: title.value,
handler: withContext(ctx => (note) => {
ctx.execFn(handler, [utils.jsToVal(note)]);
}),
});
}),
'Plugin:register_note_view_interruptor': values.FN_NATIVE(([handler]) => {
utils.assertFunction(handler);
addPluginHandler(id, 'note_view_interruptor', {
handler: withContext(ctx => async (note) => {
return utils.valToJs(await ctx.execFn(handler, [utils.jsToVal(note)]));
}),
});
}),
'Plugin:register_note_post_interruptor': values.FN_NATIVE(([handler]) => {
utils.assertFunction(handler);
addPluginHandler(id, 'note_post_interruptor', {
handler: withContext(ctx => async (note) => {
return utils.valToJs(await ctx.execFn(handler, [utils.jsToVal(note)]));
}),
});
}),
'Plugin:register_page_view_interruptor': values.FN_NATIVE(([handler]) => {
utils.assertFunction(handler);
addPluginHandler(id, 'page_view_interruptor', {
handler: withContext(ctx => async (page) => {
return utils.valToJs(await ctx.execFn(handler, [utils.jsToVal(page)]));
}),
});
}),
'Plugin:open_url': values.FN_NATIVE(([url]) => {
utils.assertString(url);
window.open(url.value, '_blank', 'noopener');
}),
'Plugin:config': values.OBJ(config),
};
}
function registerUserAction({ pluginId, title, handler }): void {
userActions.push({
title, handler: (user) => {
const pluginContext = pluginContexts.get(pluginId);
if (!pluginContext) {
return;
}
pluginContext.execFn(handler, [utils.jsToVal(user)]);
},
});
}
function registerNoteAction({ pluginId, title, handler }): void {
noteActions.push({
title, handler: (note) => {
const pluginContext = pluginContexts.get(pluginId);
if (!pluginContext) {
return;
}
pluginContext.execFn(handler, [utils.jsToVal(note)]);
},
});
}
function registerNoteViewInterruptor({ pluginId, handler }): void {
noteViewInterruptors.push({
handler: async (note) => {
const pluginContext = pluginContexts.get(pluginId);
if (!pluginContext) {
return;
}
return utils.valToJs(await pluginContext.execFn(handler, [utils.jsToVal(note)]));
},
});
}
function registerNotePostInterruptor({ pluginId, handler }): void {
notePostInterruptors.push({
handler: async (note) => {
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;
}
return utils.valToJs(await pluginContext.execFn(handler, [utils.jsToVal(page)]));
},
});
export function getPluginHandlers<K extends keyof HandlerDef>(type: K): HandlerDef[K][] {
return pluginHandlers.filter((x): x is PluginHandler<K> => x.type === type).map(x => x.ctx);
}

View File

@ -24,7 +24,7 @@ import { isSupportShare } from '@/utility/navigator.js';
import { getAppearNote } from '@/utility/get-appear-note.js';
import { genEmbedCode } from '@/utility/get-embed-code.js';
import { prefer } from '@/preferences.js';
import { noteActions } from '@/plugin.js';
import { getPluginHandlers } from '@/plugin.js';
export async function getNoteClipMenu(props: {
note: Misskey.entities.Note;
@ -497,6 +497,7 @@ export function getNoteMenu(props: {
}
}
const noteActions = getPluginHandlers('note_action');
if (noteActions.length > 0) {
menuItems.push({ type: 'divider' });

View File

@ -19,7 +19,7 @@ import { antennasCache, rolesCache, userListsCache } from '@/cache.js';
import { mainRouter } from '@/router/main.js';
import { genEmbedCode } from '@/utility/get-embed-code.js';
import { prefer } from '@/preferences.js';
import { userActions } from '@/plugin.js';
import { getPluginHandlers } from '@/plugin.js';
export function getUserMenu(user: Misskey.entities.UserDetailed, router: IRouter = mainRouter) {
const meId = $i ? $i.id : null;
@ -419,6 +419,7 @@ export function getUserMenu(user: Misskey.entities.UserDetailed, router: IRouter
});
}
const userActions = getPluginHandlers('user_action');
if (userActions.length > 0) {
menuItems.push({ type: 'divider' }, ...userActions.map(action => ({
icon: 'ti ti-plug',