import * as React from "react"; import { YourWorkComponent } from "./YourWorkComponent"; // import { ToggleOptionComponent } from "./ToggleOptionComponent"; // import { FormattingOptionsComponent } from "./FormattingOptionsComponent"; import { isSafari } from "../../maze-utils/src/config"; import { showDonationLink } from "../utils/configUtils"; import Config, { generateDebugDetails } from "../config"; import { GetChannelIDResponse, IsInfoFoundMessageResponse, LogResponse, Message, MessageResponse, PopupMessage } from "../messageTypes"; import { AnimationUtils } from "../../maze-utils/src/animationUtils"; import { SegmentListComponent } from "./SegmentListComponent"; import { ActionType, SegmentUUID, SponsorSourceType, SponsorTime } from "../types"; import { SegmentSubmissionComponent } from "./SegmentSubmissionComponent"; import { copyToClipboardPopup } from "./popupUtils"; export enum LoadingStatus { Loading, SegmentsFound, NoSegmentsFound, ConnectionError, StillLoading, NoVideo } export interface LoadingData { status: LoadingStatus; code?: number; } let loadRetryCount = 0; export const PopupComponent = () => { const [status, setStatus] = React.useState({ status: LoadingStatus.Loading }); const [extensionEnabled, setExtensionEnabled] = React.useState(!Config.config!.disableSkipping); const [channelWhitelisted, setChannelWhitelisted] = React.useState(null); const [showForceChannelCheckWarning, setShowForceChannelCheckWarning] = React.useState(false); const [showNoticeButton, setShowNoticeButton] = React.useState(Config.config!.dontShowNotice); const [currentTime, setCurrentTime] = React.useState(0); const [segments, setSegments] = React.useState([]); const [loopedChapter, setLoopedChapter] = React.useState(null); const [videoID, setVideoID] = React.useState(null); React.useEffect(() => { loadSegments({ updating: false, setStatus, setChannelWhitelisted, setVideoID, setCurrentTime, setSegments, setLoopedChapter }); setupComPort({ setStatus, setChannelWhitelisted, setVideoID, setCurrentTime, setSegments, setLoopedChapter }); forwardClickEvents(sendMessage); }, []); return (
{ window !== window.top && } { Config.config!.testingServer &&
{ chrome.runtime.sendMessage({ "message": "openConfig", "hash": "advanced" }); }}> {chrome.i18n.getMessage("betaServerWarning")}
}

SponsorBlock

{getVideoStatusText(status)}

{/* Toggle Box */}
{/* github: mbledkowski/toggle-switch */} {channelWhitelisted !== null && ( )}
{ showForceChannelCheckWarning && { chrome.runtime.sendMessage({ "message": "openConfig", "hash": "behavior" }); }}> {chrome.i18n.getMessage("forceChannelCheckPopup")} } { !Config.config.cleanPopup && } {/* Your Work box */} { !Config.config.cleanPopup && } {/* Footer */} { !Config.config.cleanPopup && } { showNoticeButton && }
); }; function getVideoStatusText(status: LoadingData): string { switch (status.status) { case LoadingStatus.Loading: return chrome.i18n.getMessage("Loading"); case LoadingStatus.SegmentsFound: return chrome.i18n.getMessage("sponsorFound"); case LoadingStatus.NoSegmentsFound: return chrome.i18n.getMessage("sponsor404"); case LoadingStatus.ConnectionError: return chrome.i18n.getMessage("connectionError") + status.code; case LoadingStatus.StillLoading: return chrome.i18n.getMessage("segmentsStillLoading"); case LoadingStatus.NoVideo: return chrome.i18n.getMessage("noVideoID"); } } interface SegmentsLoadedProps { setStatus: (status: LoadingData) => void; setChannelWhitelisted: (whitelisted: boolean | null) => void; setVideoID: (videoID: string | null) => void; setCurrentTime: (time: number) => void; setSegments: (segments: SponsorTime[]) => void; setLoopedChapter: (loopedChapter: SegmentUUID | null) => void; } interface LoadSegmentsProps extends SegmentsLoadedProps { updating: boolean; } async function loadSegments(props: LoadSegmentsProps): Promise { const response = await sendMessage({ message: "isInfoFound", updating: props.updating }) as IsInfoFoundMessageResponse; if (response && response.videoID) { segmentsLoaded(response, props); } else { // Handle error if it exists chrome.runtime.lastError; props.setStatus({ status: LoadingStatus.NoVideo, }); if (!props.updating) { loadRetryCount++; if (loadRetryCount < 6) { setTimeout(() => loadSegments(props), 100 * loadRetryCount); } } } } function segmentsLoaded(response: IsInfoFoundMessageResponse, props: SegmentsLoadedProps): void { if (response.found) { props.setStatus({ status: LoadingStatus.SegmentsFound }); } else if (response.status === 404 || response.status === 200) { props.setStatus({ status: LoadingStatus.NoSegmentsFound }); } else if (response.status) { props.setStatus({ status: LoadingStatus.ConnectionError, code: response.status }); } else { props.setStatus({ status: LoadingStatus.StillLoading }); } props.setVideoID(response.videoID); props.setCurrentTime(response.time); props.setChannelWhitelisted(response.channelWhitelisted); props.setSegments((response.sponsorTimes || []) .filter((segment) => segment.source === SponsorSourceType.Server) .sort((a, b) => b.segment[1] - a.segment[1]) .sort((a, b) => a.segment[0] - b.segment[0]) .sort((a, b) => a.actionType === ActionType.Full ? -1 : b.actionType === ActionType.Full ? 1 : 0)); props.setLoopedChapter(response.loopedChapter); } function sendMessage(request: Message): Promise { return new Promise((resolve) => { if (chrome.tabs) { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => chrome.tabs.sendMessage(tabs[0].id, request, resolve)); } else { chrome.runtime.sendMessage({ message: "tabs", data: request }, resolve); } }); } interface ComPortProps extends SegmentsLoadedProps { } function setupComPort(props: ComPortProps): void { const port = chrome.runtime.connect({ name: "popup" }); port.onDisconnect.addListener(() => setupComPort(props)); port.onMessage.addListener((msg) => onMessage(props, msg)); } function onMessage(props: ComPortProps, msg: PopupMessage) { switch (msg.message) { case "time": props.setCurrentTime(msg.time); break; case "infoUpdated": segmentsLoaded(msg, props); break; case "videoChanged": props.setStatus({ status: LoadingStatus.StillLoading }); props.setVideoID(msg.videoID); props.setChannelWhitelisted(msg.whitelisted); props.setSegments([]); break; } } function forwardClickEvents(sendMessage: (request: Message) => Promise): void { if (window !== window.top) { document.addEventListener("keydown", (e) => { const target = e.target as HTMLElement; if (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || e.key === "ArrowUp" || e.key === "ArrowDown") { return; } if (e.key === " ") { // No scrolling e.preventDefault(); } sendMessage({ message: "keydown", key: e.key, keyCode: e.keyCode, code: e.code, which: e.which, shiftKey: e.shiftKey, ctrlKey: e.ctrlKey, altKey: e.altKey, metaKey: e.metaKey }); }); } }