From 1c74e58b2655b705c316fb50522ae55e75ca387d Mon Sep 17 00:00:00 2001 From: magicdawn Date: Fri, 20 Sep 2024 22:17:22 +0800 Subject: [PATCH] feat: utilize unsafeWindow.player --- src/components/VideoCard/use/_pip-window.tsx | 1 + src/main/video-play-page.tsx | 27 +++++++------------- src/utility/bilibili/player.ts | 9 +++++++ 3 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 src/utility/bilibili/player.ts diff --git a/src/components/VideoCard/use/_pip-window.tsx b/src/components/VideoCard/use/_pip-window.tsx index da76ca18..5e2a3da9 100644 --- a/src/components/VideoCard/use/_pip-window.tsx +++ b/src/components/VideoCard/use/_pip-window.tsx @@ -155,6 +155,7 @@ function CloseThenOpenButton({ newHref, pipWindow }: { pipWindow: Window; newHre pipWindow.close() const u = new URL(newHref) u.searchParams.delete(QueryKey.PlayerScreenMode) + u.searchParams.delete(QueryKey.ForceAutoPlay) GM.openInTab(u.href, { active: true }) } diff --git a/src/main/video-play-page.tsx b/src/main/video-play-page.tsx index 9918f17e..4b097c40 100644 --- a/src/main/video-play-page.tsx +++ b/src/main/video-play-page.tsx @@ -11,6 +11,7 @@ import { hasDocumentPictureInPicture, openInPipOrPopup, } from '$components/VideoCard/use/useOpenRelated' +import { getBiliPlayer } from '$utility/bilibili/player' import { getBiliPlayerConfigAutoPlay } from '$utility/bilibili/player-config' import { onVideoChange } from '$utility/bilibili/video-page' import { Button } from 'antd' @@ -77,20 +78,14 @@ async function handleForceAutoPlay() { const isON = new URL(location.href).searchParams.get(QueryKey.ForceAutoPlay) === ForceAutoPlay.ON if (!isON) return - // make it pause - const toggle = () => - document - .querySelector('#bilibili-player [role="button"][aria-label="播放/暂停"]') - ?.click() - - const playing = () => - !!document.querySelectorAll( - '#bilibili-player .bpx-player-container:not(.bpx-state-paused)', - ).length + const playing = (): boolean => { + const player = getBiliPlayer() + return !!player && !player.isPaused() + } const timeoutAt = Date.now() + ms('30s') while (Date.now() <= timeoutAt && !playing()) { - toggle() + getBiliPlayer()?.play() await delay(1000) } debug('handleForceAutoPlay complete, playing = %s', playing()) @@ -98,13 +93,9 @@ async function handleForceAutoPlay() { function pausePlayingVideoAndOpenInPipWindow() { // make it pause - const currentPaused = !!document.querySelectorAll( - '#bilibili-player .bpx-player-container.bpx-state-paused', - ).length - if (!currentPaused) { - document - .querySelector('#bilibili-player [role="button"][aria-label="播放/暂停"]') - ?.click() + const player = getBiliPlayer() + if (player && !player.isPaused()) { + player.pause() } // open in pipwindow diff --git a/src/utility/bilibili/player.ts b/src/utility/bilibili/player.ts new file mode 100644 index 00000000..66513a73 --- /dev/null +++ b/src/utility/bilibili/player.ts @@ -0,0 +1,9 @@ +export type Player = { + play: () => void + pause: () => void + isPaused: () => boolean +} + +export function getBiliPlayer(): Player | undefined { + return (unsafeWindow as any).player +}