diff options
author | Ajay <[email protected]> | 2022-02-06 23:17:34 -0500 |
---|---|---|
committer | Ajay <[email protected]> | 2022-02-06 23:17:34 -0500 |
commit | 4d60dec7f997bb076bfe7dc9d2b7079b96df3e95 (patch) | |
tree | 6c53156c3c5f466ae0080c4fdf64cd4af04ded05 /src/utils.ts | |
parent | 53d0ac8677cd6b5664a9fabe2036f5650a4303c6 (diff) | |
download | SponsorBlock-4d60dec7f997bb076bfe7dc9d2b7079b96df3e95.tar.gz SponsorBlock-4d60dec7f997bb076bfe7dc9d2b7079b96df3e95.zip |
Save downvotes and segment hides in local storage
Diffstat (limited to 'src/utils.ts')
-rw-r--r-- | src/utils.ts | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/src/utils.ts b/src/utils.ts index 087f40ab..f593253f 100644 --- a/src/utils.ts +++ b/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"); + } } |