From c9bc4024ef0d9bbd834c13e8153b448b7cef690c Mon Sep 17 00:00:00 2001 From: the1812 Date: Sat, 18 Mar 2023 21:42:19 +0800 Subject: [PATCH] Add cv search (#677) --- .../lib/plugins/launch-bar/cv-search/index.ts | 37 +++++++++++++++++++ src/plugins/id-search/index.ts | 16 ++++---- 2 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 registry/lib/plugins/launch-bar/cv-search/index.ts diff --git a/registry/lib/plugins/launch-bar/cv-search/index.ts b/registry/lib/plugins/launch-bar/cv-search/index.ts new file mode 100644 index 0000000000..324ceed0c6 --- /dev/null +++ b/registry/lib/plugins/launch-bar/cv-search/index.ts @@ -0,0 +1,37 @@ +import type { LaunchBarActionProvider } from '@/components/launch-bar/launch-bar-action' +import type { PluginMetadata } from '@/plugins/plugin' +import { getJson } from '@/core/ajax' + +export const plugin: PluginMetadata = { + name: 'launchBar.actions.cvSearch', + displayName: '搜索栏 - 专栏跳转', + async setup({ addData }) { + addData('launchBar.actions', (providers: LaunchBarActionProvider[]) => { + providers.push({ + name: 'cvSearchProvider', + getActions: async input => { + const match = input.match(/^cv(\d+)$/) + if (!match) { + return [] + } + const id = match[1] + const indexer = `cv${id}` + const json = await getJson(`https://api.bilibili.com/x/article/viewinfo?id=${id}`) + const { title } = lodash.get(json, 'data', {}) + return [ + { + name: title || indexer, + icon: 'mdi-open-in-new', + indexer, + description: '专栏跳转', + action: () => { + window.open(`https://www.bilibili.com/read/${indexer}`, '_blank') + }, + order: 0, + }, + ] + }, + }) + }) + }, +} diff --git a/src/plugins/id-search/index.ts b/src/plugins/id-search/index.ts index a223d42863..551e511a8c 100644 --- a/src/plugins/id-search/index.ts +++ b/src/plugins/id-search/index.ts @@ -28,13 +28,14 @@ const idMatches: IdSearchProvider[] = [ ) const data = lodash.get(json, 'data', {}) const { bvid, title } = data + const indexer = `av${match[1]}` return { - name: title, + name: title || indexer, description: 'av号跳转', - indexer: `av${match[1]}`, + indexer, link: `https://www.bilibili.com/av${match[1]}`, - extraActions: bvid ? await getCopyItem('BV号', bvid, `av${match[1]}`) : [], + extraActions: bvid ? await getCopyItem('BV号', bvid, indexer) : [], } }, }, @@ -47,20 +48,21 @@ const idMatches: IdSearchProvider[] = [ ) const data = lodash.get(json, 'data', {}) const { aid, title } = data + const indexer = `BV${match[1]}` return { - name: title, + name: title || indexer, description: 'BV号跳转', - indexer: `BV${match[1]}`, + indexer, link: `https://www.bilibili.com/BV${match[1]}`, - extraActions: aid ? await getCopyItem('av号', `av${aid}`, `BV${match[1]}`) : [], + extraActions: aid ? await getCopyItem('av号', `av${aid}`, indexer) : [], } }, }, ] export const plugin: PluginMetadata = { name: 'launchBar.actions.IDSearch', - displayName: 'ID搜索快速跳转', + displayName: '搜索栏 - 视频跳转', async setup() { const { addData } = await import('../data') const { LaunchBarActionProviders } = await import('@/components/launch-bar/launch-bar-action')