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