Bilibili-Evolved/src/components/feeds/api/manager/index.ts
2022-08-28 14:27:51 +08:00

43 lines
1.1 KiB
TypeScript

import { getCookieValue } from '@/core/utils'
import { FeedsCardCallback } from '../types'
import { feedsCardCallbacks } from './base'
import { FeedsCardsManagerV1 } from './v1'
import { FeedsCardsManagerV2 } from './v2'
export * from './base'
export const isV2Feeds = () => {
const hasCookieValue = parseInt(getCookieValue('hit-dyn-v2')) > 0
if (!hasCookieValue) {
return false
}
return [
't.bilibili.com',
'space.bilibili.com',
].some(host => location.host === host)
}
export const feedsCardsManager = (() => {
const isV2 = isV2Feeds()
if (isV2) {
return new FeedsCardsManagerV2()
}
return new FeedsCardsManagerV1()
})()
/**
* 为每个动态卡片执行特定操作
* @param callback 回调函数
*/
export const forEachFeedsCard = async (callback: FeedsCardCallback) => {
const success = await feedsCardsManager.startWatching()
if (!success) {
console.error('feedsCardsManager.startWatching() failed')
return null
}
const { added } = callback
if (added) {
feedsCardsManager.cards.forEach(c => added(c))
}
feedsCardCallbacks.push({ added: none, removed: none, ...callback })
return feedsCardsManager
}