Move get hash to shared lib

This commit is contained in:
Ajay
2023-02-14 01:42:25 -05:00
parent 12c63b4ff9
commit 1d0ac86e5a
10 changed files with 28 additions and 38 deletions

14
package-lock.json generated
View File

@@ -27,7 +27,7 @@
],
"license": "LGPL-3.0-or-later",
"dependencies": {
"@ajayyy/maze-utils": "^1.1.2",
"@ajayyy/maze-utils": "^1.1.3",
"content-scripts-register-polyfill": "^4.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
@@ -68,9 +68,9 @@
}
},
"node_modules/@ajayyy/maze-utils": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.2.tgz",
"integrity": "sha512-hz8+ONJMnkX3q1XHzN2JEmmg4otsEr88SbnH2Oq35WfWuaGnjsm5VVowEZeAoEd/7ELxD+Mhk+rGZLMzSHXbqQ==",
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.3.tgz",
"integrity": "sha512-3JPPTsmW4G928M4vM6hSo5eU7FZoVLBRuhnuS4Fzi3/3miEgSwV0g1nbwWy4wJrKeB19GSaCGVjLxsGTWsgyrQ==",
"funding": [
{
"type": "individual",
@@ -13420,9 +13420,9 @@
},
"dependencies": {
"@ajayyy/maze-utils": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.2.tgz",
"integrity": "sha512-hz8+ONJMnkX3q1XHzN2JEmmg4otsEr88SbnH2Oq35WfWuaGnjsm5VVowEZeAoEd/7ELxD+Mhk+rGZLMzSHXbqQ=="
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/@ajayyy/maze-utils/-/maze-utils-1.1.3.tgz",
"integrity": "sha512-3JPPTsmW4G928M4vM6hSo5eU7FZoVLBRuhnuS4Fzi3/3miEgSwV0g1nbwWy4wJrKeB19GSaCGVjLxsGTWsgyrQ=="
},
"@ampproject/remapping": {
"version": "2.2.0",

View File

@@ -4,7 +4,7 @@
"description": "",
"main": "background.js",
"dependencies": {
"@ajayyy/maze-utils": "^1.1.2",
"@ajayyy/maze-utils": "^1.1.3",
"content-scripts-register-polyfill": "^4.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"

View File

@@ -1,8 +1,9 @@
import * as CompileConfig from "../config.json";
import * as invidiousList from "../ci/invidiouslist.json";
import { Category, CategorySelection, CategorySkipOption, NoticeVisbilityMode, PreviewBarOption, SponsorTime, Keybind, HashedValue, VideoID, SponsorHideType } from "./types";
import { Category, CategorySelection, CategorySkipOption, NoticeVisbilityMode, PreviewBarOption, SponsorTime, Keybind, VideoID, SponsorHideType } from "./types";
import { isSafari, keybindEquals } from "./utils/configUtils";
import { ProtoConfig } from "@ajayyy/maze-utils/lib/config";
import { HashedValue } from "@ajayyy/maze-utils/lib/hash";
export interface Permission {
canSubmit: boolean;

View File

@@ -6,7 +6,6 @@ import {
ChannelIDInfo,
ChannelIDStatus,
ContentContainer,
HashedValue,
Keybind,
ScheduledTime,
SegmentUUID,
@@ -42,6 +41,7 @@ import { getFormattedTime } from "@ajayyy/maze-utils/lib/formating";
import { setupVideoMutationListener, getChannelIDInfo, getVideo, refreshVideoAttachments, getIsAdPlaying, getIsLivePremiere, setIsAdPlaying, checkVideoIDChange, getVideoID, getYouTubeVideoID, setupVideoModule, checkIfNewVideoID, isOnInvidious, isOnMobileYouTube } from "@ajayyy/maze-utils/lib/video";
import { StorageChangesObject } from "@ajayyy/maze-utils/lib/config";
import { findValidElement } from "@ajayyy/maze-utils/lib/dom"
import { getHash, HashedValue } from "@ajayyy/maze-utils/lib/hash";
const utils = new Utils();
@@ -977,7 +977,7 @@ async function sponsorsLookup(keepOldSubmissions = true) {
const hashParams = getHashParams();
if (hashParams.requiredSegment) extraRequestData.requiredSegment = hashParams.requiredSegment;
const hashPrefix = (await utils.getHash(getVideoID(), 1)).slice(0, 4) as VideoID & HashedValue;
const hashPrefix = (await getHash(getVideoID(), 1)).slice(0, 4) as VideoID & HashedValue;
const response = await utils.asyncRequestToServer('GET', "/api/skipSegments/" + hashPrefix, {
categories,
actionTypes: getEnabledActionTypes(showChapterMessage),
@@ -1060,7 +1060,7 @@ async function sponsorsLookup(keepOldSubmissions = true) {
const downvotedData = Config.local.downvotedSegments[hashPrefix];
if (downvotedData) {
for (const segment of sponsorTimes) {
const hashedUUID = await utils.getHash(segment.UUID, 1);
const hashedUUID = await getHash(segment.UUID, 1);
const segmentDownvoteData = downvotedData.segments.find((downvote) => downvote.uuid === hashedUUID);
if (segmentDownvoteData) {
segment.hidden = segmentDownvoteData.hidden;
@@ -1127,7 +1127,7 @@ function getEnabledActionTypes(forceFullVideo = false): ActionType[] {
}
async function lockedCategoriesLookup(): Promise<void> {
const hashPrefix = (await utils.getHash(getVideoID(), 1)).slice(0, 4);
const hashPrefix = (await getHash(getVideoID(), 1)).slice(0, 4);
const response = await utils.asyncRequestToServer("GET", "/api/lockCategories/" + hashPrefix);
if (response.ok) {

View File

@@ -15,6 +15,7 @@ import KeybindComponent from "./components/options/KeybindComponent";
import { showDonationLink } from "./utils/configUtils";
import { localizeHtmlPage } from "./utils/pageUtils";
import { StorageChangesObject } from "@ajayyy/maze-utils/lib/config";
import { getHash } from "@ajayyy/maze-utils/lib/hash";
const utils = new Utils();
let embed = false;
@@ -532,7 +533,7 @@ function activatePrivateTextChange(element: HTMLElement) {
case "userID":
if (Config.config[option]) {
utils.asyncRequestToServer("GET", "/api/userInfo", {
publicUserID: utils.getHash(Config.config[option]),
publicUserID: getHash(Config.config[option]),
values: ["warnings", "banned"]
}).then((result) => {
const userInfo = JSON.parse(result.responseText);

View File

@@ -28,6 +28,7 @@ import GenericNotice from "./render/GenericNotice";
import { noRefreshFetchingChaptersAllowed } from "./utils/licenseKey";
import { getFormattedTime } from "@ajayyy/maze-utils/lib/formating";
import { StorageChangesObject } from "@ajayyy/maze-utils/lib/config";
import { getHash } from "@ajayyy/maze-utils/lib/hash";
const utils = new Utils();
@@ -231,7 +232,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
PageElements.optionsButton.addEventListener("click", openOptions);
PageElements.helpButton.addEventListener("click", openHelp);
PageElements.refreshSegmentsButton.addEventListener("click", refreshSegments);
PageElements.sbPopupIconCopyUserID.addEventListener("click", async () => copyToClipboard(await utils.getHash(Config.config.userID)));
PageElements.sbPopupIconCopyUserID.addEventListener("click", async () => copyToClipboard(await getHash(Config.config.userID)));
// Forward click events
if (window !== window.top) {
@@ -284,7 +285,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
if (!Config.config.payments.freeAccess && !noRefreshFetchingChaptersAllowed()) values.push("freeChaptersAccess");
utils.asyncRequestToServer("GET", "/api/userInfo", {
publicUserID: await utils.getHash(Config.config.userID),
publicUserID: await getHash(Config.config.userID),
values
}).then((res) => {
if (res.status === 200) {

View File

@@ -26,8 +26,6 @@ export interface ContentContainer {
};
}
export type HashedValue = string & { __hashBrand: unknown };
export interface VideoDurationResponse {
duration: number;
}

View File

@@ -1,6 +1,7 @@
import Config, { VideoDownvotes } from "./config";
import { CategorySelection, SponsorTime, BackgroundScriptContainer, Registration, HashedValue, VideoID, SponsorHideType } from "./types";
import { CategorySelection, SponsorTime, BackgroundScriptContainer, Registration, VideoID, SponsorHideType } from "./types";
import { getHash, HashedValue } from "@ajayyy/maze-utils/lib/hash";
import * as CompileConfig from "../config.json";
import { waitFor } from "@ajayyy/maze-utils";
import { isSafari } from "./utils/configUtils";
@@ -336,25 +337,11 @@ export default class Utils {
return typeof(browser) !== "undefined";
}
async getHash<T extends string>(value: T, times = 5000): Promise<T & HashedValue> {
if (times <= 0) return "" as T & HashedValue;
let hashHex: string = value;
for (let i = 0; i < times; i++) {
const hashBuffer = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(hashHex).buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
return hashHex as T & HashedValue;
}
async addHiddenSegment(videoID: VideoID, segmentUUID: string, hidden: SponsorHideType) {
if (chrome.extension.inIncognitoContext || !Config.config.trackDownvotes) return;
const hashedVideoID = (await this.getHash(videoID, 1)).slice(0, 4) as VideoID & HashedValue;
const UUIDHash = await this.getHash(segmentUUID, 1);
const hashedVideoID = (await getHash(videoID, 1)).slice(0, 4) as VideoID & HashedValue;
const UUIDHash = await getHash(segmentUUID, 1);
const allDownvotes = Config.local.downvotedSegments;
const currentVideoData = allDownvotes[hashedVideoID] || { segments: [], lastAccess: 0 };

View File

@@ -1,6 +1,7 @@
import Config from "../config";
import Utils from "../utils";
import * as CompileConfig from "../../config.json";
import { getHash } from "@ajayyy/maze-utils/lib/hash";
const utils = new Utils();
@@ -53,7 +54,7 @@ export async function fetchingChaptersAllowed(): Promise<boolean> {
// Check for free access if no license key, and it is the first time
const result = await utils.asyncRequestToServer("GET", "/api/userInfo", {
value: "freeChaptersAccess",
publicUserID: await utils.getHash(Config.config.userID)
publicUserID: await getHash(Config.config.userID)
});
try {

View File

@@ -1,4 +1,5 @@
import { objectToURI } from "@ajayyy/maze-utils";
import { getHash } from "@ajayyy/maze-utils/lib/hash";
import Config from "../config";
import GenericNotice, { NoticeOptions } from "../render/GenericNotice";
import { ContentContainer } from "../types";
@@ -13,7 +14,7 @@ export interface ChatConfig {
export async function openWarningDialog(contentContainer: ContentContainer): Promise<void> {
const userInfo = await utils.asyncRequestToServer("GET", "/api/userInfo", {
publicUserID: await utils.getHash(Config.config.userID),
publicUserID: await getHash(Config.config.userID),
values: ["warningReason"]
});
@@ -21,7 +22,7 @@ export async function openWarningDialog(contentContainer: ContentContainer): Pro
const warningReason = JSON.parse(userInfo.responseText)?.warningReason;
const userNameData = await utils.asyncRequestToServer("GET", "/api/getUsername?userID=" + Config.config.userID);
const userName = userNameData.ok ? JSON.parse(userNameData.responseText).userName : "";
const publicUserID = await utils.getHash(Config.config.userID);
const publicUserID = await getHash(Config.config.userID);
let notice: GenericNotice = null;
const options: NoticeOptions = {