mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-09 21:17:20 +03:00
Improve typing on getHash
This commit is contained in:
@@ -31,6 +31,8 @@ export interface FetchResponse {
|
|||||||
ok: boolean
|
ok: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type HashedValue = string & { __hashBrand: unknown };
|
||||||
|
|
||||||
export interface VideoDurationResponse {
|
export interface VideoDurationResponse {
|
||||||
duration: number;
|
duration: number;
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/utils.ts
10
src/utils.ts
@@ -1,5 +1,5 @@
|
|||||||
import Config from "./config";
|
import Config from "./config";
|
||||||
import { CategorySelection, SponsorTime, FetchResponse, BackgroundScriptContainer, Registration } from "./types";
|
import { CategorySelection, SponsorTime, FetchResponse, BackgroundScriptContainer, Registration, HashedValue } from "./types";
|
||||||
|
|
||||||
import * as CompileConfig from "../config.json";
|
import * as CompileConfig from "../config.json";
|
||||||
import { findValidElementFromSelector } from "./utils/pageUtils";
|
import { findValidElementFromSelector } from "./utils/pageUtils";
|
||||||
@@ -475,10 +475,10 @@ export default class Utils {
|
|||||||
return typeof(browser) !== "undefined";
|
return typeof(browser) !== "undefined";
|
||||||
}
|
}
|
||||||
|
|
||||||
async getHash(value: string, times = 5000): Promise<string> {
|
async getHash<T extends string>(value: T, times = 5000): Promise<T & HashedValue> {
|
||||||
if (times <= 0) return "";
|
if (times <= 0) return "" as T & HashedValue;
|
||||||
|
|
||||||
let hashHex = value;
|
let hashHex: string = value;
|
||||||
for (let i = 0; i < times; i++) {
|
for (let i = 0; i < times; i++) {
|
||||||
const hashBuffer = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(hashHex).buffer);
|
const hashBuffer = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(hashHex).buffer);
|
||||||
|
|
||||||
@@ -486,6 +486,6 @@ export default class Utils {
|
|||||||
hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
return hashHex;
|
return hashHex as T & HashedValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user