Skip to content

Commit

Permalink
Add number search (fix #677)
Browse files Browse the repository at this point in the history
  • Loading branch information
the1812 committed Apr 23, 2023
1 parent 0bb5355 commit 7a2a5ec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions registry/lib/plugins/launch-bar/number-search/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
在输入纯数字时, 提供以下选项:
- 跳转至相应的视频 (视为 av 号)
- 跳转至相应的专栏 (视为 cv 号)
42 changes: 42 additions & 0 deletions registry/lib/plugins/launch-bar/number-search/index.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
]
},
})
})
},
}

0 comments on commit 7a2a5ec

Please sign in to comment.