Skip to content

Commit 78e9f41

Browse files
committed
Update popup when new segments are fetched
1 parent 48cfee5 commit 78e9f41

File tree

4 files changed

+68
-31
lines changed

4 files changed

+68
-31
lines changed

src/background.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import * as CompileConfig from "../config.json";
22

33
import Config from "./config";
44
import { Registration } from "./types";
5+
import Utils from "./utils";
6+
import { GenericUtils } from "./utils/genericUtils";
57

68
// Make the config public for debugging purposes
79

810
window.SB = Config;
911

10-
import Utils from "./utils";
11-
import { GenericUtils } from "./utils/genericUtils";
1212
const utils = new Utils({
1313
registerFirefoxContentScript,
1414
unregisterFirefoxContentScript
@@ -24,7 +24,7 @@ if (utils.isFirefox()) {
2424
utils.wait(() => Config.config !== null).then(function() {
2525
if (Config.config.supportInvidious) utils.setupExtraSiteContentScripts();
2626
});
27-
}
27+
}
2828

2929
function onTabUpdatedListener(tabId: number) {
3030
chrome.tabs.sendMessage(tabId, {
@@ -77,17 +77,17 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
7777
ok: response.ok
7878
});
7979
});
80-
80+
8181
return true;
8282
case "submitVote":
8383
submitVote(request.type, request.UUID, request.category).then(callback);
84-
84+
8585
//this allows the callback to be called later
8686
return true;
87-
case "registerContentScript":
87+
case "registerContentScript":
8888
registerFirefoxContentScript(request);
8989
return false;
90-
case "unregisterContentScript":
90+
case "unregisterContentScript":
9191
unregisterFirefoxContentScript(request.id)
9292
return false;
9393
case "tabs": {
@@ -106,6 +106,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
106106
return true;
107107
}
108108
case "time":
109+
case "infoUpdated":
109110
if (sender.tab) {
110111
popupPort[sender.tab.id]?.postMessage(request);
111112
}
@@ -156,8 +157,8 @@ chrome.runtime.onInstalled.addListener(function () {
156157
/**
157158
* Only works on Firefox.
158159
* Firefox requires that it be applied after every extension restart.
159-
*
160-
* @param {JSON} options
160+
*
161+
* @param {JSON} options
161162
*/
162163
function registerFirefoxContentScript(options: Registration) {
163164
const oldRegistration = contentScriptRegistrations[options.id];
@@ -174,7 +175,7 @@ function registerFirefoxContentScript(options: Registration) {
174175
/**
175176
* Only works on Firefox.
176177
* Firefox requires that this is handled by the background script
177-
*
178+
*
178179
*/
179180
function unregisterFirefoxContentScript(id: string) {
180181
contentScriptRegistrations[id].unregister();
@@ -225,10 +226,10 @@ async function asyncRequestToServer(type: string, address: string, data = {}) {
225226

226227
/**
227228
* Sends a request to the specified url
228-
*
229+
*
229230
* @param type The request type "GET", "POST", etc.
230231
* @param address The address to add to the SponsorBlock server address
231-
* @param callback
232+
* @param callback
232233
*/
233234
async function sendRequestToCustomServer(type: string, url: string, data = {}) {
234235
// If GET, convert JSON to parameters
@@ -248,4 +249,4 @@ async function sendRequestToCustomServer(type: string, url: string, data = {}) {
248249
});
249250

250251
return response;
251-
}
252+
}

src/content.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,14 @@ async function sponsorsLookup(keepOldSubmissions = true) {
10041004
?.sort((a, b) => a.segment[0] - b.segment[0]);
10051005
if (!recievedSegments || !recievedSegments.length) {
10061006
// return if no video found
1007+
chrome.runtime.sendMessage({
1008+
message: "infoUpdated",
1009+
found: false,
1010+
status: lastResponseStatus,
1011+
sponsorTimes: sponsorTimes,
1012+
time: video.currentTime,
1013+
onMobileYouTube
1014+
});
10071015
retryFetch(404);
10081016
return;
10091017
}
@@ -1093,6 +1101,16 @@ async function sponsorsLookup(keepOldSubmissions = true) {
10931101

10941102
importExistingChapters(true);
10951103

1104+
// notify popup of segment changes
1105+
chrome.runtime.sendMessage({
1106+
message: "infoUpdated",
1107+
found: sponsorDataFound,
1108+
status: lastResponseStatus,
1109+
sponsorTimes: sponsorTimes,
1110+
time: video.currentTime,
1111+
onMobileYouTube
1112+
});
1113+
10961114
if (Config.config.isVip) {
10971115
lockedCategoriesLookup();
10981116
}
@@ -1138,8 +1156,8 @@ async function lockedCategoriesLookup(): Promise<void> {
11381156
}
11391157

11401158
function retryFetch(errorCode: number): void {
1141-
if (!Config.config.refetchWhenNotFound) return;
11421159
sponsorDataFound = false;
1160+
if (!Config.config.refetchWhenNotFound) return;
11431161

11441162
if (retryFetchTimeout) clearTimeout(retryFetchTimeout);
11451163
if ((errorCode !== 404 && retryCount > 1) || (errorCode !== 404 && retryCount > 10)) {

src/messageTypes.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface BaseMessage {
99
}
1010

1111
interface DefaultMessage {
12-
message:
12+
message:
1313
"update"
1414
| "sponsorStart"
1515
| "getVideoID"
@@ -95,7 +95,7 @@ interface IsChannelWhitelistedResponse {
9595
value: boolean;
9696
}
9797

98-
export type MessageResponse =
98+
export type MessageResponse =
9999
IsInfoFoundMessageResponse
100100
| GetVideoIdResponse
101101
| GetChannelIDResponse
@@ -120,4 +120,8 @@ export interface TimeUpdateMessage {
120120
time: number;
121121
}
122122

123-
export type PopupMessage = TimeUpdateMessage;
123+
export type InfoUpdatedMessage = IsInfoFoundMessageResponse & {
124+
message: "infoUpdated";
125+
}
126+
127+
export type PopupMessage = TimeUpdateMessage | InfoUpdatedMessage;

src/popup.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import Config from "./config";
22

33
import Utils from "./utils";
4-
import { SponsorTime, SponsorHideType, ActionType, SegmentUUID, SponsorSourceType, StorageChangesObject } from "./types";
5-
import { Message, MessageResponse, IsInfoFoundMessageResponse, ImportSegmentsResponse, PopupMessage } from "./messageTypes";
4+
import {
5+
ActionType,
6+
SegmentUUID,
7+
SponsorHideType,
8+
SponsorSourceType,
9+
SponsorTime,
10+
StorageChangesObject,
11+
} from "./types";
12+
import {
13+
ImportSegmentsResponse,
14+
IsInfoFoundMessageResponse,
15+
Message,
16+
MessageResponse,
17+
PopupMessage,
18+
} from "./messageTypes";
619
import { showDonationLink } from "./utils/configUtils";
720
import { AnimationUtils } from "./utils/animationUtils";
821
import { GenericUtils } from "./utils/genericUtils";
@@ -11,6 +24,7 @@ import { localizeHtmlPage } from "./utils/pageUtils";
1124
import { exportTimes } from "./utils/exporter";
1225
import GenericNotice from "./render/GenericNotice";
1326
import { noRefreshFetchingChaptersAllowed } from "./utils/licenseKey";
27+
1428
const utils = new Utils();
1529

1630
interface MessageListener {
@@ -188,7 +202,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
188202
}
189203

190204
PageElements.exportSegmentsButton.addEventListener("click", exportSegments);
191-
PageElements.importSegmentsButton.addEventListener("click",
205+
PageElements.importSegmentsButton.addEventListener("click",
192206
() => PageElements.importSegmentsMenu.classList.toggle("hidden"));
193207
PageElements.importSegmentsSubmit.addEventListener("click", importSegments);
194208

@@ -260,7 +274,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
260274
if (dontShowNotice != undefined && dontShowNotice) {
261275
PageElements.showNoticeAgain.style.display = "unset";
262276
}
263-
277+
264278
const values = ["userName", "viewCount", "minutesSaved", "vip", "permissions"];
265279
if (!Config.config.payments.freeAccess && !noRefreshFetchingChaptersAllowed()) values.push("freeChaptersAccess");
266280

@@ -427,13 +441,10 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
427441
PageElements.loadingIndicator.style.display = "none";
428442

429443
downloadedTimes = request.sponsorTimes ?? [];
444+
displayDownloadedSponsorTimes(downloadedTimes, request.time);
430445
if (request.found) {
431446
PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsorFound");
432-
433447
PageElements.issueReporterImportExport.classList.remove("hidden");
434-
if (request.sponsorTimes) {
435-
displayDownloadedSponsorTimes(request.sponsorTimes, request.time);
436-
}
437448
} else if (request.status == 404 || request.status == 200) {
438449
PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsor404");
439450
PageElements.issueReporterImportExport.classList.remove("hidden");
@@ -514,7 +525,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
514525
PageElements.issueReporterTabs.classList.add("hidden");
515526
currentSegmentTab = SegmentTab.Segments;
516527
} else {
517-
if (currentSegmentTab === SegmentTab.Segments
528+
if (currentSegmentTab === SegmentTab.Segments
518529
&& sponsorTimes.every((segment) => segment.actionType === ActionType.Chapter)) {
519530
PageElements.issueReporterTabs.classList.add("hidden");
520531
currentSegmentTab = SegmentTab.Chapters;
@@ -529,7 +540,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
529540
if (currentSegmentTab === SegmentTab.Segments) {
530541
return segment.actionType !== ActionType.Chapter;
531542
} else if (currentSegmentTab === SegmentTab.Chapters) {
532-
return segment.actionType === ActionType.Chapter
543+
return segment.actionType === ActionType.Chapter
533544
&& segment.source !== SponsorSourceType.YouTube;
534545
} else {
535546
return true;
@@ -546,7 +557,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
546557

547558
if (downloadedTimes.length > 0) {
548559
PageElements.exportSegmentsButton.classList.remove("hidden");
549-
} else {
560+
} else {
550561
PageElements.exportSegmentsButton.classList.add("hidden");
551562
}
552563

@@ -590,7 +601,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
590601
if (downloadedTimes[i].actionType === ActionType.Full) {
591602
segmentTimeFromToNode.innerText = chrome.i18n.getMessage("full");
592603
} else {
593-
segmentTimeFromToNode.innerText = GenericUtils.getFormattedTime(downloadedTimes[i].segment[0], true) +
604+
segmentTimeFromToNode.innerText = GenericUtils.getFormattedTime(downloadedTimes[i].segment[0], true) +
594605
(actionType !== ActionType.Poi
595606
? " " + chrome.i18n.getMessage("to") + " " + GenericUtils.getFormattedTime(downloadedTimes[i].segment[1], true)
596607
: "");
@@ -695,7 +706,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
695706
skipButton.id = "sponsorTimesSkipButtonContainer" + UUID;
696707
skipButton.className = "voteButton";
697708
skipButton.src = chrome.runtime.getURL("icons/skip.svg");
698-
skipButton.title = actionType === ActionType.Chapter ? chrome.i18n.getMessage("playChapter")
709+
skipButton.title = actionType === ActionType.Chapter ? chrome.i18n.getMessage("playChapter")
699710
: chrome.i18n.getMessage("skipSegment");
700711
skipButton.addEventListener("click", () => skipSegment(actionType, UUID, skipButton));
701712
votingButtons.addEventListener("dblclick", () => skipSegment(actionType, UUID));
@@ -1022,7 +1033,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
10221033
UUID: UUID
10231034
});
10241035
}
1025-
1036+
10261037
if (element) {
10271038
const stopAnimation = AnimationUtils.applyLoadingAnimation(element, 0.3);
10281039
stopAnimation();
@@ -1141,6 +1152,9 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
11411152
case "time":
11421153
displayDownloadedSponsorTimes(downloadedTimes, msg.time);
11431154
break;
1155+
case "infoUpdated":
1156+
infoFound(msg);
1157+
break;
11441158
}
11451159
}
11461160
}

0 commit comments

Comments
 (0)