Merge pull request #582 from FoseFx/fosefx-ts-msg

Types for messages
This commit is contained in:
Ajay Ramachandran
2020-12-19 14:50:53 -05:00
committed by GitHub
3 changed files with 71 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ import PreviewBar from "./js-components/previewBar";
import SkipNotice from "./render/SkipNotice"; import SkipNotice from "./render/SkipNotice";
import SkipNoticeComponent from "./components/SkipNoticeComponent"; import SkipNoticeComponent from "./components/SkipNoticeComponent";
import SubmissionNotice from "./render/SubmissionNotice"; import SubmissionNotice from "./render/SubmissionNotice";
import { Message, MessageResponse } from "./messageTypes";
// Hack to get the CSS loaded on permission-based sites (Invidious) // Hack to get the CSS loaded on permission-based sites (Invidious)
utils.wait(() => Config.config !== null, 5000, 10).then(addCSS); utils.wait(() => Config.config !== null, 5000, 10).then(addCSS);
@@ -115,7 +116,7 @@ const skipNoticeContentContainer: ContentContainer = () => ({
//get messages from the background script and the popup //get messages from the background script and the popup
chrome.runtime.onMessage.addListener(messageListener); chrome.runtime.onMessage.addListener(messageListener);
function messageListener(request: any, sender: unknown, sendResponse: (response: any) => void): void { function messageListener(request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void {
//messages from popup script //messages from popup script
switch(request.message){ switch(request.message){
case "update": case "update":
@@ -172,7 +173,6 @@ function messageListener(request: any, sender: unknown, sendResponse: (response:
break; break;
case "submitTimes": case "submitTimes":
submitSponsorTimes(); submitSponsorTimes();
break; break;
} }
} }
@@ -1222,7 +1222,7 @@ function updateSponsorTimesSubmitting(getFromConfig = true) {
} }
} }
async function changeStartSponsorButton(showStartSponsor, uploadButtonVisible) { async function changeStartSponsorButton(showStartSponsor: boolean, uploadButtonVisible: boolean): Promise<boolean> {
if(!sponsorVideoID) return false; if(!sponsorVideoID) return false;
//if it isn't visible, there is no data //if it isn't visible, there is no data
@@ -1423,7 +1423,7 @@ function dontShowNoticeAgain() {
closeAllSkipNotices(); closeAllSkipNotices();
} }
function sponsorMessageStarted(callback) { function sponsorMessageStarted(callback: (response: MessageResponse) => void) {
video = document.querySelector('video'); video = document.querySelector('video');
//send back current time //send back current time

63
src/messageTypes.ts Normal file
View File

@@ -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;

View File

@@ -2,11 +2,12 @@ import Config from "./config";
import Utils from "./utils"; import Utils from "./utils";
import { SponsorTime, SponsorHideType } from "./types"; import { SponsorTime, SponsorHideType } from "./types";
import { Message, MessageResponse } from "./messageTypes";
const utils = new Utils(); const utils = new Utils();
interface MessageListener { interface MessageListener {
(request: any, sender: unknown, callback: (response: any) => void): void; (request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void;
} }
class MessageHandler { class MessageHandler {
messageListener: MessageListener; messageListener: MessageListener;
@@ -15,7 +16,7 @@ class MessageHandler {
this.messageListener = messageListener; this.messageListener = messageListener;
} }
sendMessage(id: number, request, callback?) { sendMessage(id: number, request: Message, callback?) {
if (this.messageListener) { if (this.messageListener) {
this.messageListener(request, null, callback); this.messageListener(request, null, callback);
} else { } else {