Update popup right away when refresh is pressed

This commit is contained in:
Ajay Ramachandran
2021-08-03 16:15:53 -04:00
parent a56bba0612
commit e9e3114549
3 changed files with 35 additions and 24 deletions

View File

@@ -123,7 +123,7 @@ const manualSkipPercentCount = 0.5;
//get messages from the background script and the popup //get messages from the background script and the popup
chrome.runtime.onMessage.addListener(messageListener); 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 //messages from popup script
switch(request.message){ switch(request.message){
case "update": case "update":
@@ -144,7 +144,7 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
sponsorTimes: sponsorTimes 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 //the popup should be closed now that another is opening
closeInfoMenu(); closeInfoMenu();
} }
@@ -179,8 +179,12 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
submitSponsorTimes(); submitSponsorTimes();
break; break;
case "refreshSegments": case "refreshSegments":
sponsorsLookup(sponsorVideoID, false).then(() => sendResponse({})); sponsorsLookup(sponsorVideoID, false).then(() => sendResponse({
break; found: sponsorDataFound,
sponsorTimes: sponsorTimes
}));
return true;
} }
} }

View File

@@ -12,7 +12,6 @@ interface DefaultMessage {
message: message:
"update" "update"
| "sponsorStart" | "sponsorStart"
| "isInfoFound"
| "getVideoID" | "getVideoID"
| "getChannelID" | "getChannelID"
| "isChannelWhitelisted" | "isChannelWhitelisted"
@@ -25,7 +24,12 @@ interface BoolValueMessage {
value: boolean; value: boolean;
} }
export type Message = BaseMessage & (DefaultMessage | BoolValueMessage); interface IsInfoFoundMessage {
message: "isInfoFound";
updating: boolean;
}
export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoFoundMessage);
interface IsInfoFoundMessageResponse { interface IsInfoFoundMessageResponse {
found: boolean; found: boolean;

View File

@@ -234,19 +234,15 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
// Must be delayed so it only happens once loaded // Must be delayed so it only happens once loaded
setTimeout(() => PageElements.sponsorblockPopup.classList.remove("preload"), 250); setTimeout(() => PageElements.sponsorblockPopup.classList.remove("preload"), 250);
messageHandler.query({ getSegmentsFromContentScript(false);
active: true,
currentWindow: true
}, onTabs);
function onTabs(tabs) { function onTabs(tabs, updating: boolean): void {
messageHandler.sendMessage(tabs[0].id, {message: 'getVideoID'}, function(result) { messageHandler.sendMessage(tabs[0].id, {message: 'getVideoID'}, function(result) {
console.log(result)
if (result !== undefined && result.videoID) { if (result !== undefined && result.videoID) {
currentVideoID = result.videoID; currentVideoID = result.videoID;
creatingSegment = result.creatingSegment; creatingSegment = result.creatingSegment;
loadTabData(tabs); loadTabData(tabs, updating);
} else if (result === undefined && chrome.runtime.lastError) { } else if (result === undefined && chrome.runtime.lastError) {
//this isn't a YouTube video then, or at least the content script is not loaded //this isn't a YouTube video then, or at least the content script is not loaded
displayNoVideo(); displayNoVideo();
@@ -254,29 +250,30 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
}); });
} }
function loadTabData(tabs) { function loadTabData(tabs, updating: boolean): void {
if (!currentVideoID) { if (!currentVideoID) {
//this isn't a YouTube video then //this isn't a YouTube video then
displayNoVideo(); displayNoVideo();
return; return;
} }
//load video times for this video sponsorTimes = Config.config.segmentTimes.get(currentVideoID) ?? [];
const sponsorTimesStorage = Config.config.segmentTimes.get(currentVideoID);
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
sponsorTimes = sponsorTimesStorage;
}
updateSegmentEditingUI(); updateSegmentEditingUI();
//check if this video's sponsors are known
messageHandler.sendMessage( messageHandler.sendMessage(
tabs[0].id, tabs[0].id,
{message: 'isInfoFound'}, {message: 'isInfoFound', updating},
infoFound infoFound
); );
} }
function getSegmentsFromContentScript(updating: boolean): void {
messageHandler.query({
active: true,
currentWindow: true
}, (tabs) => onTabs(tabs, updating));
}
function infoFound(request: {found: boolean, sponsorTimes: SponsorTime[]}) { function infoFound(request: {found: boolean, sponsorTimes: SponsorTime[]}) {
if(chrome.runtime.lastError) { if(chrome.runtime.lastError) {
//This page doesn't have the injected content script, or at least not yet //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 //display the video times from the array at the top, in a different section
function displayDownloadedSponsorTimes(request: {found: boolean, sponsorTimes: SponsorTime[]}) { function displayDownloadedSponsorTimes(request: {found: boolean, sponsorTimes: SponsorTime[]}) {
if (request.sponsorTimes != undefined) { if (request.sponsorTimes != undefined) {
// Sort list by start time // Sort list by start time
const segmentTimes = request.sponsorTimes const segmentTimes = request.sponsorTimes
.sort((a, b) => a.segment[1] - b.segment[1]) .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 //add them as buttons to the issue reporting container
const container = document.getElementById("issueReporterTimeButtons"); const container = document.getElementById("issueReporterTimeButtons");
while (container.firstChild) {
container.removeChild(container.firstChild);
}
for (let i = 0; i < segmentTimes.length; i++) { for (let i = 0; i < segmentTimes.length; i++) {
const UUID = segmentTimes[i].UUID; const UUID = segmentTimes[i].UUID;
@@ -695,7 +695,10 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
messageHandler.sendMessage( messageHandler.sendMessage(
tabs[0].id, tabs[0].id,
{message: 'refreshSegments'}, {message: 'refreshSegments'},
() => stopAnimation() (response) => {
infoFound(response);
stopAnimation();
}
)} )}
); );
} }