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

View File

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

View File

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