diff --git a/src/components/item.ts b/src/components/item.ts index 927e5ca9..c4b30014 100644 --- a/src/components/item.ts +++ b/src/components/item.ts @@ -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: diff --git a/src/global.d.ts b/src/global.d.ts index 13b1dbbe..9ef1a51c 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -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 + } } } diff --git a/src/rules/live.ts b/src/rules/live.ts index 35e1475b..7acf84fa 100644 --- a/src/rules/live.ts +++ b/src/rules/live.ts @@ -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' @@ -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/', @@ -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)) @@ -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({ diff --git a/src/utils/pageType.ts b/src/utils/pageType.ts index 5ad657eb..69472419 100644 --- a/src/utils/pageType.ts +++ b/src/utils/pageType.ts @@ -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、直播活动, 不做处理