From 7a2a5ec3f6913b4c4373d7f2c25a95c0780a4f81 Mon Sep 17 00:00:00 2001 From: the1812 Date: Sun, 23 Apr 2023 17:27:27 +0800 Subject: [PATCH] Add number search (fix #677) --- .../plugins/launch-bar/number-search/index.md | 3 ++ .../plugins/launch-bar/number-search/index.ts | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 registry/lib/plugins/launch-bar/number-search/index.md create mode 100644 registry/lib/plugins/launch-bar/number-search/index.ts diff --git a/registry/lib/plugins/launch-bar/number-search/index.md b/registry/lib/plugins/launch-bar/number-search/index.md new file mode 100644 index 0000000000..c9cd3d3e4d --- /dev/null +++ b/registry/lib/plugins/launch-bar/number-search/index.md @@ -0,0 +1,3 @@ +在输入纯数字时, 提供以下选项: +- 跳转至相应的视频 (视为 av 号) +- 跳转至相应的专栏 (视为 cv 号) diff --git a/registry/lib/plugins/launch-bar/number-search/index.ts b/registry/lib/plugins/launch-bar/number-search/index.ts new file mode 100644 index 0000000000..905e91ca99 --- /dev/null +++ b/registry/lib/plugins/launch-bar/number-search/index.ts @@ -0,0 +1,42 @@ +import type { LaunchBarActionProvider } from '@/components/launch-bar/launch-bar-action' +import type { PluginMetadata } from '@/plugins/plugin' +import { getJson, getJsonWithCredentials } from '@/core/ajax' +import { createLinkAction, matchInput } from '../common' + +export const plugin: PluginMetadata = { + name: 'launchBar.actions.numberSearch', + displayName: '搜索栏 - 数字联想', + async setup({ addData }) { + addData('launchBar.actions', (providers: LaunchBarActionProvider[]) => { + providers.push({ + name: 'numberSearchProvider', + getActions: async input => { + const { match, id, indexer } = matchInput(input, /^()(\d+)$/) + if (!match) { + return [] + } + const [aidJson, cvJson] = await Promise.all([ + await getJsonWithCredentials(`https://api.bilibili.com/x/web-interface/view?aid=${id}`), + await getJson(`https://api.bilibili.com/x/article/viewinfo?id=${id}`), + ]) + const { title: videoName } = lodash.get(aidJson, 'data', {}) + const { title: articleName } = lodash.get(cvJson, 'data', {}) + return [ + createLinkAction({ + name: videoName, + description: '视频跳转', + link: `https://www.bilibili.com/av${id}`, + indexer, + }), + createLinkAction({ + name: articleName, + description: '专栏跳转', + link: `https://www.bilibili.com/read/cv${id}`, + indexer, + }), + ] + }, + }) + }) + }, +}