Skip to content

Commit

Permalink
Change video id to include cid and p
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Nov 21, 2024
1 parent 8c4adda commit ee710f7
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 69 deletions.
12 changes: 6 additions & 6 deletions src/components/DescriptionPortPillComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { ConfigProvider, Spin } from "antd";
import * as React from "react";
import { PortVideo } from "../render/DesciptionPortPill";
import { showMessage } from "../render/MessageNotice";
import { VideoID } from "../types";
import { VideoID, YoutubeID } from "../types";
import { AnimationUtils } from "../utils/animationUtils";
import { parseYoutubeID } from "../utils/parseVideoID";

export interface DescriptionPortPillProps {
bvID: VideoID;
ytbID: VideoID;
videoID: VideoID;
ytbID: YoutubeID;
showYtbVideoButton: boolean;

onSubmitPortVideo: (ytbID: VideoID) => Promise<PortVideo>;
onSubmitPortVideo: (ytbID: YoutubeID) => Promise<PortVideo>;
onVote(type: number): Promise<void>;
onRefresh(): Promise<void>;
}
Expand All @@ -20,8 +20,8 @@ export interface DescriptionPortPillState {
show: boolean;
showPreviewYtbVideo: boolean;
loading: boolean;
ytbVideoID: VideoID;
previewYtbID: VideoID;
ytbVideoID: YoutubeID;
previewYtbID: YoutubeID;
}

