Hide segments when voting from the popup

This commit is contained in:
Ajay
2022-02-06 14:04:54 -05:00
parent 4a8e769596
commit 32fa9c3398
4 changed files with 59 additions and 31 deletions

View File

@@ -645,18 +645,9 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
this.addVoteButtonInfo(chrome.i18n.getMessage("voted")); this.addVoteButtonInfo(chrome.i18n.getMessage("voted"));
// Change the sponsor locally if (segment && category) {
if (segment) { // This is the segment inside the skip notice
if (type === 0) { this.segments[index].category = category;
segment.hidden = SponsorHideType.Downvoted;
} else if (category) {
segment.category = category; // This is the actual segment on the video page
this.segments[index].category = category; //this is the segment inside the skip notice.
} else if (type === 1) {
segment.hidden = SponsorHideType.Visible;
}
this.contentContainer().updatePreviewBar();
} }
} }

View File

@@ -203,6 +203,9 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
onMobileYouTube onMobileYouTube
})); }));
return true;
case "submitVote":
vote(request.type, request.UUID).then((response) => sendResponse(response));
return true; return true;
} }
} }
@@ -1689,7 +1692,7 @@ function clearSponsorTimes() {
} }
//if skipNotice is null, it will not affect the UI //if skipNotice is null, it will not affect the UI
async function vote(type: number, UUID: SegmentUUID, category?: Category, skipNotice?: SkipNoticeComponent): Promise<void> { async function vote(type: number, UUID: SegmentUUID, category?: Category, skipNotice?: SkipNoticeComponent): Promise<VoteResponse> {
if (skipNotice !== null && skipNotice !== undefined) { if (skipNotice !== null && skipNotice !== undefined) {
//add loading info //add loading info
skipNotice.addVoteButtonInfo.bind(skipNotice)(chrome.i18n.getMessage("Loading")) skipNotice.addVoteButtonInfo.bind(skipNotice)(chrome.i18n.getMessage("Loading"))
@@ -1717,6 +1720,8 @@ async function vote(type: number, UUID: SegmentUUID, category?: Category, skipNo
} }
} }
} }
return response;
} }
async function voteAsync(type: number, UUID: SegmentUUID, category?: Category): Promise<VoteResponse> { async function voteAsync(type: number, UUID: SegmentUUID, category?: Category): Promise<VoteResponse> {
@@ -1746,7 +1751,25 @@ async function voteAsync(type: number, UUID: SegmentUUID, category?: Category):
type: type, type: type,
UUID: UUID, UUID: UUID,
category: category category: category
}, resolve); }, (response) => {
if (response.successType === 1) {
// Change the sponsor locally
const segment = utils.getSponsorTimeFromUUID(sponsorTimes, UUID);
if (segment) {
if (type === 0) {
segment.hidden = SponsorHideType.Downvoted;
} else if (category) {
segment.category = category;
} else if (type === 1) {
segment.hidden = SponsorHideType.Visible;
}
updatePreviewBar();
}
}
resolve(response);
});
}); });
} }

View File

@@ -2,7 +2,7 @@
// Message and Response Types // Message and Response Types
// //
import { SponsorTime } from "./types"; import { SegmentUUID, SponsorTime } from "./types";
interface BaseMessage { interface BaseMessage {
from?: string; from?: string;
@@ -29,7 +29,13 @@ interface IsInfoFoundMessage {
updating: boolean; updating: boolean;
} }
export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage); interface submitVoteMessage {
message: "submitVote";
type: number;
UUID: SegmentUUID;
}
export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage | submitVoteMessage);
export interface IsInfoFoundMessageResponse { export interface IsInfoFoundMessageResponse {
found: boolean; found: boolean;
@@ -59,7 +65,8 @@ export type MessageResponse =
| GetChannelIDResponse | GetChannelIDResponse
| SponsorStartResponse | SponsorStartResponse
| IsChannelWhitelistedResponse | IsChannelWhitelistedResponse
| Record<string, never>; | Record<string, never>
| VoteResponse;
export interface VoteResponse { export interface VoteResponse {
successType: number; successType: number;

View File

@@ -589,21 +589,28 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
//add loading info //add loading info
addVoteMessage(chrome.i18n.getMessage("Loading"), UUID); addVoteMessage(chrome.i18n.getMessage("Loading"), UUID);
//send the vote message to the tab messageHandler.query({
chrome.runtime.sendMessage({ active: true,
message: "submitVote", currentWindow: true
type: type, }, tabs => {
UUID: UUID messageHandler.sendMessage(
}, function (response) { tabs[0].id,
if (response != undefined) { {
//see if it was a success or failure message: "submitVote",
if (response.successType == 1 || (response.successType == -1 && response.statusCode == 429)) { type: type,
//success (treat rate limits as a success) UUID: UUID
addVoteMessage(chrome.i18n.getMessage("voted"), UUID); }, function (response) {
} else if (response.successType == -1) { if (response != undefined) {
addVoteMessage(GenericUtils.getErrorMessage(response.statusCode, response.responseText), UUID); //see if it was a success or failure
if (response.successType == 1 || (response.successType == -1 && response.statusCode == 429)) {
//success (treat rate limits as a success)
addVoteMessage(chrome.i18n.getMessage("voted"), UUID);
} else if (response.successType == -1) {
addVoteMessage(GenericUtils.getErrorMessage(response.statusCode, response.responseText), UUID);
}
}
} }
} );
}); });
} }