Skip to content

Commit

Permalink
Move submit port video to content
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Dec 11, 2024
1 parent c729c17 commit 448bcf5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
31 changes: 22 additions & 9 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { setMessageNotice, showMessage } from "./render/MessageNotice";
import { PlayerButton } from "./render/PlayerButton";
import SkipNotice from "./render/SkipNotice";
import SubmissionNotice from "./render/SubmissionNotice";
import { getPortVideoByHash, postPortVideoVote } from "./requests/portVideo";
import { getPortVideoByHash, postPortVideo, postPortVideoVote } from "./requests/portVideo";
import { asyncRequestToServer } from "./requests/requests";
import { getSegmentsByHash } from "./requests/segments";
import { getVideoLabel } from "./requests/videoLabels";
Expand Down Expand Up @@ -1237,32 +1237,45 @@ function setupCategoryPill() {

function setupDescriptionPill() {
if (!descriptionPill) {
descriptionPill = new DescriptionPortPill(getPortVideo, portVideoVote, sponsorsLookup);
descriptionPill = new DescriptionPortPill(getPortVideo, submitPortVideo, portVideoVote, sponsorsLookup);
}
descriptionPill.setupDecription(getVideoID());
}

async function getPortVideo(videoId: VideoID, bypassCache = false) {
const newPortVideo = await getPortVideoByHash(videoId, { bypassCache });
if (newPortVideo?.UUID === portVideo?.UUID) return;
async function updatePortVideoElements(newPortVideo: PortVideo) {
portVideo = newPortVideo;

// notify description pill
waitFor(() => descriptionPill).then(() => descriptionPill.setPortVideoData(portVideo));
waitFor(() => descriptionPill).then(() => descriptionPill.setPortVideoData(newPortVideo));

// notify popup of port video changes
chrome.runtime.sendMessage({
message: "infoUpdated",
found: sponsorDataFound,
status: lastResponseStatus,
sponsorTimes: sponsorTimes,
portVideo: portVideo,
portVideo: newPortVideo,
time: getVideo()?.currentTime ?? 0,
});
}

async function getPortVideo(videoId: VideoID, bypassCache = false) {
const newPortVideo = await getPortVideoByHash(videoId, { bypassCache });
if (newPortVideo?.UUID === portVideo?.UUID) return;
portVideo = newPortVideo;

updatePortVideoElements(portVideo);
}

async function submitPortVideo(ytbID: VideoID): Promise<PortVideo> {
const newPortVideo = await postPortVideo(getVideoID(), ytbID, getVideo()?.duration);
portVideo = newPortVideo;
updatePortVideoElements(portVideo);
this.sponsorsLookup(true, true, true);
return newPortVideo;
}

async function portVideoVote(UUID: string, bvID: VideoID, voteType: number) {
postPortVideoVote(UUID, bvID, voteType);
await postPortVideoVote(UUID, bvID, voteType);
await getPortVideo(this.bvID, true);
}

Expand Down
47 changes: 24 additions & 23 deletions src/render/DesciptionPortPill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import YouTubeLogoButton from "../components/YouTubeLogoButton";
import Config from "../config";
import { getPageLoaded } from "../content";
import { updatePortedSegments } from "../requests/portVideo";
import { asyncRequestToServer } from "../requests/requests";
import { PortVideo, VideoID } from "../types";
import { waitFor } from "../utils/";
import { waitForElement } from "../utils/dom";
import { getVideoDescriptionFromWindow } from "../utils/injectedScriptMessageUtils";
import { getVideo, getVideoID } from "../utils/video";
import { showMessage } from "./MessageNotice";

const id = "bsbDescriptionContainer";
Expand All @@ -21,6 +19,7 @@ export class DescriptionPortPill {
portUUID: string;
hasDescription: boolean;
getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void;
submitPortVideo: (ytbID: VideoID) => Promise<PortVideo>;
portVideoVote: (UUID: string, bvID: VideoID, voteType: number) => void;
sponsorsLookup: (keepOldSubmissions: boolean, ignoreServerCache: boolean, forceUpdatePreviewBar: boolean) => void;

Expand All @@ -31,10 +30,12 @@ export class DescriptionPortPill {

constructor(
getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void,
submitPortVideo: (ytbID: VideoID) => Promise<PortVideo>,
portVideoVote: (UUID: string, bvID: VideoID, voteType: number) => void,
sponsorsLookup: () => void
) {
this.getPortVideo = getPortVideo;
this.submitPortVideo = submitPortVideo;
this.portVideoVote = portVideoVote;
this.sponsorsLookup = sponsorsLookup;
}
Expand Down Expand Up @@ -167,27 +168,27 @@ 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 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) {
Expand Down
17 changes: 16 additions & 1 deletion src/requests/portVideo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Config from "../config";
import { VideoID } from "../types";
import { PortVideo, VideoID } from "../types";
import { getHash } from "../utils/hash";
import { FetchResponse } from "./background-request-proxy";
import { asyncRequestToServer } from "./requests";
Expand Down Expand Up @@ -50,6 +50,21 @@ export async function getPortVideoByHash(bvID: VideoID, options: RequestOptions
throw response;
}

export async function postPortVideo(bvID: VideoID, ytbID: VideoID, duration: number): Promise<PortVideoRecord> {
const response = await asyncRequestToServer("POST", "/api/portVideo", {
bvID: bvID,
ytbID,
biliDuration: duration,
userID: Config.config.userID,
userAgent: `${chrome.runtime.id}/v${chrome.runtime.getManifest().version}`,
});
if (response?.ok) {
return JSON.parse(response.responseText) as PortVideo;
} else {
throw response.responseText;
}
}

export async function postPortVideoVote(UUID: string, bvID: VideoID, voteType: number) {
const response = await asyncRequestToServer("POST", "/api/votePort", {
UUID: UUID,
Expand Down

0 comments on commit 448bcf5

Please sign in to comment.