Skip to content

Commit

Permalink
Get framerate from window
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Aug 14, 2024
1 parent 8627e5c commit c6a9638
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
checkIfNewVideoID,
checkVideoIDChange,
getChannelIDInfo,
getFrameRate,
getIsLivePremiere,
getVideo,
getVideoID,
Expand Down Expand Up @@ -2694,17 +2695,17 @@ function hotkeyListener(e: KeyboardEvent): void {
* Uses 1/30s as the frame rate, as it's the most common frame rate for videos
* @param key keydown event
*/
export function seekFrameByKeyPressListener(key) {
export async function seekFrameByKeyPressListener(key) {
const vid = getVideo();
const frameRate = await getFrameRate();
if (!vid.paused) return;

// TODO: better way to check framerate or next frame
if (keybindEquals(key, Config.config.nextFrameKeybind)) {
// next frame
vid.currentTime += 1 / 30;
vid.currentTime += 1 / frameRate;
} else if (keybindEquals(key, Config.config.previousFrameKeybind)) {
// previous frame
vid.currentTime -= 1 / 30;
vid.currentTime -= 1 / frameRate;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function windowMessageListener(message: MessageEvent) {
const data: InjectedScriptMessageSend = message.data;
if (data.type === "getBvID") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.bvid);
} else if (data.type === "getFrameRate") {
const currentQuality = window.__playinfo__.data.quality;
const frameRate = window.__playinfo__.data.dash.video.filter((v) => v.id === currentQuality)[0].frameRate;
sendMessageToContent(data, frameRate);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SBObject from "./config";
declare global {
interface Window {
SB: typeof SBObject;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
__INITIAL_STATE__?: any;
__INITIAL_STATE__?: { bvid: string };
__playinfo__?: { data: { quality: number; dash: { video: { id: number; frameRate: number }[] } } };
}
}
13 changes: 4 additions & 9 deletions src/utils/injectedScriptMessageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ export const sourceId = "biliSponsorBlock";
export interface InjectedScriptMessageBase {
source: string;
id: string;
type: InjectedScriptMessageTypeEnum;
type: string;
}

export interface InjectedScriptMessageSend extends InjectedScriptMessageBase {
responseType: InjectedScriptMessageTypeEnum;
responseType: string;
}

export interface InjectedScriptMessageRecieve extends InjectedScriptMessageBase {
data: unknown;
}

export interface InjectedScriptMessageType {
sendType: InjectedScriptMessageTypeEnum;
responseType: InjectedScriptMessageTypeEnum;
}

export enum InjectedScriptMessageTypeEnum {
getBvID = "getBvID",
returnBvID = "returnBvID",
sendType: string;
responseType: string;
}

export async function getPropertyFromWindow<T>(
Expand Down
6 changes: 3 additions & 3 deletions src/utils/parseVideoID.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getBvIDfromAvIDBiliApi } from "../requests/bilibiliApi";
import { BILI_DOMAINS } from "./constants";
import { getPropertyFromWindow, InjectedScriptMessageTypeEnum } from "./injectedScriptMessageUtils";
import { getPropertyFromWindow } from "./injectedScriptMessageUtils";
import { VideoID } from "./video";

export async function getBilibiliVideoID(url?: string): Promise<VideoID | null> {
Expand All @@ -20,8 +20,8 @@ export async function getBilibiliVideoID(url?: string): Promise<VideoID | null>
export function getBvIDFromWindow(timeout = 200): Promise<VideoID | null> {
return getPropertyFromWindow<VideoID>(
{
sendType: InjectedScriptMessageTypeEnum.getBvID,
responseType: InjectedScriptMessageTypeEnum.returnBvID,
sendType: "getBvID",
responseType: "returnBvID",
},
timeout
);
Expand Down
14 changes: 13 additions & 1 deletion src/utils/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
channelIDChange,
videoIDChange as contentVideoIDChange,
resetValues as resetContentValues,
videoElementChange
videoElementChange,
} from "../content";
import { newThumbnails } from "../thumbnail-utils/thumbnailManagement";
import { waitFor } from "./";
import { addCleanupListener, setupCleanupListener } from "./cleanup";
import { getElement, isVisible, waitForElement } from "./dom";
import { getPropertyFromWindow } from "./injectedScriptMessageUtils";
import { getBilibiliVideoID } from "./parseVideoID";
import { injectScript } from "./scriptInjector";

Expand Down Expand Up @@ -342,6 +343,17 @@ function addPageListeners(): void {
});
}

export async function getFrameRate() {
return await getPropertyFromWindow<number>({
sendType: "getFrameRate",
responseType: "returnFrameRate",
}).catch((e) => {
// fall back to 30 fps
console.log(e);
return 30;
});
}

let lastRefresh = 0;
export function getVideo(): HTMLVideoElement | null {
setupVideoMutationListener();
Expand Down

0 comments on commit c6a9638

Please sign in to comment.