diff --git a/src/content.ts b/src/content.ts index 48cbfccf..8f066fc0 100644 --- a/src/content.ts +++ b/src/content.ts @@ -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 } from "./requests/portVideo"; +import { getPortVideoByHash, postPortVideoVote } from "./requests/portVideo"; import { asyncRequestToServer } from "./requests/requests"; import { getSegmentsByHash } from "./requests/segments"; import { getVideoLabel } from "./requests/videoLabels"; @@ -1237,7 +1237,7 @@ function setupCategoryPill() { function setupDescriptionPill() { if (!descriptionPill) { - descriptionPill = new DescriptionPortPill(getPortVideo, sponsorsLookup); + descriptionPill = new DescriptionPortPill(getPortVideo, portVideoVote, sponsorsLookup); } descriptionPill.setupDecription(getVideoID()); } @@ -1261,6 +1261,11 @@ async function getPortVideo(videoId: VideoID, bypassCache = false) { }); } +async function portVideoVote(UUID: string, bvID: VideoID, voteType: number) { + postPortVideoVote(UUID, bvID, voteType); + await getPortVideo(this.bvID, true); +} + async function sponsorsLookup(keepOldSubmissions = true, ignoreServerCache = false, forceUpdatePreviewBar = false) { if (lookupWaiting) return; //there is still no video here diff --git a/src/render/DesciptionPortPill.tsx b/src/render/DesciptionPortPill.tsx index af3cf5fc..1f3f8867 100644 --- a/src/render/DesciptionPortPill.tsx +++ b/src/render/DesciptionPortPill.tsx @@ -21,6 +21,7 @@ export class DescriptionPortPill { portUUID: string; hasDescription: boolean; getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void; + portVideoVote: (UUID: string, bvID: VideoID, voteType: number) => void; sponsorsLookup: (keepOldSubmissions: boolean, ignoreServerCache: boolean, forceUpdatePreviewBar: boolean) => void; inputContainer: HTMLElement; @@ -28,8 +29,13 @@ export class DescriptionPortPill { ref: React.RefObject; root: Root; - constructor(getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void, sponsorsLookup: () => void) { + constructor( + getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void, + portVideoVote: (UUID: string, bvID: VideoID, voteType: number) => void, + sponsorsLookup: () => void + ) { this.getPortVideo = getPortVideo; + this.portVideoVote = portVideoVote; this.sponsorsLookup = sponsorsLookup; } @@ -187,18 +193,7 @@ export class DescriptionPortPill { return; } - const response = await asyncRequestToServer("POST", "/api/votePort", { - UUID: this.portUUID, - bvID: this.bvID, - userID: Config.config.userID, - type: voteType, - }); - if (!response?.ok) { - throw response?.responseText ? response.responseText : "投票失败!"; - } - - await this.getPortVideo(this.bvID, true); - this.ref.current.setState({ ytbVideoID: this.ytbID, previewYtbID: this.ytbID }); + this.portVideoVote(this.portUUID, this.bvID, voteType); } private async updateSegments() { diff --git a/src/requests/portVideo.ts b/src/requests/portVideo.ts index f54b719a..ec1b70e7 100644 --- a/src/requests/portVideo.ts +++ b/src/requests/portVideo.ts @@ -1,3 +1,4 @@ +import Config from "../config"; import { VideoID } from "../types"; import { getHash } from "../utils/hash"; import { FetchResponse } from "./background-request-proxy"; @@ -49,6 +50,18 @@ export async function getPortVideoByHash(bvID: VideoID, options: RequestOptions throw response; } +export async function postPortVideoVote(UUID: string, bvID: VideoID, voteType: number) { + const response = await asyncRequestToServer("POST", "/api/votePort", { + UUID: UUID, + bvID: bvID, + userID: Config.config.userID, + type: voteType, + }); + if (!response?.ok) { + throw response?.responseText ? response.responseText : "投票失败!"; + } +} + export async function updatePortedSegments(bvID: VideoID): Promise { return asyncRequestToServer("POST", "/api/updatePortedSegments", { videoID: bvID }); }