diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/requests.ts | 2 | ||||
-rw-r--r-- | src/utils/segmentData.ts | 104 | ||||
-rw-r--r-- | src/utils/thumbnails.ts | 50 |
3 files changed, 146 insertions, 10 deletions
diff --git a/src/utils/requests.ts b/src/utils/requests.ts index acbde374..8ce80601 100644 --- a/src/utils/requests.ts +++ b/src/utils/requests.ts @@ -23,8 +23,6 @@ export function asyncRequestToCustomServer(type: string, url: string, data = {}, export async function asyncRequestToServer(type: string, address: string, data = {}, headers = {}): Promise<FetchResponse> { const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress; - console.log(address, headers) - return await (asyncRequestToCustomServer(type, serverAddress + address, data, headers)); } diff --git a/src/utils/segmentData.ts b/src/utils/segmentData.ts new file mode 100644 index 00000000..1c2e631a --- /dev/null +++ b/src/utils/segmentData.ts @@ -0,0 +1,104 @@ +import { DataCache } from "../../maze-utils/src/cache"; +import { getHash, HashedValue } from "../../maze-utils/src/hash"; +import Config from "../config"; +import * as CompileConfig from "../../config.json"; +import { ActionType, ActionTypes, SponsorSourceType, SponsorTime, VideoID } from "../types"; +import { getHashParams } from "./pageUtils"; +import { asyncRequestToServer } from "./requests"; + +const segmentDataCache = new DataCache<VideoID, SegmentResponse>(() => { + return { + segments: null, + status: 200 + }; +}, 5); + +const pendingList: Record<VideoID, Promise<SegmentResponse>> = {}; + +export interface SegmentResponse { + segments: SponsorTime[] | null; + status: number; +} + +export async function getSegmentsForVideo(videoID: VideoID, ignoreCache: boolean): Promise<SegmentResponse> { + if (!ignoreCache) { + const cachedData = segmentDataCache.getFromCache(videoID); + if (cachedData) { + segmentDataCache.cacheUsed(videoID); + return cachedData; + } + } + + if (pendingList[videoID]) { + return await pendingList[videoID]; + } + + const pendingData = fetchSegmentsForVideo(videoID); + pendingList[videoID] = pendingData; + + const result = await pendingData; + delete pendingList[videoID]; + + return result; +} + +async function fetchSegmentsForVideo(videoID: VideoID): Promise<SegmentResponse> { + const categories: string[] = Config.config.categorySelections.map((category) => category.name); + + const extraRequestData: Record<string, unknown> = {}; + const hashParams = getHashParams(); + if (hashParams.requiredSegment) extraRequestData.requiredSegment = hashParams.requiredSegment; + + const hashPrefix = (await getHash(videoID, 1)).slice(0, 4) as VideoID & HashedValue; + const response = await asyncRequestToServer('GET', "/api/skipSegments/" + hashPrefix, { + categories: CompileConfig.categoryList, + actionTypes: ActionTypes, + ...extraRequestData + }, { + "X-CLIENT-NAME": `${chrome.runtime.id}/v${chrome.runtime.getManifest().version}` + }); + + if (response.ok) { + const enabledActionTypes = getEnabledActionTypes(); + + const receivedSegments: SponsorTime[] = JSON.parse(response.responseText) + ?.filter((video) => video.videoID === videoID) + ?.map((video) => video.segments)?.[0] + ?.filter((segment) => enabledActionTypes.includes(segment.actionType) && categories.includes(segment.category)) + ?.map((segment) => ({ + ...segment, + source: SponsorSourceType.Server + })) + ?.sort((a, b) => a.segment[0] - b.segment[0]); + + if (receivedSegments && receivedSegments.length) { + const result = { + segments: receivedSegments, + status: response.status + }; + + segmentDataCache.setupCache(videoID).segments = result.segments; + return result; + } else { + // Setup with null data + segmentDataCache.setupCache(videoID); + } + } + + return { + segments: null, + status: response.status + }; +} + +function getEnabledActionTypes(forceFullVideo = false): ActionType[] { + const actionTypes = [ActionType.Skip, ActionType.Poi, ActionType.Chapter]; + if (Config.config.muteSegments) { + actionTypes.push(ActionType.Mute); + } + if (Config.config.fullVideoSegments || forceFullVideo) { + actionTypes.push(ActionType.Full); + } + + return actionTypes; +}
\ No newline at end of file diff --git a/src/utils/thumbnails.ts b/src/utils/thumbnails.ts index 61d28f18..0fb2579e 100644 --- a/src/utils/thumbnails.ts +++ b/src/utils/thumbnails.ts @@ -1,10 +1,15 @@ import { isOnInvidious, parseYouTubeVideoIDFromURL } from "../../maze-utils/src/video"; import Config from "../config"; import { getVideoLabel } from "./videoLabels"; -import { setThumbnailListener } from "../../maze-utils/src/thumbnailManagement"; - -export async function labelThumbnails(thumbnails: HTMLImageElement[]): Promise<void> { - await Promise.all(thumbnails.map((t) => labelThumbnail(t))); +import { getThumbnailSelector, setThumbnailListener } from "../../maze-utils/src/thumbnailManagement"; +import { VideoID } from "../types"; +import { getSegmentsForVideo } from "./segmentData"; + +export async function handleThumbnails(thumbnails: HTMLImageElement[]): Promise<void> { + await Promise.all(thumbnails.map((t) => { + labelThumbnail(t); + setupThumbnailHover(t); + })); } export async function labelThumbnail(thumbnail: HTMLImageElement): Promise<HTMLElement | null> { @@ -13,9 +18,7 @@ export async function labelThumbnail(thumbnail: HTMLImageElement): Promise<HTMLE return null; } - const link = (isOnInvidious() ? thumbnail.parentElement : thumbnail.querySelector("#thumbnail")) as HTMLAnchorElement - if (!link || link.nodeName !== "A" || !link.href) return null; // no link found - const videoID = parseYouTubeVideoIDFromURL(link.href)?.videoID; + const videoID = extractVideoID(thumbnail); if (!videoID) { hideThumbnailLabel(thumbnail); return null; @@ -37,6 +40,37 @@ export async function labelThumbnail(thumbnail: HTMLImageElement): Promise<HTMLE return overlay; } +export async function setupThumbnailHover(thumbnail: HTMLImageElement): Promise<void> { + // Cache would be reset every load due to no SPA + if (isOnInvidious()) return; + + const mainElement = thumbnail.closest("#dismissible") as HTMLElement; + if (mainElement) { + mainElement.removeEventListener("mouseenter", thumbnailHoverListener); + mainElement.addEventListener("mouseenter", thumbnailHoverListener); + } +} + +function thumbnailHoverListener(e: MouseEvent) { + if (!chrome.runtime?.id) return; + + const thumbnail = (e.target as HTMLElement).querySelector(getThumbnailSelector()) as HTMLImageElement; + if (!thumbnail) return; + + // Pre-fetch data for this video + const videoID = extractVideoID(thumbnail); + if (videoID) { + void getSegmentsForVideo(videoID, false); + } +} + +function extractVideoID(thumbnail: HTMLImageElement): VideoID | null { + const link = (isOnInvidious() ? thumbnail.parentElement : thumbnail.querySelector("#thumbnail")) as HTMLAnchorElement + if (!link || link.nodeName !== "A" || !link.href) return null; // no link found + + return parseYouTubeVideoIDFromURL(link.href)?.videoID; +} + function getOldThumbnailLabel(thumbnail: HTMLImageElement): HTMLElement | null { return thumbnail.querySelector(".sponsorThumbnailLabel") as HTMLElement | null; } @@ -109,7 +143,7 @@ function insertSBIconDefinition() { } export function setupThumbnailListener(): void { - setThumbnailListener(labelThumbnails, () => { + setThumbnailListener(handleThumbnails, () => { insertSBIconDefinition(); }, () => Config.isReady()); }
\ No newline at end of file |