Update popup when new segments are fetched

This commit is contained in:
mini-bomba
2022-10-08 18:34:20 +02:00
parent 48cfee57b7
commit 78e9f41854
4 changed files with 68 additions and 31 deletions

View File

@@ -2,13 +2,13 @@ import * as CompileConfig from "../config.json";
import Config from "./config"; import Config from "./config";
import { Registration } from "./types"; import { Registration } from "./types";
import Utils from "./utils";
import { GenericUtils } from "./utils/genericUtils";
// Make the config public for debugging purposes // Make the config public for debugging purposes
window.SB = Config; window.SB = Config;
import Utils from "./utils";
import { GenericUtils } from "./utils/genericUtils";
const utils = new Utils({ const utils = new Utils({
registerFirefoxContentScript, registerFirefoxContentScript,
unregisterFirefoxContentScript unregisterFirefoxContentScript
@@ -106,6 +106,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
return true; return true;
} }
case "time": case "time":
case "infoUpdated":
if (sender.tab) { if (sender.tab) {
popupPort[sender.tab.id]?.postMessage(request); popupPort[sender.tab.id]?.postMessage(request);
} }

View File

@@ -1004,6 +1004,14 @@ async function sponsorsLookup(keepOldSubmissions = true) {
?.sort((a, b) => a.segment[0] - b.segment[0]); ?.sort((a, b) => a.segment[0] - b.segment[0]);
if (!recievedSegments || !recievedSegments.length) { if (!recievedSegments || !recievedSegments.length) {
// return if no video found // return if no video found
chrome.runtime.sendMessage({
message: "infoUpdated",
found: false,
status: lastResponseStatus,
sponsorTimes: sponsorTimes,
time: video.currentTime,
onMobileYouTube
});
retryFetch(404); retryFetch(404);
return; return;
} }
@@ -1093,6 +1101,16 @@ async function sponsorsLookup(keepOldSubmissions = true) {
importExistingChapters(true); importExistingChapters(true);
// notify popup of segment changes
chrome.runtime.sendMessage({
message: "infoUpdated",
found: sponsorDataFound,
status: lastResponseStatus,
sponsorTimes: sponsorTimes,
time: video.currentTime,
onMobileYouTube
});
if (Config.config.isVip) { if (Config.config.isVip) {
lockedCategoriesLookup(); lockedCategoriesLookup();
} }
@@ -1138,8 +1156,8 @@ async function lockedCategoriesLookup(): Promise<void> {
} }
function retryFetch(errorCode: number): void { function retryFetch(errorCode: number): void {
if (!Config.config.refetchWhenNotFound) return;
sponsorDataFound = false; sponsorDataFound = false;
if (!Config.config.refetchWhenNotFound) return;
if (retryFetchTimeout) clearTimeout(retryFetchTimeout); if (retryFetchTimeout) clearTimeout(retryFetchTimeout);
if ((errorCode !== 404 && retryCount > 1) || (errorCode !== 404 && retryCount > 10)) { if ((errorCode !== 404 && retryCount > 1) || (errorCode !== 404 && retryCount > 10)) {

View File

@@ -120,4 +120,8 @@ export interface TimeUpdateMessage {
time: number; time: number;
} }
export type PopupMessage = TimeUpdateMessage; export type InfoUpdatedMessage = IsInfoFoundMessageResponse & {
message: "infoUpdated";
}
export type PopupMessage = TimeUpdateMessage | InfoUpdatedMessage;

View File

@@ -1,8 +1,21 @@
import Config from "./config"; import Config from "./config";
import Utils from "./utils"; import Utils from "./utils";
import { SponsorTime, SponsorHideType, ActionType, SegmentUUID, SponsorSourceType, StorageChangesObject } from "./types"; import {
import { Message, MessageResponse, IsInfoFoundMessageResponse, ImportSegmentsResponse, PopupMessage } from "./messageTypes"; ActionType,
SegmentUUID,
SponsorHideType,
SponsorSourceType,
SponsorTime,
StorageChangesObject,
} from "./types";
import {
ImportSegmentsResponse,
IsInfoFoundMessageResponse,
Message,
MessageResponse,
PopupMessage,
} from "./messageTypes";
import { showDonationLink } from "./utils/configUtils"; import { showDonationLink } from "./utils/configUtils";
import { AnimationUtils } from "./utils/animationUtils"; import { AnimationUtils } from "./utils/animationUtils";
import { GenericUtils } from "./utils/genericUtils"; import { GenericUtils } from "./utils/genericUtils";
@@ -11,6 +24,7 @@ import { localizeHtmlPage } from "./utils/pageUtils";
import { exportTimes } from "./utils/exporter"; import { exportTimes } from "./utils/exporter";
import GenericNotice from "./render/GenericNotice"; import GenericNotice from "./render/GenericNotice";
import { noRefreshFetchingChaptersAllowed } from "./utils/licenseKey"; import { noRefreshFetchingChaptersAllowed } from "./utils/licenseKey";
const utils = new Utils(); const utils = new Utils();
interface MessageListener { interface MessageListener {
@@ -427,13 +441,10 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
PageElements.loadingIndicator.style.display = "none"; PageElements.loadingIndicator.style.display = "none";
downloadedTimes = request.sponsorTimes ?? []; downloadedTimes = request.sponsorTimes ?? [];
displayDownloadedSponsorTimes(downloadedTimes, request.time);
if (request.found) { if (request.found) {
PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsorFound"); PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsorFound");
PageElements.issueReporterImportExport.classList.remove("hidden"); PageElements.issueReporterImportExport.classList.remove("hidden");
if (request.sponsorTimes) {
displayDownloadedSponsorTimes(request.sponsorTimes, request.time);
}
} else if (request.status == 404 || request.status == 200) { } else if (request.status == 404 || request.status == 200) {
PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsor404"); PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsor404");
PageElements.issueReporterImportExport.classList.remove("hidden"); PageElements.issueReporterImportExport.classList.remove("hidden");
@@ -1141,6 +1152,9 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
case "time": case "time":
displayDownloadedSponsorTimes(downloadedTimes, msg.time); displayDownloadedSponsorTimes(downloadedTimes, msg.time);
break; break;
case "infoUpdated":
infoFound(msg);
break;
} }
} }
} }