export class DescriptionPortPillComponent extends React.Component<DescriptionPortPillProps, DescriptionPortPillState> {
Expand Down
5 changes: 5 additions & 0 deletions src/config/configUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import Config from "../config";
import { VideoID } from "../types";

export function showDonationLink(): boolean {
return navigator.vendor !== "Apple Computer, Inc." && Config.config.showDonationLink;
}

export function getUnsubmittedSegmentKey(videoID: VideoID): string {
return videoID.bvid + videoID.cid;
}
30 changes: 16 additions & 14 deletions src/content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SkipNoticeComponent from "./components/SkipNoticeComponent";
import Config from "./config";
import { isSafari, Keybind, keybindEquals, keybindToString, StorageChangesObject } from "./config/config";
import { getUnsubmittedSegmentKey } from "./config/configUtils";
import PreviewBar, { PreviewBarSegment } from "./js-components/previewBar";
import { SkipButtonControlBar } from "./js-components/skipButtonControlBar";
import { Message, MessageResponse, VoteResponse } from "./messageTypes";
Expand Down Expand Up @@ -359,7 +360,7 @@ function messageListener(
}

if (addedSegments) {
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(getVideoID())] = sponsorTimesSubmitting;
Config.forceLocalUpdate("unsubmittedSegments");

updateSegmentSubmitting();
Expand Down Expand Up @@ -922,11 +923,12 @@ function isSegmentMarkedNearCurrentTime(currentTime: number, range: number = 5):
/**
* This makes sure the videoID is still correct and if the sponsorTime is included
*/
async function incorrectVideoCheck(videoID?: string, sponsorTime?: SponsorTime): Promise<boolean> {
async function incorrectVideoCheck(videoID?: VideoID, sponsorTime?: SponsorTime): Promise<boolean> {
const currentVideoID = await getBilibiliVideoID();
const recordedVideoID = videoID || getVideoID();
if (
currentVideoID !== recordedVideoID ||
// TODO: check cid
currentVideoID.bvid !== recordedVideoID.bvid ||
(sponsorTime &&
(!sponsorTimes ||
!sponsorTimes?.some(
Expand Down Expand Up @@ -1255,7 +1257,7 @@ async function sponsorsLookup(keepOldSubmissions = true, ignoreServerCache = fal
const hashParams = getHashParams();
if (hashParams.requiredSegment) extraRequestData.requiredSegment = hashParams.requiredSegment;

const hashPrefix = (await getVideoIDHash(getVideoID())).slice(0, 4) as VideoID & HashedValue;
const hashPrefix = (await getVideoIDHash(getVideoID())).slice(0, 4) as HashedValue;
const response = await getSegmentsByHash(hashPrefix, extraRequestData, ignoreServerCache);

// store last response status
Expand Down Expand Up @@ -1370,7 +1372,7 @@ function getEnabledActionTypes(forceFullVideo = false): ActionType[] {
}

async function lockedCategoriesLookup(): Promise<void> {
const hashPrefix = (await getHash(getVideoID(), 1)).slice(0, 4);
const hashPrefix = (await getVideoIDHash(getVideoID())).slice(0, 4);
const response = await asyncRequestToServer("GET", "/api/lockCategories/" + hashPrefix);

if (response.ok) {
Expand Down Expand Up @@ -2097,7 +2099,7 @@ function startOrEndTimingNewSegment() {
}

// Save the newly created segment
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(getVideoID())] = sponsorTimesSubmitting;
Config.forceLocalUpdate("unsubmittedSegments");

// Make sure they know if someone has already submitted something it while they were watching
Expand Down Expand Up @@ -2133,12 +2135,12 @@ function cancelCreatingSegment() {
if (sponsorTimesSubmitting.length > 1) {
// If there's more than one segment: remove last
sponsorTimesSubmitting.pop();
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(getVideoID())] = sponsorTimesSubmitting;
} else {
// Otherwise delete the video entry & close submission menu
resetSponsorSubmissionNotice();
sponsorTimesSubmitting = [];
delete Config.local.unsubmittedSegments[getVideoID()];
delete Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(getVideoID())];
}
Config.forceLocalUpdate("unsubmittedSegments");
}
Expand All @@ -2148,7 +2150,7 @@ function cancelCreatingSegment() {
}

function updateSponsorTimesSubmitting(getFromConfig = true) {
const segmentTimes = Config.local.unsubmittedSegments[getVideoID()];
const segmentTimes = Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(getVideoID())];

//see if this data should be saved in the sponsorTimesSubmitting variable
if (getFromConfig && segmentTimes != undefined) {
Expand Down Expand Up @@ -2233,13 +2235,13 @@ function closeInfoMenu() {
function clearSponsorTimes() {
const currentVideoID = getVideoID();

const sponsorTimes = Config.local.unsubmittedSegments[currentVideoID];
const sponsorTimes = Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(currentVideoID) ];

if (sponsorTimes != undefined && sponsorTimes.length > 0) {
resetSponsorSubmissionNotice();

//clear the sponsor times
delete Config.local.unsubmittedSegments[currentVideoID];
delete Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(currentVideoID)];
Config.forceLocalUpdate("unsubmittedSegments");

//clear sponsor times submitting
Expand Down Expand Up @@ -2439,7 +2441,7 @@ async function sendSubmitMessage(): Promise<boolean> {
}

//update sponsorTimes
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(getVideoID())] = sponsorTimesSubmitting;
Config.forceLocalUpdate("unsubmittedSegments");

// Check to see if any of the submissions are below the minimum duration set
Expand Down Expand Up @@ -2467,7 +2469,7 @@ async function sendSubmitMessage(): Promise<boolean> {
stopAnimation();

// Remove segments from storage since they've already been submitted
delete Config.local.unsubmittedSegments[getVideoID()];
delete Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(getVideoID())];
Config.forceLocalUpdate("unsubmittedSegments");

const newSegments = sponsorTimesSubmitting;
Expand Down Expand Up @@ -2740,7 +2742,7 @@ function checkForPreloadedSegment() {
}

if (pushed) {
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getUnsubmittedSegmentKey(getVideoID())] = sponsorTimesSubmitting;
Config.forceLocalUpdate("unsubmittedSegments");
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/document.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AID, BvID, CID, VideoID } from "./types";

Check failure on line 1 in src/document.ts

View workflow job for this annotation

GitHub Actions / Create artifacts

'AID' is defined but never used

Check failure on line 1 in src/document.ts

View workflow job for this annotation

GitHub Actions / Create artifacts

'BvID' is defined but never used

Check failure on line 1 in src/document.ts

View workflow job for this annotation

GitHub Actions / Create artifacts

'CID' is defined but never used

Check failure on line 1 in src/document.ts

View workflow job for this annotation

GitHub Actions / Create artifacts

'AID' is defined but never used

Check failure on line 1 in src/document.ts

View workflow job for this annotation

GitHub Actions / Create artifacts

'BvID' is defined but never used

Check failure on line 1 in src/document.ts

View workflow job for this annotation

GitHub Actions / Create artifacts

'CID' is defined but never used
import { InjectedScriptMessageSend, sourceId } from "./utils/injectedScriptMessageUtils";

const sendMessageToContent = (messageData: InjectedScriptMessageSend, payload): void => {
Expand All @@ -18,8 +19,14 @@ function windowMessageListener(message: MessageEvent) {
return;
}
if (data?.source === sourceId && data?.responseType) {
if (data.type === "getBvID") {
sendMessageToContent(data, window?.__INITIAL_STATE__?.bvid);
if (data.type === "getVideoID") {
const videoID = {
bvid: window?.__INITIAL_STATE__?.bvid,
aid: window?.__INITIAL_STATE__?.aid,
p: window?.__INITIAL_STATE__?.p,
cid: window?.__INITIAL_STATE__?.videoData?.cid,
} as VideoID;
sendMessageToContent(data, videoID);
} else if (data.type === "getFrameRate") {
const currentQuality = window?.__playinfo__?.data?.quality;
const frameRate = window?.__playinfo__?.data?.dash?.video.filter((v) => v.id === currentQuality)[0]
Expand Down
13 changes: 12 additions & 1 deletion src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ import SBObject from "./config";
declare global {
interface Window {
SB: typeof SBObject;
__INITIAL_STATE__?: { bvid: string; upInfo: { mid: number }; videoData: { desc: string } };
__INITIAL_STATE__?: {
aid: number;
bvid: string;
p: number;
upInfo: { mid: number };
videoData: {
aid: number;
bvid: string;
cid: number;
desc: string;
};
};
__playinfo__?: { data: { quality: number; dash: { video: { id: number; frameRate: number }[] } } };
}
}
6 changes: 3 additions & 3 deletions src/messageTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Message and Response Types
//

import { SegmentUUID, SponsorHideType, SponsorTime } from "./types";
import { SegmentUUID, SponsorHideType, SponsorTime, VideoID } from "./types";

interface BaseMessage {
from?: string;
Expand Down Expand Up @@ -90,7 +90,7 @@ export interface IsInfoFoundMessageResponse {
}

interface GetVideoIdResponse {
videoID: string;
videoID: VideoID;
}

export interface GetChannelIDResponse {
Expand Down Expand Up @@ -141,7 +141,7 @@ export type InfoUpdatedMessage = IsInfoFoundMessageResponse & {

export interface VideoChangedPopupMessage {
message: "videoChanged";
videoID: string;
videoID: VideoID;
whitelisted: boolean;
}

Expand Down
7 changes: 4 additions & 3 deletions src/popup/SubmitBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import * as React from "react";
import Config from "../config";
import { Message } from "../messageTypes";
import { SponsorTime, VideoID } from "../types";
import { getUnsubmittedSegmentKey } from "../config/configUtils";

interface SubmitBoxProps {
sendTabMessage: (data: Message, callback?) => void;
sendTabMessageAsync: (data: Message) => Promise<unknown>;
}

interface SubmitBoxState {
unsubmittedSegments: Record<VideoID, SponsorTime[]>;
unsubmittedSegments: Record<string, SponsorTime[]>;
currentVideoID: VideoID;

showSubmitBox: boolean;
Expand All @@ -27,11 +28,11 @@ class SubmitBox extends React.Component<SubmitBoxProps, SubmitBoxState> {
}

private sponsorTimes(): SponsorTime[] {
return this.state.unsubmittedSegments[this.state.currentVideoID] || [];
return this.state.unsubmittedSegments[getUnsubmittedSegmentKey(this.state.currentVideoID)] || [];
}

private isCreatingSegment(): boolean {
const segments = this.state.unsubmittedSegments[this.state.currentVideoID];
const segments = this.state.unsubmittedSegments[getUnsubmittedSegmentKey(this.state.currentVideoID)];
if (!segments) return false;
const lastSegment = segments[segments.length - 1];
return lastSegment && lastSegment?.segment?.length !== 2;
Expand Down
22 changes: 11 additions & 11 deletions src/render/DesciptionPortPill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Config from "../config";
import { getPageLoaded } from "../content";
import { getPortVideoByHash, updatePortedSegments } from "../requests/portVideo";
import { asyncRequestToServer } from "../requests/requests";
import { VideoID } from "../types";
import { VideoID, YoutubeID } from "../types";
import { waitFor } from "../utils/";
import { waitForElement } from "../utils/dom";
import { getVideoDescriptionFromWindow } from "../utils/injectedScriptMessageUtils";
Expand All @@ -16,16 +16,16 @@ import { showMessage } from "./MessageNotice";
const id = "bsbDescriptionContainer";

export interface PortVideo {
bvID: VideoID;
ytbID: VideoID;
videoID: VideoID;
ytbID: YoutubeID;
UUID: string;
votes: number;
locked: boolean;
}

export class DescriptionPortPill {
bvID: VideoID;
ytbID: VideoID;
videoID: VideoID;
ytbID: YoutubeID;
portUUID: string;
hasDescription: boolean;
sponsorsLookup: (keepOldSubmissions: boolean, ignoreServerCache: boolean, forceUpdatePreviewBar: boolean) => void;
Expand All @@ -43,10 +43,10 @@ export class DescriptionPortPill {
if (!Config.config.showPortVideoButton) {
return;
}
if (this.bvID === videoId) {
if (this.videoID === videoId) {
return;
}
this.bvID = videoId;
this.videoID = videoId;

this.cleanup();

Expand Down Expand Up @@ -96,7 +96,7 @@ export class DescriptionPortPill {
this.root.render(
<DescriptionPortPillComponent
ref={this.ref}
bvID={this.bvID}
videoID={this.videoID}
ytbID={this.ytbID}
showYtbVideoButton={Config.config.showPreviewYoutubeButton}
onSubmitPortVideo={(ytbID) => this.submitPortVideo(ytbID)}
Expand Down Expand Up @@ -169,7 +169,7 @@ export class DescriptionPortPill {
}
}

private async submitPortVideo(ytbID: VideoID): Promise<PortVideo> {
private async submitPortVideo(ytbID: YoutubeID): Promise<PortVideo> {
const response = await asyncRequestToServer("POST", "/api/portVideo", {
bvID: getVideoID(),
ytbID,
Expand Down Expand Up @@ -199,15 +199,15 @@ export class DescriptionPortPill {

const response = await asyncRequestToServer("POST", "/api/votePort", {
UUID: this.portUUID,
bvID: this.bvID,
bvID: this.videoID,
userID: Config.config.userID,
type: voteType,
});
if (!response?.ok) {
throw response?.responseText ? response.responseText : "投票失败!";
}

await this.getPortVideo(this.bvID, true);
await this.getPortVideo(this.videoID, true);
this.ref.current.setState({ ytbVideoID: this.ytbID, previewYtbID: this.ytbID });
}

Expand Down
4 changes: 2 additions & 2 deletions src/requests/bilibiliApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VideoID } from "../types";
import { AID, VideoID } from "../types";

export async function getBvIDfromAvIDBiliApi(avID: VideoID): Promise<VideoID | null> {
export async function getBvIDfromAvIDBiliApi(avID: AID): Promise<VideoID | null> {
try {
const response = await fetch(`https://api.bilibili.com/x/web-interface/view?aid=${avID}`);
if (!response.ok) {
Expand Down
Loading

0 comments on commit ee710f7

Please sign in to comment.