Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Jan 16, 2025
1 parent 9f2523c commit c1cbeae
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 33 deletions.
35 changes: 6 additions & 29 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import SubmissionNotice from "./render/SubmissionNotice";
import { FetchResponse } from "./requests/background-request-proxy";
import { getPortVideoByHash, postPortVideo, postPortVideoVote, updatePortedSegments } from "./requests/portVideo";
import { asyncRequestToServer } from "./requests/requests";
import { getSegmentsByHash } from "./requests/segments";
import { getSegmentsByHash, getSegmentsByVideoID } from "./requests/segments";

Check failure on line 18 in src/content.ts

View workflow job for this annotation

GitHub Actions / Create artifacts

'getSegmentsByHash' is defined but never used
import { getVideoLabel } from "./requests/videoLabels";
import { checkPageForNewThumbnails, setupThumbnailListener } from "./thumbnail-utils/thumbnailManagement";
import {
Expand Down Expand Up @@ -1342,31 +1342,20 @@ async function sponsorsLookup(keepOldSubmissions = true, ignoreServerCache = fal
return;
}

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 getVideoIDHash(getVideoID())).slice(0, 4) as VideoID & HashedValue;
const response = await getSegmentsByHash(hashPrefix, extraRequestData, ignoreServerCache);
// const response = await getSegmentsByHash(hashPrefix, extraRequestData, ignoreServerCache);
const response = await getSegmentsByVideoID(getVideoID(), extraRequestData, ignoreServerCache);

// store last response status
lastResponseStatus = response?.status;

