mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2026-01-28 05:10:50 +03:00
Save downvotes and segment hides in local storage
This commit is contained in:
43
src/utils.ts
43
src/utils.ts
@@ -1,5 +1,5 @@
|
||||
import Config from "./config";
|
||||
import { CategorySelection, SponsorTime, FetchResponse, BackgroundScriptContainer, Registration, HashedValue } from "./types";
|
||||
import Config, { VideoDownvotes } from "./config";
|
||||
import { CategorySelection, SponsorTime, FetchResponse, BackgroundScriptContainer, Registration, HashedValue, VideoID, SponsorHideType } from "./types";
|
||||
|
||||
import * as CompileConfig from "../config.json";
|
||||
import { findValidElementFromSelector } from "./utils/pageUtils";
|
||||
@@ -488,4 +488,43 @@ export default class Utils {
|
||||
|
||||
return hashHex as T & HashedValue;
|
||||
}
|
||||
|
||||
async addHiddenSegment(videoID: VideoID, segmentUUID: string, hidden: SponsorHideType) {
|
||||
const hashedVideoID = (await this.getHash(videoID, 1)).slice(0, 4) as VideoID & HashedValue;
|
||||
const UUIDHash = await this.getHash(segmentUUID, 1);
|
||||
|
||||
const allDownvotes = Config.local.downvotedSegments;
|
||||
const currentVideoData = allDownvotes[hashedVideoID] || { segments: [], lastAccess: 0 };
|
||||
|
||||
currentVideoData.lastAccess = Date.now();
|
||||
const existingData = currentVideoData.segments.find((segment) => segment.uuid === UUIDHash);
|
||||
if (hidden === SponsorHideType.Visible) {
|
||||
delete allDownvotes[hashedVideoID];
|
||||
} else {
|
||||
if (existingData) {
|
||||
existingData.hidden = hidden;
|
||||
} else {
|
||||
currentVideoData.segments.push({
|
||||
uuid: UUIDHash,
|
||||
hidden
|
||||
});
|
||||
}
|
||||
|
||||
allDownvotes[hashedVideoID] = currentVideoData;
|
||||
}
|
||||
|
||||
const entries = Object.entries(allDownvotes);
|
||||
if (entries.length > 10000) {
|
||||
let min: [string, VideoDownvotes] = null;
|
||||
for (let i = 0; i < entries[0].length; i++) {
|
||||
if (min === null || entries[i][1].lastAccess < min[1].lastAccess) {
|
||||
min = entries[i];
|
||||
}
|
||||
}
|
||||
|
||||
delete allDownvotes[min[0]];
|
||||
}
|
||||
|
||||
Config.forceLocalUpdate("downvotedSegments");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user