Skip to content

Commit c1e8abb

Browse files
feat(bilibili): add support for live streaming of matches (#9303)
* feat(bilibili): Add support for live streaming of matches * chore(bilibili): update iFrameRegExp to restrict to live.bilibili.com * refactor(bilibili): Replacement of deprecated methods with new ones --------- Co-authored-by: Bas van Zanten <me@bas950.com>
1 parent ee88c98 commit c1e8abb

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

websites/B/bilibili/iframe.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const iframe = new iFrame()
2+
3+
iframe.on('UpdateData', async () => {
4+
const title = document.querySelector('.smaller-title')?.getAttribute('title') ?? document.querySelector('.small-title')?.getAttribute('title')
5+
const roomOwner = document.querySelector('.room-owner-username')?.getAttribute('title')
6+
if (title === undefined || roomOwner === undefined) {
7+
return
8+
}
9+
iframe.send({
10+
details: title,
11+
state: roomOwner,
12+
})
13+
})

websites/B/bilibili/metadata.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"biligame.com"
2929
],
3030
"regExp": "([a-z0-9-]+[.])*(bilibili|biligame)[.]com[/]",
31-
"version": "2.5.5",
31+
"version": "2.5.6",
3232
"logo": "https://cdn.rcd.gg/PreMiD/websites/B/bilibili/assets/logo.png",
3333
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/B/bilibili/assets/thumbnail.jpg",
3434
"color": "#05acff",
@@ -37,6 +37,8 @@
3737
"video",
3838
"anime"
3939
],
40+
"iframe": true,
41+
"iFrameRegExp": "live[.]bilibili[.]com",
4042
"settings": [
4143
{
4244
"id": "privacy",

websites/B/bilibili/presence.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Assets } from 'premid'
1+
import { Assets, getTimestamps, getTimestampsFromMedia, timestampFromFormat } from 'premid'
22

33
const presence = new Presence({ clientId: '639591760791732224' })
44
const browsingTimestamp = Math.floor(Date.now() / 1000)
@@ -8,33 +8,40 @@ let uploader: HTMLElement | null,
88
uploaderName: string,
99
uploaderLink: string,
1010
title: HTMLElement | null,
11+
iFrameTitle: string,
12+
iFrameRoomOwnerName: string,
1113
videoPaused: boolean,
1214
currentTime: number,
1315
duration: number,
1416
timestamps: number[]
1517

18+
presence.on('iFrameData', (data: any) => {
19+
iFrameTitle = data.details
20+
iFrameRoomOwnerName = data.state
21+
})
22+
1623
presence.on('UpdateData', async () => {
1724
const presenceData: PresenceData = {
1825
largeImageKey: 'https://cdn.rcd.gg/PreMiD/websites/B/bilibili/assets/logo.png',
1926
}
2027
const privacy = await presence.getSetting<boolean>('privacy')
2128

22-
async function getTimestamps() {
29+
async function internalGetTimestamps() {
2330
let video = document.querySelector<HTMLVideoElement>('bpx-player-container')
2431
if (!video) {
2532
video = document.querySelector<HTMLVideoElement>('video')!
2633
videoPaused = video.paused
27-
timestamps = presence.getTimestampsfromMedia(video)
34+
timestamps = getTimestampsFromMedia(video)
2835
}
2936
else {
3037
videoPaused = document.querySelector('.bpx-state-paused') === null
31-
currentTime = presence.timestampFromFormat(
38+
currentTime = timestampFromFormat(
3239
document.querySelector('.bpx-player-ctrl-time-current')?.textContent ?? '',
3340
)
34-
duration = presence.timestampFromFormat(
41+
duration = timestampFromFormat(
3542
document.querySelector('.bpx-player-ctrl-time-duration')?.textContent ?? '',
3643
)
37-
timestamps = presence.getTimestamps(currentTime, duration)
44+
timestamps = getTimestamps(currentTime, duration)
3845
}
3946

4047
[presenceData.startTimestamp, presenceData.endTimestamp] = timestamps
@@ -53,7 +60,7 @@ presence.on('UpdateData', async () => {
5360
return
5461
}
5562

56-
getTimestamps()
63+
internalGetTimestamps()
5764

5865
if (document.querySelector('div.membersinfo-normal')) {
5966
uploader = document.querySelector('.staff-name')
@@ -90,7 +97,6 @@ presence.on('UpdateData', async () => {
9097
},
9198
]
9299
}
93-
94100
switch (document.location.hostname) {
95101
case 'www.bilibili.com': {
96102
switch (urlpath[1]) {
@@ -133,7 +139,7 @@ presence.on('UpdateData', async () => {
133139
setVideoStatus()
134140
break
135141
}
136-
getTimestamps()
142+
internalGetTimestamps()
137143
presenceData.details = document
138144
?.querySelector('.list-title')
139145
?.textContent
@@ -161,7 +167,7 @@ presence.on('UpdateData', async () => {
161167
presenceData.details = 'Watching an episode'
162168
break
163169
}
164-
getTimestamps()
170+
internalGetTimestamps()
165171
presenceData.details = document
166172
?.querySelector('.mediainfo_mediaTitle__Zyiqh')
167173
?.textContent
@@ -210,22 +216,34 @@ presence.on('UpdateData', async () => {
210216
break
211217
}
212218
case 'live.bilibili.com': {
213-
if (document.querySelector('.small-title') === null) {
214-
presenceData.details = document
219+
if (privacy) {
220+
presenceData.details = 'Watching a live stream'
221+
break
222+
}
223+
const presenceDetails = document.querySelector('.small-title') === null
224+
? presenceData.details = document
215225
.querySelector('.smaller-title')
216226
?.textContent
217227
?.trim()
218-
}
219-
else if (document.querySelector('.smaller-title') === null) {
220-
presenceData.details = document
228+
: presenceData.details = document
221229
.querySelector('.small-title')
222230
?.textContent
223231
?.trim()
224-
}
225-
presenceData.state = document
226-
.querySelector('.room-owner-username')
232+
const presenceState = document.querySelector('.room-owner-username')
227233
?.textContent
228234
?.trim()
235+
const isCompetition = presenceDetails === undefined && presenceState === undefined
236+
if (isCompetition === true) {
237+
if (iFrameTitle === undefined || iFrameRoomOwnerName === undefined) {
238+
return
239+
}
240+
presenceData.details = iFrameTitle
241+
presenceData.state = iFrameRoomOwnerName
242+
}
243+
else {
244+
presenceData.details = presenceDetails
245+
presenceData.state = presenceState
246+
}
229247
presenceData.buttons = [
230248
{
231249
label: 'Watch Stream',

0 commit comments

Comments
 (0)