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
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;
}
}

View File

@@ -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;

View File

@@ -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();
}
)}
);
}