diff options
author | Ajay Ramachandran <[email protected]> | 2021-08-03 16:15:53 -0400 |
---|---|---|
committer | Ajay Ramachandran <[email protected]> | 2021-08-03 16:15:53 -0400 |
commit | e9e311454960684a2cfc119271f377ad5118fce5 (patch) | |
tree | 1ced835b9315009ce75af493bd9db70f53d39b8f | |
parent | a56bba06124fb5e30139e796646f01322a9292ab (diff) | |
download | SponsorBlock-e9e311454960684a2cfc119271f377ad5118fce5.tar.gz SponsorBlock-e9e311454960684a2cfc119271f377ad5118fce5.zip |
Update popup right away when refresh is pressed
-rw-r--r-- | src/content.ts | 12 | ||||
-rw-r--r-- | src/messageTypes.ts | 8 | ||||
-rw-r--r-- | src/popup.ts | 39 |
3 files changed, 35 insertions, 24 deletions
diff --git a/src/content.ts b/src/content.ts index e8746a9f..232d940f 100644 --- a/src/content.ts +++ b/src/content.ts @@ -123,7 +123,7 @@ const manualSkipPercentCount = 0.5; //get messages from the background script and the popup chrome.runtime.onMessage.addListener(messageListener); -function messageListener(request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void { +function messageListener(request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void | boolean { //messages from popup script switch(request.message){ case "update": @@ -144,7 +144,7 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo sponsorTimes: sponsorTimes }); - if (popupInitialised && document.getElementById("sponsorBlockPopupContainer") != null) { + if (!request.updating && popupInitialised && document.getElementById("sponsorBlockPopupContainer") != null) { //the popup should be closed now that another is opening closeInfoMenu(); } @@ -179,8 +179,12 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo submitSponsorTimes(); break; case "refreshSegments": - sponsorsLookup(sponsorVideoID, false).then(() => sendResponse({})); - break; + sponsorsLookup(sponsorVideoID, false).then(() => sendResponse({ + found: sponsorDataFound, + sponsorTimes: sponsorTimes + })); + + return true; } } diff --git a/src/messageTypes.ts b/src/messageTypes.ts index 9e0da461..49a5ad92 100644 --- a/src/messageTypes.ts +++ b/src/messageTypes.ts @@ -12,7 +12,6 @@ interface DefaultMessage { message: "update" | "sponsorStart" - | "isInfoFound" | "getVideoID" | "getChannelID" | "isChannelWhitelisted" @@ -25,7 +24,12 @@ interface BoolValueMessage { value: boolean; } -export type Message = BaseMessage & (DefaultMessage | BoolValueMessage); +interface IsInfoFoundMessage { + message: "isInfoFound"; + updating: boolean; +} + +export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage); interface IsInfoFoundMessageResponse { found: boolean; diff --git a/src/popup.ts b/src/popup.ts index b5ca8235..092b7db2 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -234,19 +234,15 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> { // Must be delayed so it only happens once loaded setTimeout(() => PageElements.sponsorblockPopup.classList.remove("preload"), 250); - messageHandler.query({ - active: true, - currentWindow: true - }, onTabs); + getSegmentsFromContentScript(false); - function onTabs(tabs) { + function onTabs(tabs, updating: boolean): void { messageHandler.sendMessage(tabs[0].id, {message: 'getVideoID'}, function(result) { - console.log(result) if (result !== undefined && result.videoID) { currentVideoID = result.videoID; creatingSegment = result.creatingSegment; - loadTabData(tabs); + loadTabData(tabs, updating); } else if (result === undefined && chrome.runtime.lastError) { //this isn't a YouTube video then, or at least the content script is not loaded displayNoVideo(); @@ -254,29 +250,30 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> { }); } - function loadTabData(tabs) { + function loadTabData(tabs, updating: boolean): void { if (!currentVideoID) { //this isn't a YouTube video then displayNoVideo(); return; } - //load video times for this video - const sponsorTimesStorage = Config.config.segmentTimes.get(currentVideoID); - if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) { - sponsorTimes = sponsorTimesStorage; - } - + sponsorTimes = Config.config.segmentTimes.get(currentVideoID) ?? []; updateSegmentEditingUI(); - //check if this video's sponsors are known messageHandler.sendMessage( tabs[0].id, - {message: 'isInfoFound'}, + {message: 'isInfoFound', updating}, infoFound ); } + function getSegmentsFromContentScript(updating: boolean): void { + messageHandler.query({ + active: true, + currentWindow: true + }, (tabs) => onTabs(tabs, updating)); + } + function infoFound(request: {found: boolean, sponsorTimes: SponsorTime[]}) { if(chrome.runtime.lastError) { //This page doesn't have the injected content script, or at least not yet @@ -369,7 +366,6 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> { //display the video times from the array at the top, in a different section function displayDownloadedSponsorTimes(request: {found: boolean, sponsorTimes: SponsorTime[]}) { if (request.sponsorTimes != undefined) { - // Sort list by start time const segmentTimes = request.sponsorTimes .sort((a, b) => a.segment[1] - b.segment[1]) @@ -377,6 +373,10 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> { //add them as buttons to the issue reporting container const container = document.getElementById("issueReporterTimeButtons"); + while (container.firstChild) { + container.removeChild(container.firstChild); + } + for (let i = 0; i < segmentTimes.length; i++) { const UUID = segmentTimes[i].UUID; @@ -695,7 +695,10 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> { messageHandler.sendMessage( tabs[0].id, {message: 'refreshSegments'}, - () => stopAnimation() + (response) => { + infoFound(response); + stopAnimation(); + } )} ); } |