Skip to content

Commit

Permalink
feat: live page auto best quality #108
Browse files Browse the repository at this point in the history
  • Loading branch information
festoney8 committed Jul 20, 2024
1 parent 477fa40 commit 085f826
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/components/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,18 @@ export class CheckboxItem implements IItem {
if (['complete', 'interactive'].includes(document.readyState)) {
this.option.enableFunc()?.then().catch()
} else {
document.addEventListener('DOMContentLoaded', this.option.enableFunc)
document.addEventListener('DOMContentLoaded', () => {
this.option.enableFunc && this.option.enableFunc()?.then().catch()
})
}
break
case 'document-idle':
if (document.readyState === 'complete') {
this.option.enableFunc()?.then().catch()
} else {
document.addEventListener('load', this.option.enableFunc)
window.addEventListener('load', () => {
this.option.enableFunc && this.option.enableFunc()?.then().catch()
})
}
break
default:
Expand Down
22 changes: 22 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,27 @@ export declare global {
comment_next_version?: 'ELEMENTS' | 'DEFAULT'
}
}
EmbedPlayer?: {
instance?: {
getPlayerInfo: () => {
quality?: string
qualityCandidates?: {
qn?: string
desc?: string
}[]
}
switchQuality?: function
}
}
livePlayer?: {
getPlayerInfo: () => {
quality?: string
qualityCandidates?: {
qn?: string
desc?: string
}[]
}
switchQuality?: function
}
}
}
41 changes: 38 additions & 3 deletions src/rules/live.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { unsafeWindow } from '$'
import { Group } from '../components/group'
import { CheckboxItem } from '../components/item'
import { debugRules as debug } from '../utils/logger'
import { debugRules as debug, error } from '../utils/logger'
import { isPageLiveHome, isPageLiveRoom } from '../utils/pageType'
import fontFaceRegular from './styles/fontFaceRegular.scss?inline'

Expand Down Expand Up @@ -159,7 +160,7 @@ if (isPageLiveRoom()) {
enableFunc: async () => {
let cnt = 0
const id = setInterval(() => {
if (document.querySelector('.rendererRoot, #internationalHeader')) {
if (document.querySelector('.rendererRoot, #main.live-activity-full-main, #internationalHeader')) {
if (!location.href.includes('/blanc/')) {
window.location.href = location.href.replace(
'live.bilibili.com/',
Expand All @@ -174,6 +175,40 @@ if (isPageLiveRoom()) {
},
enableFuncRunAt: 'document-end',
}),
// 自动切换最高画质
new CheckboxItem({
itemID: 'auto-best-quality',
description: '自动切换最高画质 (实验功能)',
enableFunc: async () => {
const qualityFn = () => {
const player = unsafeWindow.EmbedPlayer?.instance || unsafeWindow.livePlayer
if (player) {
try {
const info = player?.getPlayerInfo()
const arr = player?.getPlayerInfo().qualityCandidates
if (info && arr && arr.length) {
let maxQn = 0
arr.forEach((v) => {
if (v.qn && parseInt(v.qn) > maxQn) {
maxQn = parseInt(v.qn)
}
})
if (maxQn && info.quality && maxQn > parseInt(info.quality)) {
player.switchQuality(`${maxQn}`)
}
}
} catch (err) {
error('auto-best-quality error', err)
}
}
}
setTimeout(qualityFn, 2000)
setTimeout(qualityFn, 4000)
setTimeout(qualityFn, 6000)
setTimeout(qualityFn, 8000)
},
enableFuncRunAt: 'document-idle',
}),
]
liveGroupList.push(new Group('live-basic', '直播页 基本功能', basicItems))

Expand Down Expand Up @@ -283,7 +318,7 @@ if (isPageLiveRoom()) {
new CheckboxItem({
itemID: 'live-page-head-web-player-awesome-pk-vm',
description: '隐藏 直播PK特效',
itemCSS: `#pk-vm, #awesome-pk-vm {display: none !important;}`,
itemCSS: `#pk-vm, #awesome-pk-vm, #universal-pk-vm {display: none !important;}`,
}),
// 隐藏 滚动礼物通告
new CheckboxItem({
Expand Down
2 changes: 1 addition & 1 deletion src/utils/pageType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const currPage = (): string => {
}
if (host === 'live.bilibili.com') {
// 匹配blanc页(赛事直播or活动直播用),用于对iframe内直播生效
if (pathname.match(/^\/(?:blanc\/)?\d+/)) {
if (pathname.match(/^\/(?:blanc\/)?\d+(#\/)?/)) {
return 'liveRoom'
}
// 匹配各种直播页iframe、直播活动, 不做处理
Expand Down

0 comments on commit 085f826

Please sign in to comment.