aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAjay Ramachandran <[email protected]>2024-03-18 20:59:19 -0400
committerGitHub <[email protected]>2024-03-18 20:59:19 -0400
commit2dd69d443b6452d88f07d010fac7213b896f2ea8 (patch)
treee892a9a9fcd66710aac4a2b862d69385ab90b86f
parent6732850b4186b74aae73fdce894d9e71699728dd (diff)
parenta1505bcf205c609e7ddd65b941562e26ceecd19f (diff)
downloadSponsorBlock-2dd69d443b6452d88f07d010fac7213b896f2ea8.tar.gz
SponsorBlock-2dd69d443b6452d88f07d010fac7213b896f2ea8.zip
Merge pull request #1983 from HanYaodong/dev
[Fix #1979] Stop Refresh Animation on Popup When Content Script is Not Injected
-rw-r--r--src/content.ts8
-rw-r--r--src/messageTypes.ts7
-rw-r--r--src/popup.ts61
3 files changed, 43 insertions, 33 deletions
diff --git a/src/content.ts b/src/content.ts
index f89fb369..a7b8c45b 100644
--- a/src/content.ts
+++ b/src/content.ts
@@ -259,11 +259,11 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
case "refreshSegments":
// update video on refresh if videoID invalid
if (!getVideoID()) {
- checkVideoIDChange().then(() => {
- // if still no video ID found, return an empty info to the popup
- if (!getVideoID()) 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 339bfafe..191166a0 100644
--- a/src/messageTypes.ts
+++ b/src/messageTypes.ts
@@ -103,7 +103,8 @@ export type MessageResponse =
| IsChannelWhitelistedResponse
| Record<string, never> // empty object response {}
| VoteResponse
- | ImportSegmentsResponse;
+ | ImportSegmentsResponse
+ | RefreshSegmentsResponse;
export interface VoteResponse {
successType: number;
@@ -115,6 +116,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 f954f168..2f3a0d7a 100644
--- a/src/popup.ts
+++ b/src/popup.ts
@@ -15,6 +15,7 @@ import {
Message,
MessageResponse,
PopupMessage,
+ RefreshSegmentsResponse,
SponsorStartResponse,
VoteResponse,
} from "./messageTypes";
@@ -459,39 +460,35 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
stopLoadingAnimation = null;
}
- if (chrome.runtime.lastError) {
+ if (chrome.runtime.lastError || request == undefined || request.found == undefined) {
//This page doesn't have the injected content script, or at least not yet
+ // Or if the request is empty, meaning the current page is not YouTube or a video page
displayNoVideo();
return;
}
- // if request has no field other than message, then the page currently being browsed is not YouTube
- if (request.found != undefined) {
- //remove loading text
- PageElements.mainControls.style.display = "block";
- if (request.onMobileYouTube) PageElements.mainControls.classList.add("hidden");
- PageElements.whitelistButton.classList.remove("hidden");
- PageElements.loadingIndicator.style.display = "none";
-
- downloadedTimes = request.sponsorTimes ?? [];
- displayDownloadedSponsorTimes(downloadedTimes, request.time);
- if (request.found) {
- PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsorFound");
- PageElements.issueReporterImportExport.classList.remove("hidden");
- } else if (request.status == 404 || request.status == 200) {
- PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsor404");
- PageElements.issueReporterImportExport.classList.remove("hidden");
+ //remove loading text
+ PageElements.mainControls.style.display = "block";
+ if (request.onMobileYouTube) PageElements.mainControls.classList.add("hidden");
+ PageElements.whitelistButton.classList.remove("hidden");
+ PageElements.loadingIndicator.style.display = "none";
+
+ downloadedTimes = request.sponsorTimes ?? [];
+ displayDownloadedSponsorTimes(downloadedTimes, request.time);
+ if (request.found) {
+ PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsorFound");
+ PageElements.issueReporterImportExport.classList.remove("hidden");
+ } else if (request.status == 404 || request.status == 200) {
+ PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsor404");
+ PageElements.issueReporterImportExport.classList.remove("hidden");
+ } else {
+ if (request.status) {
+ PageElements.videoFound.innerHTML = chrome.i18n.getMessage("connectionError") + request.status;
} else {
- if (request.status) {
- PageElements.videoFound.innerHTML = chrome.i18n.getMessage("connectionError") + request.status;
- } else {
- PageElements.videoFound.innerHTML = chrome.i18n.getMessage("segmentsStillLoading");
- }
-
- PageElements.issueReporterImportExport.classList.remove("hidden");
+ PageElements.videoFound.innerHTML = chrome.i18n.getMessage("segmentsStillLoading");
}
- } else {
- displayNoVideo();
+
+ PageElements.issueReporterImportExport.classList.remove("hidden");
}
//see if whitelist button should be swapped
@@ -982,9 +979,17 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
stopLoadingAnimation = AnimationUtils.applyLoadingAnimation(PageElements.refreshSegmentsButton, 0.3);
}
- function refreshSegments() {
+ async function refreshSegments() {
startLoadingAnimation();
- sendTabMessage({ message: 'refreshSegments' });
+ const response = await sendTabMessageAsync({ message: 'refreshSegments' }) as RefreshSegmentsResponse;
+
+ if (response == null || !response.hasVideo) {
+ if (stopLoadingAnimation != null) {
+ stopLoadingAnimation();
+ stopLoadingAnimation = null;
+ }
+ displayNoVideo();
+ }
}
function skipSegment(actionType: ActionType, UUID: SegmentUUID, element?: HTMLElement): void {