mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-11-10 09:07:45 +01:00
Improve typing on getHash
This commit is contained in:
parent
816e9a78be
commit
0b6ade4a1d
2 changed files with 7 additions and 5 deletions
|
@ -31,6 +31,8 @@ export interface FetchResponse {
|
|||
ok: boolean
|
||||
}
|
||||
|
||||
export type HashedValue = string & { __hashBrand: unknown };
|
||||
|
||||
export interface VideoDurationResponse {
|
||||
duration: number;
|
||||
}
|
||||
|
|
10
src/utils.ts
10
src/utils.ts
|
@ -1,5 +1,5 @@
|
|||
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 { findValidElementFromSelector } from "./utils/pageUtils";
|
||||
|
@ -475,10 +475,10 @@ export default class Utils {
|
|||
return typeof(browser) !== "undefined";
|
||||
}
|
||||
|
||||
async getHash(value: string, times = 5000): Promise<string> {
|
||||
if (times <= 0) return "";
|
||||
async getHash<T extends string>(value: T, times = 5000): Promise<T & HashedValue> {
|
||||
if (times <= 0) return "" as T & HashedValue;
|
||||
|
||||
let hashHex = value;
|
||||
let hashHex: string = value;
|
||||
for (let i = 0; i < times; i++) {
|
||||
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('');
|
||||
}
|
||||
|
||||
return hashHex;
|
||||
return hashHex as T & HashedValue;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue