mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-09 13:07:05 +03:00
Add advanced skip options
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import { DataCache } from "../../maze-utils/src/cache";
|
||||
import { getHash, HashedValue } from "../../maze-utils/src/hash";
|
||||
import Config from "../config";
|
||||
import Config, { AdvancedSkipRule, SkipRuleAttribute, SkipRuleOperator } from "../config";
|
||||
import * as CompileConfig from "../../config.json";
|
||||
import { ActionType, ActionTypes, SponsorSourceType, SponsorTime, VideoID } from "../types";
|
||||
import { ActionType, ActionTypes, CategorySelection, CategorySkipOption, SponsorSourceType, SponsorTime, VideoID } from "../types";
|
||||
import { getHashParams } from "./pageUtils";
|
||||
import { asyncRequestToServer } from "./requests";
|
||||
import { extensionUserAgent } from "../../maze-utils/src";
|
||||
import { VideoLabelsCacheData } from "./videoLabels";
|
||||
|
||||
const segmentDataCache = new DataCache<VideoID, SegmentResponse>(() => {
|
||||
return {
|
||||
@@ -44,8 +45,6 @@ export async function getSegmentsForVideo(videoID: VideoID, ignoreCache: boolean
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -67,7 +66,8 @@ async function fetchSegmentsForVideo(videoID: VideoID): Promise<SegmentResponse>
|
||||
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))
|
||||
?.filter((segment) => enabledActionTypes.includes(segment.actionType)
|
||||
&& getCategorySelection(segment).option !== CategorySkipOption.Disabled)
|
||||
?.map((segment) => ({
|
||||
...segment,
|
||||
source: SponsorSourceType.Server
|
||||
@@ -105,3 +105,80 @@ function getEnabledActionTypes(forceFullVideo = false): ActionType[] {
|
||||
|
||||
return actionTypes;
|
||||
}
|
||||
|
||||
export function getCategorySelection(segment: SponsorTime | VideoLabelsCacheData): CategorySelection {
|
||||
for (const ruleSet of Config.local.skipRules) {
|
||||
if (ruleSet.rules.every((rule) => isSkipRulePassing(segment, rule))) {
|
||||
return { name: segment.category, option: ruleSet.skipOption } as CategorySelection;
|
||||
}
|
||||
}
|
||||
|
||||
for (const selection of Config.config.categorySelections) {
|
||||
if (selection.name === segment.category) {
|
||||
return selection;
|
||||
}
|
||||
}
|
||||
return { name: segment.category, option: CategorySkipOption.Disabled} as CategorySelection;
|
||||
}
|
||||
|
||||
function getSkipRuleValue(segment: SponsorTime | VideoLabelsCacheData, rule: AdvancedSkipRule): string | number | undefined {
|
||||
switch (rule.attribute) {
|
||||
case SkipRuleAttribute.StartTime:
|
||||
return (segment as SponsorTime).segment?.[0];
|
||||
case SkipRuleAttribute.EndTime:
|
||||
return (segment as SponsorTime).segment?.[1];
|
||||
case SkipRuleAttribute.Duration:
|
||||
return (segment as SponsorTime).segment?.[1] - (segment as SponsorTime).segment?.[0];
|
||||
case SkipRuleAttribute.Category:
|
||||
return segment.category;
|
||||
case SkipRuleAttribute.Description:
|
||||
return (segment as SponsorTime).description || "";
|
||||
case SkipRuleAttribute.Source:
|
||||
switch ((segment as SponsorTime).source) {
|
||||
case SponsorSourceType.Local:
|
||||
return "local";
|
||||
case SponsorSourceType.YouTube:
|
||||
return "youtube";
|
||||
case SponsorSourceType.Server:
|
||||
return "server";
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function isSkipRulePassing(segment: SponsorTime | VideoLabelsCacheData, rule: AdvancedSkipRule): boolean {
|
||||
const value = getSkipRuleValue(segment, rule);
|
||||
|
||||
switch (rule.operator) {
|
||||
case SkipRuleOperator.Less:
|
||||
return typeof value === "number" && value < (rule.value as number);
|
||||
case SkipRuleOperator.LessOrEqual:
|
||||
return typeof value === "number" && value <= (rule.value as number);
|
||||
case SkipRuleOperator.Greater:
|
||||
return typeof value === "number" && value > (rule.value as number);
|
||||
case SkipRuleOperator.GreaterOrEqual:
|
||||
return typeof value === "number" && value >= (rule.value as number);
|
||||
case SkipRuleOperator.Equal:
|
||||
return value === rule.value;
|
||||
case SkipRuleOperator.NotEqual:
|
||||
return value !== rule.value;
|
||||
case SkipRuleOperator.Contains:
|
||||
return String(value).includes(String(rule.value));
|
||||
case SkipRuleOperator.Regex:
|
||||
return new RegExp(rule.value as string).test(String(value));
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function getCategoryDefaultSelection(category: string): CategorySelection {
|
||||
for (const selection of Config.config.categorySelections) {
|
||||
if (selection.name === category) {
|
||||
return selection;
|
||||
}
|
||||
}
|
||||
return { name: category, option: CategorySkipOption.Disabled} as CategorySelection;
|
||||
}
|
||||
Reference in New Issue
Block a user