From cb773c4686edd5ea644fa1bd848718261fc0743b Mon Sep 17 00:00:00 2001 From: HanYaodong Date: Sat, 9 Mar 2024 23:28:23 +0800 Subject: [PATCH] Popup: Stop refresh animation when content script is not inserted --- src/content.ts | 11 +++++------ src/messageTypes.ts | 7 ++++++- src/popup.ts | 14 ++++++++++++-- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/content.ts b/src/content.ts index b8652aaa..8059da11 100644 --- a/src/content.ts +++ b/src/content.ts @@ -249,13 +249,12 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo case "refreshSegments": // update video on refresh if videoID invalid if (!getVideoID()) { - checkVideoIDChange().then(() => { - if (!getVideoID()) { - // if not on a video page, send a message to the popup to stop refresh animation - chrome.runtime.sendMessage({ message: "infoUpdated" }); - } - }); + checkVideoIDChange() } + + // if popup rescieves no response, or the videoID is invalid, + // it will assume the page is not a video page and stop the refresh animation + sendResponse({ hasVideo: getVideoID() != null }); // fetch segments sponsorsLookup(false); diff --git a/src/messageTypes.ts b/src/messageTypes.ts index 091aef3b..90c1da77 100644 --- a/src/messageTypes.ts +++ b/src/messageTypes.ts @@ -102,7 +102,8 @@ export type MessageResponse = | IsChannelWhitelistedResponse | Record // empty object response {} | VoteResponse - | ImportSegmentsResponse; + | ImportSegmentsResponse + | RefreshSegmentsResponse; export interface VoteResponse { successType: number; @@ -114,6 +115,10 @@ interface ImportSegmentsResponse { importedSegments: SponsorTime[]; } +export interface RefreshSegmentsResponse { + hasVideo: boolean; +} + export interface TimeUpdateMessage { message: "time"; time: number; diff --git a/src/popup.ts b/src/popup.ts index 00381e93..4bdaea96 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -14,6 +14,7 @@ import { Message, MessageResponse, PopupMessage, + RefreshSegmentsResponse, SponsorStartResponse, VoteResponse, } from "./messageTypes"; @@ -936,9 +937,18 @@ async function runThePopup(messageListener?: MessageListener): Promise { stopLoadingAnimation = AnimationUtils.applyLoadingAnimation(PageElements.refreshSegmentsButton, 0.3); } - function refreshSegments() { + async function refreshSegments() { startLoadingAnimation(); - sendTabMessage({ message: 'refreshSegments' }); + const response = await sendTabMessageAsync({ message: 'refreshSegments' }) as RefreshSegmentsResponse; + console.log(response) + + if (response == null || !response.hasVideo) { + if (stopLoadingAnimation != null) { + stopLoadingAnimation(); + stopLoadingAnimation = null; + } + displayNoVideo(); + } } function skipSegment(actionType: ActionType, UUID: SegmentUUID, element?: HTMLElement): void {