Skip to content

Commit

Permalink
feat: utilize unsafeWindow.player
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Sep 20, 2024
1 parent 2d7db1b commit 1c74e58
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/components/VideoCard/use/_pip-window.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}

Expand Down
27 changes: 9 additions & 18 deletions src/main/video-play-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -77,34 +78,24 @@ 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<HTMLElement>('#bilibili-player [role="button"][aria-label="播放/暂停"]')
?.click()

const playing = () =>
!!document.querySelectorAll<HTMLDivElement>(
'#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())
}

function pausePlayingVideoAndOpenInPipWindow() {
// make it pause
const currentPaused = !!document.querySelectorAll<HTMLDivElement>(
'#bilibili-player .bpx-player-container.bpx-state-paused',
).length
if (!currentPaused) {
document
.querySelector<HTMLElement>('#bilibili-player [role="button"][aria-label="播放/暂停"]')
?.click()
const player = getBiliPlayer()
if (player && !player.isPaused()) {
player.pause()
}

// open in pipwindow
Expand Down
9 changes: 9 additions & 0 deletions src/utility/bilibili/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type Player = {
play: () => void
pause: () => void
isPaused: () => boolean
}

export function getBiliPlayer(): Player | undefined {
return (unsafeWindow as any).player
}

0 comments on commit 1c74e58

Please sign in to comment.