Improve id search title

This commit is contained in:
the1812 2023-03-18 20:10:43 +08:00
parent fc2d7e3cff
commit b4f3c3bd07
2 changed files with 60 additions and 38 deletions

View File

@ -1,5 +1,6 @@
import { LaunchBarActionProvider, LaunchBarAction } from '@/components/launch-bar/launch-bar-action' import { LaunchBarActionProvider, LaunchBarAction } from '@/components/launch-bar/launch-bar-action'
import { PluginMetadata } from '../plugin' import { PluginMetadata } from '../plugin'
import { IdSearchProvider } from './types'
const getCopyItem = async (name: string, id: string, original: string) => { const getCopyItem = async (name: string, id: string, original: string) => {
const item: LaunchBarAction = { const item: LaunchBarAction = {
@ -17,39 +18,43 @@ const getCopyItem = async (name: string, id: string, original: string) => {
} }
return [item] return [item]
} }
const idMatches = [ const idMatches: IdSearchProvider[] = [
{ {
pattern: /^av([\d]+)$/i, pattern: /^av([\d]+)$/i,
name: (match: RegExpMatchArray) => `av${match[1]}`, getActions: async match => {
badge: 'av号跳转',
link: (match: RegExpMatchArray) => `https://www.bilibili.com/av${match[1]}`,
extend: async (match: RegExpMatchArray) => {
const { getJsonWithCredentials } = await import('@/core/ajax') const { getJsonWithCredentials } = await import('@/core/ajax')
const json = await getJsonWithCredentials( const json = await getJsonWithCredentials(
`https://api.bilibili.com/x/web-interface/view?aid=${match[1]}`, `https://api.bilibili.com/x/web-interface/view?aid=${match[1]}`,
) )
const bv = lodash.get(json, 'data.bvid', null) const data = lodash.get(json, 'data', {})
if (bv === null) { const { bvid, title } = data
return []
return {
name: title,
description: 'av号跳转',
indexer: `av${match[1]}`,
link: `https://www.bilibili.com/av${match[1]}`,
extraActions: bvid ? await getCopyItem('BV号', bvid, `av${match[1]}`) : [],
} }
return getCopyItem('BV号', bv, `av${match[1]}`)
}, },
}, },
{ {
pattern: /^bv([\da-zA-Z]+)$/i, pattern: /^bv([\da-zA-Z]+)$/i,
name: (match: RegExpMatchArray) => `BV${match[1]}`, getActions: async match => {
badge: 'BV号跳转',
link: (match: RegExpMatchArray) => `https://www.bilibili.com/BV${match[1]}`,
extend: async (match: RegExpMatchArray) => {
const { getJsonWithCredentials } = await import('@/core/ajax') const { getJsonWithCredentials } = await import('@/core/ajax')
const json = await getJsonWithCredentials( const json = await getJsonWithCredentials(
`https://api.bilibili.com/x/web-interface/view?bvid=${match[1]}`, `https://api.bilibili.com/x/web-interface/view?bvid=${match[1]}`,
) )
const av = lodash.get(json, 'data.aid', null) const data = lodash.get(json, 'data', {})
if (av === null) { const { aid, title } = data
return []
return {
name: title,
description: 'BV号跳转',
indexer: `BV${match[1]}`,
link: `https://www.bilibili.com/BV${match[1]}`,
extraActions: aid ? await getCopyItem('av号', `av${aid}`, `BV${match[1]}`) : [],
} }
return getCopyItem('av号', `av${av}`, `BV${match[1]}`)
}, },
}, },
] ]
@ -62,33 +67,31 @@ export const plugin: PluginMetadata = {
addData(LaunchBarActionProviders, (providers: LaunchBarActionProvider[]) => { addData(LaunchBarActionProviders, (providers: LaunchBarActionProvider[]) => {
providers.push({ providers.push({
name: 'IDSearchProvider', name: 'IDSearchProvider',
// getEnterAction: input => {
// for (const it of idMatches) {
// const match = input.match(it.pattern)
// if (match) {
// return () => window.open(it.link(match), '_blank')
// }
// }
// return null
// },
getActions: async input => { getActions: async input => {
const results: LaunchBarAction[] = [] const results: LaunchBarAction[] = []
for (const it of idMatches) { for (const it of idMatches) {
const match = input.match(it.pattern) const match = input.match(it.pattern)
if (match) { if (match) {
results.push({ const {
name: it.name(match), name,
icon: 'mdi-open-in-new', description = '',
description: it.badge, indexer,
action: () => { link,
window.open(it.link(match), '_blank') extraActions = [],
} = await it.getActions(match)
results.push(
{
name,
icon: 'mdi-open-in-new',
indexer,
description,
action: () => {
window.open(link, '_blank')
},
order: 0,
}, },
order: 0, ...extraActions,
}) )
if (it.extend) {
const actions = await it.extend(match)
results.push(...actions)
}
} }
} }
return results return results

View File

@ -0,0 +1,19 @@
import type { LaunchBarAction } from '@/components/launch-bar/launch-bar-action'
export interface IdSearchProvider {
/** 匹配的正则 */
pattern: RegExp
/** 匹配时运行此函数获取结果 */
getActions: (match: RegExpMatchArray) => Promise<{
/** 显示的选项名称 */
name: string
/** 选择此选项后打开的链接 */
link: string
/** 提供给搜索栏过滤的关键词 */
indexer: string
/** 显示的选项描述 */
description?: string
/** 额外提供一些自定义的 LaunchBarAction */
extraActions?: LaunchBarAction[]
}>
}