mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-06 19:47:04 +03:00
90 lines
3.0 KiB
TypeScript
90 lines
3.0 KiB
TypeScript
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 { ActionTypes, SponsorSourceType, SponsorTime, VideoID } from "../types";
|
|
import { getHashParams } from "./pageUtils";
|
|
import { asyncRequestToServer } from "./requests";
|
|
import { extensionUserAgent } from "../../maze-utils/src";
|
|
|
|
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 extraRequestData: Record<string, unknown> = {};
|
|
const hashParams = getHashParams();
|
|
if (hashParams.requiredSegment) extraRequestData.requiredSegment = hashParams.requiredSegment;
|
|
|
|
const hashPrefix = (await getHash(videoID, 1)).slice(0, 5) as VideoID & HashedValue;
|
|
const hasDownvotedSegments = !!Config.local.downvotedSegments[hashPrefix.slice(0, 4)];
|
|
const response = await asyncRequestToServer('GET', "/api/skipSegments/" + hashPrefix, {
|
|
categories: CompileConfig.categoryList,
|
|
actionTypes: ActionTypes,
|
|
trimUUIDs: hasDownvotedSegments ? null : 5,
|
|
...extraRequestData
|
|
}, {
|
|
"X-CLIENT-NAME": extensionUserAgent(),
|
|
});
|
|
|
|
if (response.ok) {
|
|
const receivedSegments: SponsorTime[] = JSON.parse(response.responseText)
|
|
?.filter((video) => video.videoID === videoID)
|
|
?.map((video) => video.segments)?.[0]
|
|
?.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
|
|
};
|
|
} |