Skip to content

Commit

Permalink
Move port video update to content
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Dec 11, 2024
1 parent 704b9b5 commit 01a4325
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
19 changes: 17 additions & 2 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { setMessageNotice, showMessage } from "./render/MessageNotice";
import { PlayerButton } from "./render/PlayerButton";
import SkipNotice from "./render/SkipNotice";
import SubmissionNotice from "./render/SubmissionNotice";
import { getPortVideoByHash, postPortVideo, postPortVideoVote } from "./requests/portVideo";
import { FetchResponse } from "./requests/background-request-proxy";
import { getPortVideoByHash, postPortVideo, postPortVideoVote, updatePortedSegments } from "./requests/portVideo";
import { asyncRequestToServer } from "./requests/requests";
import { getSegmentsByHash } from "./requests/segments";
import { getVideoLabel } from "./requests/videoLabels";
Expand Down Expand Up @@ -1237,7 +1238,13 @@ function setupCategoryPill() {

function setupDescriptionPill() {
if (!descriptionPill) {
descriptionPill = new DescriptionPortPill(getPortVideo, submitPortVideo, portVideoVote, sponsorsLookup);
descriptionPill = new DescriptionPortPill(
getPortVideo,
submitPortVideo,
portVideoVote,
updateSegments,
sponsorsLookup
);
}
descriptionPill.setupDecription(getVideoID());
}
Expand Down Expand Up @@ -1279,6 +1286,14 @@ async function portVideoVote(UUID: string, bvID: VideoID, voteType: number) {
await getPortVideo(this.bvID, true);
}

async function updateSegments(): Promise<FetchResponse> {
const response = await updatePortedSegments(getVideoID());
if (response.ok) {
this.sponsorsLookup(true, true, true);
}
return response;
}

async function sponsorsLookup(keepOldSubmissions = true, ignoreServerCache = false, forceUpdatePreviewBar = false) {
if (lookupWaiting) return;
//there is still no video here
Expand Down
38 changes: 9 additions & 29 deletions src/render/DesciptionPortPill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DescriptionPortPillComponent } from "../components/DescriptionPortPillC
import YouTubeLogoButton from "../components/YouTubeLogoButton";
import Config from "../config";
import { getPageLoaded } from "../content";
import { updatePortedSegments } from "../requests/portVideo";
import { FetchResponse } from "../requests/background-request-proxy";
import { PortVideo, VideoID } from "../types";
import { waitFor } from "../utils/";
import { waitForElement } from "../utils/dom";
Expand All @@ -21,6 +21,7 @@ export class DescriptionPortPill {
getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void;
submitPortVideo: (ytbID: VideoID) => Promise<PortVideo>;
portVideoVote: (UUID: string, bvID: VideoID, voteType: number) => void;
updateSegments: () => Promise<FetchResponse>;
sponsorsLookup: (keepOldSubmissions: boolean, ignoreServerCache: boolean, forceUpdatePreviewBar: boolean) => void;

inputContainer: HTMLElement;
Expand All @@ -32,11 +33,13 @@ export class DescriptionPortPill {
getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void,
submitPortVideo: (ytbID: VideoID) => Promise<PortVideo>,
portVideoVote: (UUID: string, bvID: VideoID, voteType: number) => void,
updateSegments: () => Promise<FetchResponse>,
sponsorsLookup: () => void
) {
this.getPortVideo = getPortVideo;
this.submitPortVideo = submitPortVideo;
this.portVideoVote = portVideoVote;
this.updateSegments = updateSegments;
this.sponsorsLookup = sponsorsLookup;
}

Expand Down Expand Up @@ -111,7 +114,7 @@ export class DescriptionPortPill {
showYtbVideoButton={Config.config.showPreviewYoutubeButton}
onSubmitPortVideo={(ytbID) => this.submitPortVideo(ytbID)}
onVote={(type) => this.vote(type)}
onRefresh={() => this.updateSegments()}
onRefresh={() => this.updateSegmentHandler()}
/>
);

Expand Down Expand Up @@ -168,28 +171,6 @@ export class DescriptionPortPill {
this.portUUID = null;
}

// private async submitPortVideo(ytbID: VideoID): Promise<PortVideo> {
// const response = await asyncRequestToServer("POST", "/api/portVideo", {
// bvID: getVideoID(),
// ytbID,
// biliDuration: getVideo().duration,
// userID: Config.config.userID,
// userAgent: `${chrome.runtime.id}/v${chrome.runtime.getManifest().version}`,
// });
// if (response?.ok) {
// const newPortVideo = JSON.parse(response.responseText) as PortVideo;
// this.ytbID = ytbID;
// this.portUUID = newPortVideo.UUID;

// this.sponsorsLookup(true, true, true);

// return newPortVideo;
// } else {
// throw response.responseText;
// }
// return null;
// }

private async vote(voteType: number) {
if (!this.portUUID) {
console.error("No port video to vote on");
Expand All @@ -199,18 +180,17 @@ export class DescriptionPortPill {
this.portVideoVote(this.portUUID, this.bvID, voteType);
}

private async updateSegments() {
const response = await updatePortedSegments(this.bvID);
private async updateSegmentHandler() {
const response = await this.updateSegments();
if (!response?.ok) {
if (response.status === 429) {
showMessage(response.responseText, "info");
} else {
console.error(response.responseText);
showMessage(chrome.i18n.getMessage("refreshFailed") + response.responseText, "error");
return;
}
} else {
showMessage(chrome.i18n.getMessage("refreshSuccess"), "success");
}
this.sponsorsLookup(true, true, true);
showMessage(chrome.i18n.getMessage("refreshSuccess"), "success");
}
}

0 comments on commit 01a4325

Please sign in to comment.