Update popup when new segments are fetched

This commit is contained in:
mini-bomba
2022-10-08 18:34:20 +02:00
parent 48cfee57b7
commit 78e9f41854
4 changed files with 68 additions and 31 deletions

View File

@@ -2,13 +2,13 @@ import * as CompileConfig from "../config.json";
import Config from "./config";
import { Registration } from "./types";
import Utils from "./utils";
import { GenericUtils } from "./utils/genericUtils";
// Make the config public for debugging purposes
window.SB = Config;
import Utils from "./utils";
import { GenericUtils } from "./utils/genericUtils";
const utils = new Utils({
registerFirefoxContentScript,
unregisterFirefoxContentScript
@@ -24,7 +24,7 @@ if (utils.isFirefox()) {
utils.wait(() => Config.config !== null).then(function() {
if (Config.config.supportInvidious) utils.setupExtraSiteContentScripts();
});
}
}
function onTabUpdatedListener(tabId: number) {
chrome.tabs.sendMessage(tabId, {
@@ -77,17 +77,17 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
ok: response.ok
});
});
return true;
case "submitVote":
submitVote(request.type, request.UUID, request.category).then(callback);
//this allows the callback to be called later
return true;
case "registerContentScript":
case "registerContentScript":
registerFirefoxContentScript(request);
return false;
case "unregisterContentScript":
case "unregisterContentScript":
unregisterFirefoxContentScript(request.id)
return false;
case "tabs": {
@@ -106,6 +106,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
return true;
}
case "time":
case "infoUpdated":
if (sender.tab) {
popupPort[sender.tab.id]?.postMessage(request);
}
@@ -156,8 +157,8 @@ chrome.runtime.onInstalled.addListener(function () {
/**
* Only works on Firefox.
* Firefox requires that it be applied after every extension restart.
*
* @param {JSON} options
*
* @param {JSON} options
*/
function registerFirefoxContentScript(options: Registration) {
const oldRegistration = contentScriptRegistrations[options.id];
@@ -174,7 +175,7 @@ function registerFirefoxContentScript(options: Registration) {
/**
* Only works on Firefox.
* Firefox requires that this is handled by the background script
*
*
*/
function unregisterFirefoxContentScript(id: string) {
contentScriptRegistrations[id].unregister();
@@ -225,10 +226,10 @@ async function asyncRequestToServer(type: string, address: string, data = {}) {
/**
* Sends a request to the specified url
*
*
* @param type The request type "GET", "POST", etc.
* @param address The address to add to the SponsorBlock server address
* @param callback
* @param callback
*/
async function sendRequestToCustomServer(type: string, url: string, data = {}) {
// If GET, convert JSON to parameters
@@ -248,4 +249,4 @@ async function sendRequestToCustomServer(type: string, url: string, data = {}) {
});
return response;
}
}

View File

@@ -1004,6 +1004,14 @@ async function sponsorsLookup(keepOldSubmissions = true) {
?.sort((a, b) => a.segment[0] - b.segment[0]);
if (!recievedSegments || !recievedSegments.length) {
// return if no video found
chrome.runtime.sendMessage({
message: "infoUpdated",
found: false,
status: lastResponseStatus,
sponsorTimes: sponsorTimes,
time: video.currentTime,
onMobileYouTube
});
retryFetch(404);
return;
}
@@ -1093,6 +1101,16 @@ async function sponsorsLookup(keepOldSubmissions = true) {
importExistingChapters(true);
// notify popup of segment changes
chrome.runtime.sendMessage({
message: "infoUpdated",
found: sponsorDataFound,
status: lastResponseStatus,
sponsorTimes: sponsorTimes,
time: video.currentTime,
onMobileYouTube
});
if (Config.config.isVip) {
lockedCategoriesLookup();
}
@@ -1138,8 +1156,8 @@ async function lockedCategoriesLookup(): Promise<void> {
}
function retryFetch(errorCode: number): void {
if (!Config.config.refetchWhenNotFound) return;
sponsorDataFound = false;
if (!Config.config.refetchWhenNotFound) return;
if (retryFetchTimeout) clearTimeout(retryFetchTimeout);
if ((errorCode !== 404 && retryCount > 1) || (errorCode !== 404 && retryCount > 10)) {

View File

@@ -9,7 +9,7 @@ interface BaseMessage {
}
interface DefaultMessage {
message:
message:
"update"
| "sponsorStart"
| "getVideoID"
@@ -95,7 +95,7 @@ interface IsChannelWhitelistedResponse {
value: boolean;
}
export type MessageResponse =
export type MessageResponse =
IsInfoFoundMessageResponse
| GetVideoIdResponse
| GetChannelIDResponse
@@ -120,4 +120,8 @@ export interface TimeUpdateMessage {
time: number;
}
export type PopupMessage = TimeUpdateMessage;
export type InfoUpdatedMessage = IsInfoFoundMessageResponse & {
message: "infoUpdated";
}
export type PopupMessage = TimeUpdateMessage | InfoUpdatedMessage;

View File

@@ -1,8 +1,21 @@
import Config from "./config";
import Utils from "./utils";
import { SponsorTime, SponsorHideType, ActionType, SegmentUUID, SponsorSourceType, StorageChangesObject } from "./types";
import { Message, MessageResponse, IsInfoFoundMessageResponse, ImportSegmentsResponse, PopupMessage } from "./messageTypes";
import {
ActionType,
SegmentUUID,
SponsorHideType,
SponsorSourceType,
SponsorTime,
StorageChangesObject,
} from "./types";
import {
ImportSegmentsResponse,
IsInfoFoundMessageResponse,
Message,
MessageResponse,
PopupMessage,
} from "./messageTypes";
import { showDonationLink } from "./utils/configUtils";
import { AnimationUtils } from "./utils/animationUtils";
import { GenericUtils } from "./utils/genericUtils";
@@ -11,6 +24,7 @@ import { localizeHtmlPage } from "./utils/pageUtils";
import { exportTimes } from "./utils/exporter";
import GenericNotice from "./render/GenericNotice";
import { noRefreshFetchingChaptersAllowed } from "./utils/licenseKey";
const utils = new Utils();
interface MessageListener {
@@ -188,7 +202,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
}
PageElements.exportSegmentsButton.addEventListener("click", exportSegments);
PageElements.importSegmentsButton.addEventListener("click",
PageElements.importSegmentsButton.addEventListener("click",
() => PageElements.importSegmentsMenu.classList.toggle("hidden"));
PageElements.importSegmentsSubmit.addEventListener("click", importSegments);
@@ -260,7 +274,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
if (dontShowNotice != undefined && dontShowNotice) {
PageElements.showNoticeAgain.style.display = "unset";
}
const values = ["userName", "viewCount", "minutesSaved", "vip", "permissions"];
if (!Config.config.payments.freeAccess && !noRefreshFetchingChaptersAllowed()) values.push("freeChaptersAccess");
@@ -427,13 +441,10 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
PageElements.loadingIndicator.style.display = "none";
downloadedTimes = request.sponsorTimes ?? [];
displayDownloadedSponsorTimes(downloadedTimes, request.time);
if (request.found) {
PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsorFound");
PageElements.issueReporterImportExport.classList.remove("hidden");
if (request.sponsorTimes) {
displayDownloadedSponsorTimes(request.sponsorTimes, request.time);
}
} else if (request.status == 404 || request.status == 200) {
PageElements.videoFound.innerHTML = chrome.i18n.getMessage("sponsor404");
PageElements.issueReporterImportExport.classList.remove("hidden");
@@ -514,7 +525,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
PageElements.issueReporterTabs.classList.add("hidden");
currentSegmentTab = SegmentTab.Segments;
} else {
if (currentSegmentTab === SegmentTab.Segments
if (currentSegmentTab === SegmentTab.Segments
&& sponsorTimes.every((segment) => segment.actionType === ActionType.Chapter)) {
PageElements.issueReporterTabs.classList.add("hidden");
currentSegmentTab = SegmentTab.Chapters;
@@ -529,7 +540,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
if (currentSegmentTab === SegmentTab.Segments) {
return segment.actionType !== ActionType.Chapter;
} else if (currentSegmentTab === SegmentTab.Chapters) {
return segment.actionType === ActionType.Chapter
return segment.actionType === ActionType.Chapter
&& segment.source !== SponsorSourceType.YouTube;
} else {
return true;
@@ -546,7 +557,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
if (downloadedTimes.length > 0) {
PageElements.exportSegmentsButton.classList.remove("hidden");
} else {
} else {
PageElements.exportSegmentsButton.classList.add("hidden");
}
@@ -590,7 +601,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
if (downloadedTimes[i].actionType === ActionType.Full) {
segmentTimeFromToNode.innerText = chrome.i18n.getMessage("full");
} else {
segmentTimeFromToNode.innerText = GenericUtils.getFormattedTime(downloadedTimes[i].segment[0], true) +
segmentTimeFromToNode.innerText = GenericUtils.getFormattedTime(downloadedTimes[i].segment[0], true) +
(actionType !== ActionType.Poi
? " " + chrome.i18n.getMessage("to") + " " + GenericUtils.getFormattedTime(downloadedTimes[i].segment[1], true)
: "");
@@ -695,7 +706,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
skipButton.id = "sponsorTimesSkipButtonContainer" + UUID;
skipButton.className = "voteButton";
skipButton.src = chrome.runtime.getURL("icons/skip.svg");
skipButton.title = actionType === ActionType.Chapter ? chrome.i18n.getMessage("playChapter")
skipButton.title = actionType === ActionType.Chapter ? chrome.i18n.getMessage("playChapter")
: chrome.i18n.getMessage("skipSegment");
skipButton.addEventListener("click", () => skipSegment(actionType, UUID, skipButton));
votingButtons.addEventListener("dblclick", () => skipSegment(actionType, UUID));
@@ -1022,7 +1033,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
UUID: UUID
});
}
if (element) {
const stopAnimation = AnimationUtils.applyLoadingAnimation(element, 0.3);
stopAnimation();
@@ -1141,6 +1152,9 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
case "time":
displayDownloadedSponsorTimes(downloadedTimes, msg.time);
break;
case "infoUpdated":
infoFound(msg);
break;
}
}
}