From cd4f5fc6679d1b1e7e762da29cd45ec8c7f14b7d Mon Sep 17 00:00:00 2001 From: Max Baumann Date: Tue, 15 Dec 2020 19:37:48 +0100 Subject: refactor(types): add strong types to messages --- src/content.ts | 10 ++++----- src/messageTypes.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/popup.ts | 7 +++--- src/types.ts | 6 ++--- 4 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 src/messageTypes.ts diff --git a/src/content.ts b/src/content.ts index cded9c45..a94032a9 100644 --- a/src/content.ts +++ b/src/content.ts @@ -12,6 +12,7 @@ import PreviewBar from "./js-components/previewBar"; import SkipNotice from "./render/SkipNotice"; import SkipNoticeComponent from "./components/SkipNoticeComponent"; import SubmissionNotice from "./render/SubmissionNotice"; +import { Message, MessageResponse } from "./messageTypes"; // Hack to get the CSS loaded on permission-based sites (Invidious) utils.wait(() => Config.config !== null, 5000, 10).then(addCSS); @@ -27,7 +28,7 @@ let sponsorVideoID: VideoID = null; // JSON video info let videoInfo: any = null; //the channel this video is about -let channelID; +let channelID: string; // Skips are scheduled to ensure precision. // Skips are rescheduled every seeking event. @@ -112,7 +113,7 @@ const skipNoticeContentContainer: ContentContainer = () => ({ //get messages from the background script and the popup chrome.runtime.onMessage.addListener(messageListener); -function messageListener(request: any, sender: any, sendResponse: (response: any) => void): void { +function messageListener(request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void { //messages from popup script switch(request.message){ case "update": @@ -169,7 +170,6 @@ function messageListener(request: any, sender: any, sendResponse: (response: any break; case "submitTimes": submitSponsorTimes(); - break; } } @@ -1209,7 +1209,7 @@ function updateSponsorTimesSubmitting(getFromConfig = true) { } } -async function changeStartSponsorButton(showStartSponsor, uploadButtonVisible) { +async function changeStartSponsorButton(showStartSponsor: boolean, uploadButtonVisible: boolean): Promise { if(!sponsorVideoID) return false; //if it isn't visible, there is no data @@ -1410,7 +1410,7 @@ function dontShowNoticeAgain() { closeAllSkipNotices(); } -function sponsorMessageStarted(callback) { +function sponsorMessageStarted(callback: (response: MessageResponse) => void) { video = document.querySelector('video'); //send back current time diff --git a/src/messageTypes.ts b/src/messageTypes.ts new file mode 100644 index 00000000..cd1914a8 --- /dev/null +++ b/src/messageTypes.ts @@ -0,0 +1,63 @@ +// +// Message and Response Types +// + +import { SponsorTime } from "./types"; + +interface BaseMessage { + from?: string; +} + +interface DefaultMessage { + message: + "update" + | "sponsorStart" + | "sponsorDataChanged" + | "isInfoFound" + | "getVideoID" + | "getChannelID" + | "isChannelWhitelisted" + | "submitTimes"; +} + +interface BoolValueMessage { + message: "whitelistChange"; + value: boolean; +} + +interface ChangeStartSponsorButtonMessage { + message: "changeStartSponsorButton"; + showStartSponsor: boolean; + uploadButtonVisible: boolean; +} + +export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | ChangeStartSponsorButtonMessage); + +interface IsInfoFoundMessageResponse { + found: boolean; + sponsorTimes: SponsorTime[]; +} + +interface GetVideoIdResponse { + videoID: string; +} + +interface GetChannelIDResponse { + channelID: string; +} + +interface SponsorStartResponse { + time: number; +} + +interface IsChannelWhitelistedResponse { + value: boolean; +} + +export type MessageResponse = + IsInfoFoundMessageResponse + | GetVideoIdResponse + | GetChannelIDResponse + | SponsorStartResponse + | IsChannelWhitelistedResponse; + diff --git a/src/popup.ts b/src/popup.ts index 1ccc7cae..cda3f140 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -2,11 +2,12 @@ import Config from "./config"; import Utils from "./utils"; import { SponsorTime, SponsorHideType } from "./types"; +import { Message, MessageResponse } from "./messageTypes"; const utils = new Utils(); interface MessageListener { - (request: any, sender: any, callback: (response: any) => void): void; -} + (request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void; +} class MessageHandler { messageListener: MessageListener; @@ -15,7 +16,7 @@ class MessageHandler { this.messageListener = messageListener; } - sendMessage(id: number, request, callback?) { + sendMessage(id: number, request: Message, callback?) { if (this.messageListener) { this.messageListener(request, null, callback); } else { diff --git a/src/types.ts b/src/types.ts index d7818e01..0a3be890 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,7 +3,7 @@ import SkipNoticeComponent from "./components/SkipNoticeComponent"; interface ContentContainer { (): { - vote: (type: any, UUID: any, category?: string, skipNotice?: SkipNoticeComponent) => void, + vote: (type: number, UUID: string, category?: string, skipNotice?: SkipNoticeComponent) => void, dontShowNoticeAgain: () => void, unskipSponsorTime: (segment: SponsorTime) => void, sponsorTimes: SponsorTime[], @@ -15,9 +15,9 @@ interface ContentContainer { onMobileYouTube: boolean, sponsorSubmissionNotice: SubmissionNotice, resetSponsorSubmissionNotice: () => void, - changeStartSponsorButton: (showStartSponsor: any, uploadButtonVisible: any) => Promise, + changeStartSponsorButton: (showStartSponsor: boolean, uploadButtonVisible: boolean) => Promise, previewTime: (time: number, unpause?: boolean) => void, - videoInfo: any, + videoInfo: VideoInfo, getRealCurrentTime: () => number } } -- cgit v1.2.3