Skip to content

Commit

Permalink
fix(webpage): revert auto restore progress from bilibili
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Feb 15, 2024
1 parent ec12563 commit 77f83db
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
31 changes: 25 additions & 6 deletions apps/app/src/lib/remote-player/hook/handler-register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ import {
serializeMediaStatePropValue,
} from "../type";

export function registerHandlers(plugin: MediaPlugin) {
const player = plugin.media;
const port = plugin.controller;
export interface MediaStateRef {
prevSeek: {
value: number;
time: number;
} | null;
}

export function registerHandlers(this: MediaPlugin) {
const player = this.media;
const port = this.controller;
const ref = this.stateRef;
mediaReadonlyStateProps.forEach((prop) => {
port.handle(`get${capitalize(prop)}`, () => ({
value: serializeMediaStatePropValue(player[prop]),
Expand All @@ -30,9 +38,19 @@ export function registerHandlers(plugin: MediaPlugin) {
port.handle(`get${capitalize(prop)}`, () => ({
value: serializeMediaStatePropValue(player[prop]),
}));
port.handle(`set${capitalize(prop)}`, (val) => {
(player as any)[prop] = val;
});
if (prop === "currentTime") {
port.handle(`set${capitalize(prop)}`, (val) => {
ref.prevSeek = {
value: player.currentTime,
time: Date.now(),
};
(player as any)[prop] = val;
});
} else {
port.handle(`set${capitalize(prop)}`, (val) => {
(player as any)[prop] = val;
});
}
});
mediaActionProps.forEach((prop) => {
port.handle(prop, async (...args) => ({
Expand Down Expand Up @@ -73,6 +91,7 @@ export function registerHandlers(plugin: MediaPlugin) {
transfer: [ab],
};
});
return ref;
}

async function streamToArrayBuffer(readableStream: ReadableStream<Uint8Array>) {
Expand Down
4 changes: 3 additions & 1 deletion apps/app/src/lib/remote-player/lib/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LifeCycle } from "@/lib/lifecycle";
import { TimeoutError } from "@/lib/message";
import { registerEvents } from "../hook/event-register";
import type { MediaStateRef } from "../hook/handler-register";
import { registerHandlers } from "../hook/handler-register";
import { handleReadyState } from "../hook/ready-state";
import { fluentTimeUpdate } from "../hook/time-update";
Expand All @@ -25,6 +26,7 @@ export default class MediaPlugin extends LifeCycle {
}

#media: HTMLMediaElement | null = null;
protected stateRef: MediaStateRef = { prevSeek: null };
findMedia(): Promise<HTMLMediaElement> {
return waitForSelector<HTMLMediaElement>("video, audio");
}
Expand Down Expand Up @@ -110,7 +112,7 @@ export default class MediaPlugin extends LifeCycle {
handleReadyState(this);
fluentTimeUpdate(this);
registerEvents(this);
registerHandlers(this);
registerHandlers.call(this);
this.controller.send(mountedEvent, void 0);
}
}
Expand Down
40 changes: 40 additions & 0 deletions apps/app/src/web/userscript/bilibili.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class BilibiliPlugin extends MediaPlugin {
JSON.stringify({ media: { autoplay: false } }),
);
await super.onload();
this.revertAutoSeek();
await this.untilMediaReady("canplay");
await Promise.all([this.toggleDanmaku(false), this.untilWebFullscreen()]);
}
Expand Down Expand Up @@ -103,6 +104,45 @@ export default class BilibiliPlugin extends MediaPlugin {
fsButton.click();
}

async revertAutoSeek() {
const plyaer = this.player;
const toastContainer = plyaer.querySelector<HTMLDivElement>(
".bpx-player-toast-auto",
);
if (!toastContainer) {
console.log("toast container not found");
return;
}

const handler = () => {
if (
this.stateRef.prevSeek &&
this.stateRef.prevSeek.time > Date.now() - 5e3
) {
// if the seek is recent (within 5 seconds)
this.media.currentTime = this.stateRef.prevSeek.value;
} else {
this.media.currentTime = 0;
}
};

const observer = new MutationObserver((mutations) => {
mutations
.find((m) => m.type === "childList" && m.addedNodes.length > 0)
?.addedNodes.forEach((node) => {
if (node.textContent?.includes("已为您定位至")) {
(node as HTMLDivElement).style.opacity = "0";
handler();
}
});
});
observer.observe(toastContainer, {
childList: true,
subtree: true,
});
this.register(() => observer.disconnect());
}

async untilWebFullscreen() {
const playerEl = this.player;
if (this.isWebFullscreen()) return;
Expand Down

0 comments on commit 77f83db

Please sign in to comment.