mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-11 22:17:21 +03:00
Only do pre-fetch on hover if there are segments near the start of the video
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { isOnInvidious, parseYouTubeVideoIDFromURL } from "../../maze-utils/src/video";
|
import { isOnInvidious, parseYouTubeVideoIDFromURL } from "../../maze-utils/src/video";
|
||||||
import Config from "../config";
|
import Config from "../config";
|
||||||
import { getVideoLabel } from "./videoLabels";
|
import { getHasStartSegment, getVideoLabel } from "./videoLabels";
|
||||||
import { getThumbnailSelector, setThumbnailListener } from "../../maze-utils/src/thumbnailManagement";
|
import { getThumbnailSelector, setThumbnailListener } from "../../maze-utils/src/thumbnailManagement";
|
||||||
import { VideoID } from "../types";
|
import { VideoID } from "../types";
|
||||||
import { getSegmentsForVideo } from "./segmentData";
|
import { getSegmentsForVideo } from "./segmentData";
|
||||||
@@ -59,14 +59,14 @@ function thumbnailHoverListener(e: MouseEvent) {
|
|||||||
|
|
||||||
// Pre-fetch data for this video
|
// Pre-fetch data for this video
|
||||||
let fetched = false;
|
let fetched = false;
|
||||||
const preFetch = () => {
|
const preFetch = async () => {
|
||||||
fetched = true;
|
fetched = true;
|
||||||
const videoID = extractVideoID(thumbnail);
|
const videoID = extractVideoID(thumbnail);
|
||||||
if (videoID) {
|
if (videoID && await getHasStartSegment(videoID)) {
|
||||||
void getSegmentsForVideo(videoID, false);
|
void getSegmentsForVideo(videoID, false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const timeout = setTimeout(preFetch, 200);
|
const timeout = setTimeout(preFetch, 100);
|
||||||
const onMouseDown = () => {
|
const onMouseDown = () => {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
if (!fetched) {
|
if (!fetched) {
|
||||||
|
|||||||
@@ -6,9 +6,14 @@ import { asyncRequestToServer } from "./requests";
|
|||||||
|
|
||||||
const utils = new Utils();
|
const utils = new Utils();
|
||||||
|
|
||||||
|
interface VideoLabelsCacheData {
|
||||||
|
category: Category;
|
||||||
|
hasStartSegment: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export interface LabelCacheEntry {
|
export interface LabelCacheEntry {
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
videos: Record<VideoID, Category>;
|
videos: Record<VideoID, VideoLabelsCacheData>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const labelCache: Record<string, LabelCacheEntry> = {};
|
const labelCache: Record<string, LabelCacheEntry> = {};
|
||||||
@@ -21,7 +26,7 @@ async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry |
|
|||||||
return cachedEntry;
|
return cachedEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}`);
|
const response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}?hasStartSegment=true`);
|
||||||
if (response.status !== 200) {
|
if (response.status !== 200) {
|
||||||
// No video labels or server down
|
// No video labels or server down
|
||||||
labelCache[hashPrefix] = {
|
labelCache[hashPrefix] = {
|
||||||
@@ -36,7 +41,10 @@ async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry |
|
|||||||
|
|
||||||
const newEntry: LabelCacheEntry = {
|
const newEntry: LabelCacheEntry = {
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
videos: Object.fromEntries(data.map(video => [video.videoID, video.segments[0].category])),
|
videos: Object.fromEntries(data.map(video => [video.videoID, {
|
||||||
|
category: video.segments[0]?.category,
|
||||||
|
hasStartSegment: video.hasStartSegment
|
||||||
|
}])),
|
||||||
};
|
};
|
||||||
labelCache[hashPrefix] = newEntry;
|
labelCache[hashPrefix] = newEntry;
|
||||||
|
|
||||||
@@ -55,11 +63,11 @@ async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry |
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getVideoLabel(videoID: VideoID): Promise<Category | null> {
|
export async function getVideoLabel(videoID: VideoID): Promise<Category | null> {
|
||||||
const prefix = (await getHash(videoID, 1)).slice(0, 3);
|
const prefix = (await getHash(videoID, 1)).slice(0, 4);
|
||||||
const result = await getLabelHashBlock(prefix);
|
const result = await getLabelHashBlock(prefix);
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
const category = result.videos[videoID];
|
const category = result.videos[videoID]?.category;
|
||||||
if (category && utils.getCategorySelection(category).option !== CategorySkipOption.Disabled) {
|
if (category && utils.getCategorySelection(category).option !== CategorySkipOption.Disabled) {
|
||||||
return category;
|
return category;
|
||||||
} else {
|
} else {
|
||||||
@@ -69,3 +77,14 @@ export async function getVideoLabel(videoID: VideoID): Promise<Category | null>
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getHasStartSegment(videoID: VideoID): Promise<boolean | null> {
|
||||||
|
const prefix = (await getHash(videoID, 1)).slice(0, 4);
|
||||||
|
const result = await getLabelHashBlock(prefix);
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
return result?.videos[videoID]?.hasStartSegment ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user