if (response?.ok) {
const receivedSegments: SponsorTime[] = JSON.parse(response.responseText)
?.filter((video: SponsorTimeHashedID) => video.videoID === getVideoID())
?.map((video: SponsorTimeHashedID) => video.segments)?.[0]
?.filter(
(segment: SponsorTime) =>
getEnabledActionTypes().includes(segment.actionType) && categories.includes(segment.category)
)
?.map((segment: SponsorTime) => ({
...segment,
source: SponsorSourceType.Server,
}))
?.sort((a: SponsorTime, b: SponsorTime) => a.segment[0] - b.segment[0]);
if (response.status === 200) {
const receivedSegments: SponsorTime[] = response.segments;

if (receivedSegments && receivedSegments.length) {
sponsorDataFound = true;

Expand Down Expand Up @@ -1451,18 +1440,6 @@ async function sponsorsLookup(keepOldSubmissions = true, ignoreServerCache = fal
}
}

function getEnabledActionTypes(forceFullVideo = false): ActionType[] {
const actionTypes = [ActionType.Skip, ActionType.Poi];
if (Config.config.muteSegments) {
actionTypes.push(ActionType.Mute);
}
if (Config.config.fullVideoSegments || forceFullVideo) {
actionTypes.push(ActionType.Full);
}

return actionTypes;
}

async function lockedCategoriesLookup(): Promise<void> {
const hashPrefix = (await getHash(getVideoID(), 1)).slice(0, 4);
const response = await asyncRequestToServer("GET", "/api/lockCategories/" + hashPrefix);
Expand Down
66 changes: 64 additions & 2 deletions src/requests/segments.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,78 @@
import Config from "../config";
import { ActionType, SponsorSourceType, SponsorTime, SponsorTimeHashedID, VideoID } from "../types";
import { DataCache } from "../utils/cache";
import { getVideoIDHash, HashedValue } from "../utils/hash";
import { asyncRequestToServer } from "./requests";

const segmentCache = new DataCache<VideoID, SegmentResponse>(() => {
return {
segments: null,
status: 200,
};
}, 50);

export interface SegmentResponse {
segments: SponsorTime[] | null;
status: number;
}

export async function getSegmentsByHash(
hashPrefix: string,
extraRequestData: Record<string, unknown>,
ignoreServerCache: boolean
ignoreCache: boolean
) {
const response = await asyncRequestToServer(
"GET",
"/api/skipSegments/" + hashPrefix,
extraRequestData,
ignoreServerCache
ignoreCache
);

return response;
}

export async function getSegmentsByVideoID(
videoID: string,
extraRequestData: Record<string, unknown> = {},
ignoreCache: boolean = false
): Promise<SegmentResponse> {
if (ignoreCache) {
segmentCache.delete(videoID);
}
const cachedData = segmentCache.getFromCache(videoID);
if (cachedData) {
return cachedData;
}

const categories: string[] = Config.config.categorySelections.map((category) => category.name);
const hashPrefix = (await getVideoIDHash(videoID)).slice(0, 4) as VideoID & HashedValue;
const response = await getSegmentsByHash(hashPrefix, extraRequestData, ignoreCache);

const segments = JSON.parse(response?.responseText)
?.filter((video: SponsorTimeHashedID) => video.videoID === videoID)
?.map((video: SponsorTimeHashedID) => video.segments)?.[0]
?.filter(
(segment: SponsorTime) =>
getEnabledActionTypes().includes(segment.actionType) && categories.includes(segment.category)
)
?.map((segment: SponsorTime) => ({
...segment,
source: SponsorSourceType.Server,
}))
?.sort((a: SponsorTime, b: SponsorTime) => a.segment[0] - b.segment[0]);

console.log(segments);
return null;
}

function getEnabledActionTypes(forceFullVideo = false): ActionType[] {
const actionTypes = [ActionType.Skip, ActionType.Poi];
if (Config.config.muteSegments) {
actionTypes.push(ActionType.Mute);
}
if (Config.config.fullVideoSegments || forceFullVideo) {
actionTypes.push(ActionType.Full);
}

return actionTypes;
}
1 change: 1 addition & 0 deletions src/requests/videoLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async function getLabelHashBlock(hashPrefix: string, refreshCache: boolean = fal
}

export async function getVideoLabel(videoID: VideoID, refreshCache: boolean = false): Promise<Category | null> {
return "sponsor" as Category;
const prefix = (await getVideoIDHash(videoID)).slice(0, 3);
const result = await getLabelHashBlock(prefix, refreshCache);

Expand Down
18 changes: 16 additions & 2 deletions src/thumbnail-utils/thumbnails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Config from "../config";
import { getVideoLabel } from "../requests/videoLabels";
import { VideoID } from "../types";
import { waitFor } from "../utils/";
import { getBvIDFromURL } from "../utils/parseVideoID";
import { getLabelAnchorSelector, getLinkAttribute, getLinkSelectors } from "./thumbnail-selectors";
Expand Down Expand Up @@ -52,7 +53,7 @@ export async function labelThumbnailProcess(
const [videoID] = videoIDs;

// 获取或创建缩略图标签
const { overlay, text } = await createOrGetThumbnail(thumbnail, containerType);
const { overlay, text } = await createOrGetThumbnail(thumbnail, containerType, videoID);

const category = await getVideoLabel(videoID);
if (!category) {
Expand Down Expand Up @@ -90,10 +91,23 @@ async function hideThumbnailLabel(thumbnail: HTMLElement): Promise<void> {
}
}

const preloadSegments = (e: MouseEvent) => {
const videoID = (e.target as HTMLElement).getAttribute("data-bsb-bvid");
console.log("pointerenter", videoID);
};

async function createOrGetThumbnail(
thumbnail: HTMLElement,
containerType: string
containerType: string,
videoID: VideoID
): Promise<{ overlay: HTMLElement; text: HTMLElement }> {
// only add evnet listener once, add preloadSegments to thumbnail when pointerenter
if (thumbnail.getAttribute("data-bsb-bvid") != videoID) {
thumbnail.setAttribute("data-bsb-bvid", videoID);
thumbnail.removeEventListener("mouseenter", preloadSegments);
thumbnail.addEventListener("mouseenter", preloadSegments);
}

const oldLabelElement = await getOldThumbnailLabel(thumbnail);
if (oldLabelElement) {
return {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class DataCache<T extends string, V> {
}

public getFromCache(key: T): (V & CacheRecord) | undefined {
this.cacheUsed(key);
return this.cache[key];
}

Expand All @@ -25,6 +26,10 @@ export class DataCache<T extends string, V> {
this.gc();
}

public delete(key: T): void {
delete this.cache[key];
}

public setupCache(key: T): V & CacheRecord {
if (!this.cache[key] && this.init) {
this.set(key, this.init());
Expand Down

0 comments on commit c1cbeae

Please sign in to comment.