Add animation to refresh segments

Closes https://github.com/ajayyy/SponsorBlock/issues/839
This commit is contained in:
Ajay Ramachandran
2021-07-13 18:12:45 -04:00
parent 572fee265d
commit 76b78ef132
3 changed files with 94 additions and 77 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 { async function messageListener(request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): Promise<void> {
//messages from popup script //messages from popup script
switch(request.message){ switch(request.message){
case "update": case "update":
@@ -179,7 +179,8 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
submitSponsorTimes(); submitSponsorTimes();
break; break;
case "refreshSegments": case "refreshSegments":
sponsorsLookup(sponsorVideoID, false); await sponsorsLookup(sponsorVideoID, false);
sendResponse({});
break; break;
} }
} }
@@ -594,81 +595,81 @@ async function sponsorsLookup(id: string, keepOldSubmissions = true) {
// Check for hashPrefix setting // Check for hashPrefix setting
const hashPrefix = (await utils.getHash(id, 1)).substr(0, 4); const hashPrefix = (await utils.getHash(id, 1)).substr(0, 4);
utils.asyncRequestToServer('GET', "/api/skipSegments/" + hashPrefix, { const response = await utils.asyncRequestToServer('GET', "/api/skipSegments/" + hashPrefix, {
categories categories
}).then(async (response: FetchResponse) => {
if (response?.ok) {
const recievedSegments: SponsorTime[] = JSON.parse(response.responseText)
?.filter((video) => video.videoID === id)
?.map((video) => video.segments)[0];
if (!recievedSegments || !recievedSegments.length) {
// return if no video found
retryFetch();
return;
}
sponsorDataFound = true;
// Check if any old submissions should be kept
if (sponsorTimes !== null && keepOldSubmissions) {
for (let i = 0; i < sponsorTimes.length; i++) {
if (sponsorTimes[i].source === SponsorSourceType.Local) {
// This is a user submission, keep it
recievedSegments.push(sponsorTimes[i]);
}
}
}
const oldSegments = sponsorTimes || [];
sponsorTimes = recievedSegments;
// Hide all submissions smaller than the minimum duration
if (Config.config.minDuration !== 0) {
for (let i = 0; i < sponsorTimes.length; i++) {
if (sponsorTimes[i].segment[1] - sponsorTimes[i].segment[0] < Config.config.minDuration) {
sponsorTimes[i].hidden = SponsorHideType.MinimumDuration;
}
}
}
if (keepOldSubmissions) {
for (const segment of oldSegments) {
const otherSegment = sponsorTimes.find((other) => segment.UUID === other.UUID);
if (otherSegment) {
// If they downvoted it, or changed the category, keep it
otherSegment.hidden = segment.hidden;
otherSegment.category = segment.category;
}
}
}
startSkipScheduleCheckingForStartSponsors();
//update the preview bar
//leave the type blank for now until categories are added
if (lastPreviewBarUpdate == id || (lastPreviewBarUpdate == null && !isNaN(video.duration))) {
//set it now
//otherwise the listener can handle it
updatePreviewBar();
}
sponsorLookupRetries = 0;
} else if (response?.status === 404) {
retryFetch();
} else if (sponsorLookupRetries < 15 && !recheckStarted) {
recheckStarted = true;
//TODO lower when server becomes better (back to 1 second)
//some error occurred, try again in a second
setTimeout(() => {
if (sponsorVideoID && sponsorTimes?.length === 0) {
sponsorsLookup(sponsorVideoID);
}
}, 5000 + Math.random() * 15000 + 5000 * sponsorLookupRetries);
sponsorLookupRetries++;
}
}); });
if (response?.ok) {
const recievedSegments: SponsorTime[] = JSON.parse(response.responseText)
?.filter((video) => video.videoID === id)
?.map((video) => video.segments)[0];
if (!recievedSegments || !recievedSegments.length) {
// return if no video found
retryFetch();
return;
}
sponsorDataFound = true;
// Check if any old submissions should be kept
if (sponsorTimes !== null && keepOldSubmissions) {
for (let i = 0; i < sponsorTimes.length; i++) {
if (sponsorTimes[i].source === SponsorSourceType.Local) {
// This is a user submission, keep it
recievedSegments.push(sponsorTimes[i]);
}
}
}
const oldSegments = sponsorTimes || [];
sponsorTimes = recievedSegments;
// Hide all submissions smaller than the minimum duration
if (Config.config.minDuration !== 0) {
for (let i = 0; i < sponsorTimes.length; i++) {
if (sponsorTimes[i].segment[1] - sponsorTimes[i].segment[0] < Config.config.minDuration) {
sponsorTimes[i].hidden = SponsorHideType.MinimumDuration;
}
}
}
if (keepOldSubmissions) {
for (const segment of oldSegments) {
const otherSegment = sponsorTimes.find((other) => segment.UUID === other.UUID);
if (otherSegment) {
// If they downvoted it, or changed the category, keep it
otherSegment.hidden = segment.hidden;
otherSegment.category = segment.category;
}
}
}
startSkipScheduleCheckingForStartSponsors();
//update the preview bar
//leave the type blank for now until categories are added
if (lastPreviewBarUpdate == id || (lastPreviewBarUpdate == null && !isNaN(video.duration))) {
//set it now
//otherwise the listener can handle it
updatePreviewBar();
}
sponsorLookupRetries = 0;
} else if (response?.status === 404) {
retryFetch();
} else if (sponsorLookupRetries < 15 && !recheckStarted) {
recheckStarted = true;
//TODO lower when server becomes better (back to 1 second)
//some error occurred, try again in a second
setTimeout(() => {
if (sponsorVideoID && sponsorTimes?.length === 0) {
sponsorsLookup(sponsorVideoID);
}
}, 5000 + Math.random() * 15000 + 5000 * sponsorLookupRetries);
sponsorLookupRetries++;
}
} }
function retryFetch(): void { function retryFetch(): void {

View File

@@ -53,5 +53,6 @@ export type MessageResponse =
| GetVideoIdResponse | GetVideoIdResponse
| GetChannelIDResponse | GetChannelIDResponse
| SponsorStartResponse | SponsorStartResponse
| IsChannelWhitelistedResponse; | IsChannelWhitelistedResponse
| Record<string, never>;

View File

@@ -679,13 +679,28 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
} }
function refreshSegments() { function refreshSegments() {
PageElements.refreshSegmentsButton.style.animation = "rotate 0.3s 0s infinite";
messageHandler.query({ messageHandler.query({
active: true, active: true,
currentWindow: true currentWindow: true
}, tabs => { }, tabs => {
messageHandler.sendMessage( messageHandler.sendMessage(
tabs[0].id, tabs[0].id,
{message: 'refreshSegments'} {message: 'refreshSegments'},
() => {
// Make the animation finite
PageElements.refreshSegmentsButton.style.animation = "rotate 0.3s";
// When the animation is over, hide the button
const animationEndListener = () => {
PageElements.refreshSegmentsButton.style.animation = "none";
PageElements.refreshSegmentsButton.removeEventListener("animationend", animationEndListener);
};
PageElements.refreshSegmentsButton.addEventListener("animationend", animationEndListener);
}
)} )}
); );
} }