Skip to content

Commit

Permalink
finished stream page (i think so!)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric2788 committed Jan 7, 2024
1 parent bc901cd commit 33f8385
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 110 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
"@react-hooks-library/core": "^0.5.1",
"autoprefixer": "^10.4.16",
"brotli-dec-wasm": "^2.1.0",
"danmaku": "^2.0.6",
"dexie": "^3.2.4",
"dexie-react-hooks": "^1.1.7",
"hash-wasm": "^4.11.0",
"hls.js": "^1.4.14",
"media-chrome": "^2.0.1",
"mpegts.js": "^1.7.3",
"n-danmaku": "^2.2.1",
"plasmo": "0.84.0",
"react": "18.2.0",
"react-contexify": "^6.0.0",
Expand Down
12 changes: 7 additions & 5 deletions src/api/bilibili.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GetInfoByRoomResponse, SpecAreaRankResponse, StreamUrlResponse, SuperChatList, V1Response, WbiAccInfoResponse, WebInterfaceNavResponse } from "~types/bilibili"
import { fetchSameCredentialBase, fetchSameCredentialV1, retryCatcher } from '~utils/fetch'
import { fetchSameCredentialBase, fetchSameCredentialV1, retryCatcher, sendRequest } from '~utils/fetch'

import type { NeptuneIsMyWaifu } from "~background/functions/getBLiveCachedData"
import func from '~utils/func'
Expand Down Expand Up @@ -60,7 +60,9 @@ export async function ensureLogin(): Promise<boolean> {

export async function ensureIsVtuber(info: StreamInfo): Promise<StreamInfo> {
// real vtuber identification
const vup = await retryCatcher(() => identifyVup(info.uid), 3)
const vup = await retryCatcher(() => identifyVup(info.uid), 3,
{ id: info.uid, name: info.username, locale: 'idk' } // if failed, always return is vtuber = true
)

// if not undefined
if (vup) {
Expand Down Expand Up @@ -97,7 +99,7 @@ export async function requestUserInfo(mid: string): Promise<WbiAccInfoResponse>
const wrid = await w_rid(mid, now)
const url = `https://api.bilibili.com/x/space/wbi/acc/info?platform=web&token=&web_location=1550101&wts=${now}&mid=${mid}&w_rid=${wrid}`

const res = await sendMessager('request', {
const res = await sendRequest<V1Response<WbiAccInfoResponse>>({
url,
options: {
method: "GET",
Expand All @@ -107,8 +109,8 @@ export async function requestUserInfo(mid: string): Promise<WbiAccInfoResponse>
'Origin': 'https://space.bilibili.com'
},
}
}) as V1Response<WbiAccInfoResponse>

})
if (res.code !== 0) throw new Error(`B站API请求错误: ${res.message}`)
return res.data
}
Expand Down
10 changes: 5 additions & 5 deletions src/api/vtb-moe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { VtbMoeDetailResponse, VtbMoeListResponse } from "~types/bilibili"
import { sendMessager } from '~utils/messaging'
import { sendRequest } from "~utils/fetch"

export async function getVupDetail(uid: string): Promise<VtbMoeDetailResponse | undefined> {
try {
return await sendMessager('request', {
return await sendRequest<VtbMoeDetailResponse>({
url: `https://api.vtbs.moe/v1/detail/${uid}`,
timeout: 5000
})
Expand All @@ -20,11 +20,11 @@ export type VupResponse = {
}

export async function listAllVupUids(): Promise<VupResponse[]> {
const req = await sendMessager('request', {
const res = await sendRequest<VtbMoeListResponse>({
url: 'https://vdb.vtbs.moe/json/list.json',
timeout: 5000
}) as VtbMoeListResponse
return req.vtbs.filter(v => v.type === 'vtuber').map(v => {
})
return res.vtbs.filter(v => v.type === 'vtuber').map(v => {
const acc = v.accounts.find(acc => acc.platform == 'bilibili')
return acc ? {
id: acc.id,
Expand Down
6 changes: 3 additions & 3 deletions src/background/forwards/danmaku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type ResponseBody = {
uname: string
text: string
color: string
pos: 'ltr' | 'rtl' | 'top' | 'bottom'
pos: 'scroll' | 'top' | 'bottom'
}


Expand All @@ -19,7 +19,7 @@ export type ForwardBody = {

const handler: ForwardHandler<ForwardBody, ResponseBody> = (req) => {

let pos: 'ltr' | 'rtl' | 'top' | 'bottom' = 'rtl'
let pos: 'scroll' | 'top' | 'bottom' = 'scroll'
switch (req.body.position) {
case 5:
pos = 'top'
Expand All @@ -34,7 +34,7 @@ const handler: ForwardHandler<ForwardBody, ResponseBody> = (req) => {
body: {
uname: req.body.uname,
text: req.body.text,
color: req.body.color.toString(16),
color: `#${req.body.color.toString(16)}`,
pos,
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/background/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ import type { PlasmoMessaging } from "@plasmohq/messaging"
import { isBackgroundScript } from '~utils/file'
import { sendMessager } from '~utils/messaging'

// follow from ./messages/*.ts












export type MessagingData = typeof messagers

interface MessageData<T extends object, R = any> {
Expand Down
7 changes: 7 additions & 0 deletions src/background/messages/fetch-developer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export type RequestBody = {}

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { data: developer, error } = await sendInternal('request', { url: developerLink })
if (error) {
await sendInternal('notify', {
title: '获取远端开发设定失败',
message: error,
})
return
}
await setSettingStorage('settings.developer', developer)
}

Expand Down
4 changes: 2 additions & 2 deletions src/background/messages/open-window.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { PlasmoMessaging } from "@plasmohq/messaging"

export type RequestBody = chrome.windows.CreateData & {
tab?: string
tab?: string,
}

const handler: PlasmoMessaging.MessageHandler<RequestBody, any> = async (req, res) => {
const { url, tab, type } = req.body
const { url, tab } = req.body
const result = await chrome.windows.create({
type: 'popup',
focused: true,
Expand Down
5 changes: 4 additions & 1 deletion src/contents/forwarder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ if (remove) remove()
remove = addBLiveMessageCommandListener('DANMU_MSG', (data) => {
const uname = data.info[2][1]
const text = data.info[1]
if (Array.isArray(text)) return
const color = data.info[0][3]
const position = data.info[0][1]
sendForward('pages', 'danmaku', { uname, text, color, position })
})
})


11 changes: 9 additions & 2 deletions src/contents/main/components/ButtonList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ import { sendMessager } from "~utils/messaging"

function ButtonList(): JSX.Element {

const { settings, info } = useContext(StreamInfoContext)
const streamInfo = useContext(StreamInfoContext)

if (!streamInfo) {
console.warn('BJF App is not ready.')
return <></>
}

const { settings, info } = streamInfo
const { "settings.display": displaySettings, "settings.features": featureSettings } = settings

const { createPopupWindow } = usePopupWindow(featureSettings.enabledPip, {
width: 700,
height: 450,
height: 450
})

const restart = () => sendForward('background', 'redirect', { target: 'content-script', command: 'command', body: { command: 'restart' }, queryInfo: { url: location.href } })
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export function usePopupWindow(enabledPip: boolean, options: PopupCreateInfo) {
iframe.src = url
iframe.style.width = '100%'
iframe.style.height = '100%'
iframe.height = options.height?.toString()
iframe.width = options.width?.toString()
iframe.allow = 'autoplay; fullscreen;'
iframe.frameBorder = '0'
iframe.allowFullscreen = true
iframe.mozallowfullscreen = true
iframe.msallowfullscreen = true
Expand Down
3 changes: 2 additions & 1 deletion src/players/flv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class FlvPlayer implements StreamPlayer {
cors: true,
withCredentials: true
}, {
autoCleanupSourceBuffer: false,
stashInitialSize: 1024 * 1024,
autoCleanupSourceBuffer: true,
headers: {
'Origin': 'https://live.bilibili.com',
'Referer': `https://live.bilibili.com/${this.room}`
Expand Down
6 changes: 3 additions & 3 deletions src/players/hls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class HlsPlayer implements StreamPlayer {

loadAndPlay(url: string, video: HTMLMediaElement): Promise<void> {
this.player = new Hls({
lowLatencyMode: true
lowLatencyMode: true,
})
return new Promise((res, rej) => {
this.player.on(Hls.Events.MEDIA_ATTACHED, () => {
this.player.once(Hls.Events.MEDIA_ATTACHED, () => {
console.log('video and hls.js are now bound together !')
res()
})

this.player.on(Hls.Events.MANIFEST_PARSED, (event, data) => {
this.player.once(Hls.Events.MANIFEST_PARSED, (event, data) => {
console.log('manifest loaded, found ' + data.levels.length + ' quality level', data)
})

Expand Down
2 changes: 1 addition & 1 deletion src/players/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function loadStream(roomId: string, urls: StreamUrls, video: HTMLVideoElem
return player
} catch (err: Error | any) {
console.error(`Player failed to load: `, err, ', from: ', url)
throw new Error(`Player failed to load: ${err.message}`)
continue
}
}
throw new Error('No player is supported')
Expand Down
Loading

0 comments on commit 33f8385

Please sign in to comment.