diff --git a/src/components/SkipNoticeComponent.tsx b/src/components/SkipNoticeComponent.tsx index 41daac44..f7fa6022 100644 --- a/src/components/SkipNoticeComponent.tsx +++ b/src/components/SkipNoticeComponent.tsx @@ -645,18 +645,9 @@ class SkipNoticeComponent extends React.Component sendResponse(response)); return true; } } @@ -1689,7 +1692,7 @@ function clearSponsorTimes() { } //if skipNotice is null, it will not affect the UI -async function vote(type: number, UUID: SegmentUUID, category?: Category, skipNotice?: SkipNoticeComponent): Promise { +async function vote(type: number, UUID: SegmentUUID, category?: Category, skipNotice?: SkipNoticeComponent): Promise { if (skipNotice !== null && skipNotice !== undefined) { //add loading info 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 { @@ -1746,7 +1751,25 @@ async function voteAsync(type: number, UUID: SegmentUUID, category?: Category): type: type, UUID: UUID, 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); + }); }); } diff --git a/src/messageTypes.ts b/src/messageTypes.ts index 1b2949ea..8bee75f8 100644 --- a/src/messageTypes.ts +++ b/src/messageTypes.ts @@ -2,7 +2,7 @@ // Message and Response Types // -import { SponsorTime } from "./types"; +import { SegmentUUID, SponsorTime } from "./types"; interface BaseMessage { from?: string; @@ -29,7 +29,13 @@ interface IsInfoFoundMessage { 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 { found: boolean; @@ -59,7 +65,8 @@ export type MessageResponse = | GetChannelIDResponse | SponsorStartResponse | IsChannelWhitelistedResponse - | Record; + | Record + | VoteResponse; export interface VoteResponse { successType: number; diff --git a/src/popup.ts b/src/popup.ts index d84beda1..d6885759 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -589,21 +589,28 @@ async function runThePopup(messageListener?: MessageListener): Promise { //add loading info addVoteMessage(chrome.i18n.getMessage("Loading"), UUID); - //send the vote message to the tab - chrome.runtime.sendMessage({ - message: "submitVote", - type: type, - UUID: UUID - }, function (response) { - if (response != undefined) { - //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); + messageHandler.query({ + active: true, + currentWindow: true + }, tabs => { + messageHandler.sendMessage( + tabs[0].id, + { + message: "submitVote", + type: type, + UUID: UUID + }, function (response) { + if (response != undefined) { + //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); + } + } } - } + ); }); }