diff --git a/package-lock.json b/package-lock.json index 7e806d7e..5cd00a3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10494,11 +10494,6 @@ "traverse": "0.4.x" } }, - "js-sha256": { - "version": "0.9.0", - "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz", - "integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==" - }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/package.json b/package.json index 4a6b0783..b2b0ddbf 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,6 @@ "babel-loader": "^8.0.6", "babel-preset-env": "^1.7.0", "concurrently": "^5.1.0", - "js-sha256": "^0.9.0", "react": "^16.12.0", "react-dom": "^16.12.0" }, diff --git a/src/content.ts b/src/content.ts index a2911f9a..fe149d90 100644 --- a/src/content.ts +++ b/src/content.ts @@ -544,7 +544,7 @@ function incorrectVideoCheck(videoID?: string, sponsorTime?: SponsorTime): boole } } -function sponsorsLookup(id: string) { +async function sponsorsLookup(id: string) { video = document.querySelector('video') // Youtube video player //there is no video here if (video == null) { @@ -569,7 +569,7 @@ function sponsorsLookup(id: string) { updateAdFlag(); // Make sure it doesn't get double called with the playing event - if (Math.abs(lastCheckVideoTime - video.currentTime) > 0.3 + if (Math.abs(lastCheckVideoTime - video.currentTime) > 0.3 || (lastCheckVideoTime !== video.currentTime && Date.now() - lastCheckTime > 2000)) { lastCheckTime = Date.now(); lastCheckVideoTime = video.currentTime; @@ -580,7 +580,7 @@ function sponsorsLookup(id: string) { }); video.addEventListener('playing', () => { // Make sure it doesn't get double called with the play event - if (Math.abs(lastCheckVideoTime - video.currentTime) > 0.3 + if (Math.abs(lastCheckVideoTime - video.currentTime) > 0.3 || (lastCheckVideoTime !== video.currentTime && Date.now() - lastCheckTime > 2000)) { lastCheckTime = Date.now(); lastCheckVideoTime = video.currentTime; @@ -592,7 +592,7 @@ function sponsorsLookup(id: string) { if (!video.paused){ // Reset lastCheckVideoTime lastCheckTime = Date.now(); - lastCheckVideoTime = video.currentTime; + lastCheckVideoTime = video.currentTime; startSponsorSchedule(); } @@ -621,7 +621,8 @@ function sponsorsLookup(id: string) { // Check for hashPrefix setting let getRequest; if (Config.config.hashPrefix) { - getRequest = utils.asyncRequestToServer('GET', "/api/skipSegments/" + utils.getHash(id, 1).substr(0,4), { + const hashPrefix = (await utils.getHash(id, 1)).substr(0, 4); + getRequest = utils.asyncRequestToServer('GET', "/api/skipSegments/" + hashPrefix, { categories }); } else { diff --git a/src/utils.ts b/src/utils.ts index d95da7dc..91c8eb86 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,5 @@ import Config from "./config"; import { CategorySelection, SponsorTime, FetchResponse } from "./types"; -import { sha256 } from 'js-sha256'; import * as CompileConfig from "../config.json"; @@ -380,19 +379,21 @@ class Utils { return typeof(browser) !== "undefined"; } - getHash(value: string, times=5000): string { + async getHash(value: string, times = 5000): Promise { if (times <= 0) return ""; + let hashBuffer = new TextEncoder().encode(value).buffer; + for (let i = 0; i < times; i++) { - let hash = sha256.create(); - hash.update(value); - hash.hex(); - value = hash.toString(); + hashBuffer = await crypto.subtle.digest('SHA-256', hashBuffer); } - return value; + const hashArray = Array.from(new Uint8Array(hashBuffer)); + const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); + + return hashHex; } } -export default Utils; \ No newline at end of file +export default Utils;