Skip to content

Commit b7a574f

Browse files
committed
Clear segment list & show loading animation in popup on video change
also removed the creatingSegment variable - results in "Start/End Segment Now" correctly updating when using buttons on the controls panel instead also the "refreshSegments" message no longer sends a response, as we send an update manually now
1 parent c8cbd89 commit b7a574f

File tree

4 files changed

+57
-21
lines changed

4 files changed

+57
-21
lines changed

src/background.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
107107
}
108108
case "time":
109109
case "infoUpdated":
110+
case "videoChanged":
110111
if (sender.tab) {
111112
popupPort[sender.tab.id]?.postMessage(request);
112113
}

src/content.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
215215
case "getVideoID":
216216
sendResponse({
217217
videoID: sponsorVideoID,
218-
creatingSegment: isSegmentCreationInProgress(),
219218
});
220219

221220
break;
@@ -243,15 +242,9 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
243242
// update video on refresh if videoID invalid
244243
if (!sponsorVideoID) videoIDChange(getYouTubeVideoID(document));
245244
// fetch segments
246-
sponsorsLookup(false).then(() => sendResponse({
247-
found: sponsorDataFound,
248-
status: lastResponseStatus,
249-
sponsorTimes: sponsorTimes,
250-
time: video.currentTime,
251-
onMobileYouTube
252-
}));
245+
sponsorsLookup(false);
253246

254-
return true;
247+
break;
255248
case "unskip":
256249
unskipSponsorTime(sponsorTimes.find((segment) => segment.UUID === request.UUID), null, true);
257250
break;
@@ -438,6 +431,13 @@ async function videoIDChange(id: string): Promise<void> {
438431
}
439432
}
440433

434+
// Notify the popup about the video change
435+
chrome.runtime.sendMessage({
436+
message: "videoChanged",
437+
videoID: sponsorVideoID,
438+
whitelisted: channelWhitelisted
439+
});
440+
441441
sponsorsLookup();
442442

443443
// Make sure all player buttons are properly added

src/messageTypes.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,10 @@ export type InfoUpdatedMessage = IsInfoFoundMessageResponse & {
124124
message: "infoUpdated";
125125
}
126126

127-
export type PopupMessage = TimeUpdateMessage | InfoUpdatedMessage;
127+
export interface VideoChangedPopupMessage {
128+
message: "videoChanged";
129+
videoID: string;
130+
whitelisted: boolean;
131+
}
132+
133+
export type PopupMessage = TimeUpdateMessage | InfoUpdatedMessage | VideoChangedPopupMessage;

src/popup.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
8484
};
8585
type PageElements = { [key: string]: HTMLElement } & InputPageElements
8686

87-
/** If true, the content script is in the process of creating a new segment. */
88-
let creatingSegment = false;
87+
let stopLoadingAnimation = null;
8988

9089
//the start and end time pairs (2d)
9190
let sponsorTimes: SponsorTime[] = [];
@@ -393,7 +392,6 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
393392
messageHandler.sendMessage(tabs[0].id, { message: 'getVideoID' }, function (result) {
394393
if (result !== undefined && result.videoID) {
395394
currentVideoID = result.videoID;
396-
creatingSegment = result.creatingSegment;
397395

398396
loadTabData(tabs, updating);
399397
} else if (result === undefined && chrome.runtime.lastError) {
@@ -429,6 +427,12 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
429427
}
430428

431429
async function infoFound(request: IsInfoFoundMessageResponse) {
430+
// End any loading animation
431+
if (stopLoadingAnimation != null) {
432+
stopLoadingAnimation();
433+
stopLoadingAnimation = null;
434+
}
435+
432436
if (chrome.runtime.lastError) {
433437
//This page doesn't have the injected content script, or at least not yet
434438
displayNoVideo();
@@ -490,10 +494,8 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
490494
}
491495

492496
function startSponsorCallback(response: SponsorStartResponse) {
493-
creatingSegment = response.creatingSegment;
494-
495497
// Only update the segments after a segment was created
496-
if (!creatingSegment) {
498+
if (!response.creatingSegment) {
497499
sponsorTimes = Config.config.unsubmittedSegments[currentVideoID] || [];
498500
}
499501

@@ -728,9 +730,16 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
728730
PageElements.showNoticeAgain.style.display = "none";
729731
}
730732

733+
function isCreatingSegment(): boolean {
734+
const segments = Config.config.unsubmittedSegments[currentVideoID];
735+
if (!segments) return false;
736+
const lastSegment = segments[segments.length - 1];
737+
return lastSegment && lastSegment?.segment?.length !== 2;
738+
}
739+
731740
/** Updates any UI related to segment editing and submission according to the current state. */
732741
function updateSegmentEditingUI() {
733-
PageElements.sponsorStart.innerText = chrome.i18n.getMessage(creatingSegment ? "sponsorEnd" : "sponsorStart");
742+
PageElements.sponsorStart.innerText = chrome.i18n.getMessage(isCreatingSegment() ? "sponsorEnd" : "sponsorStart");
734743

735744
PageElements.submitTimes.style.display = sponsorTimes && sponsorTimes.length > 0 ? "unset" : "none";
736745
PageElements.submissionHint.style.display = sponsorTimes && sponsorTimes.length > 0 ? "unset" : "none";
@@ -929,11 +938,13 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
929938
});
930939
}
931940

932-
async function refreshSegments() {
933-
const stopAnimation = AnimationUtils.applyLoadingAnimation(PageElements.refreshSegmentsButton, 0.3);
941+
function startLoadingAnimation() {
942+
stopLoadingAnimation = AnimationUtils.applyLoadingAnimation(PageElements.refreshSegmentsButton, 0.3);
943+
}
934944

935-
infoFound(await sendTabMessageAsync({ message: 'refreshSegments' }) as IsInfoFoundMessageResponse)
936-
stopAnimation();
945+
function refreshSegments() {
946+
startLoadingAnimation();
947+
sendTabMessage({ message: 'refreshSegments' });
937948
}
938949

939950
function skipSegment(actionType: ActionType, UUID: SegmentUUID, element?: HTMLElement): void {
@@ -1058,6 +1069,24 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
10581069
case "infoUpdated":
10591070
infoFound(msg);
10601071
break;
1072+
case "videoChanged":
1073+
currentVideoID = msg.videoID
1074+
sponsorTimes = Config.config.unsubmittedSegments[currentVideoID] ?? [];
1075+
updateSegmentEditingUI();
1076+
1077+
if (msg.whitelisted) {
1078+
PageElements.whitelistChannel.style.display = "none";
1079+
PageElements.unwhitelistChannel.style.display = "unset";
1080+
PageElements.whitelistToggle.checked = true;
1081+
document.querySelectorAll('.SBWhitelistIcon')[0].classList.add("rotated");
1082+
}
1083+
1084+
// Clear segments list & start loading animation
1085+
// We'll get a ping once they're loaded
1086+
startLoadingAnimation();
1087+
PageElements.videoFound.innerHTML = chrome.i18n.getMessage("Loading");
1088+
displayDownloadedSponsorTimes([], 0);
1089+
break;
10611090
}
10621091
}
10631092
}

0 commit comments

Comments
 (0)