diff --git a/add-on/src/lib/redirect-handler/blockOrObserve.ts b/add-on/src/lib/redirect-handler/blockOrObserve.ts index 2a4728280..3c1c72e6b 100644 --- a/add-on/src/lib/redirect-handler/blockOrObserve.ts +++ b/add-on/src/lib/redirect-handler/blockOrObserve.ts @@ -1,4 +1,5 @@ import debug from 'debug' +import { fastHashCode } from 'fast-hash-code' import browser from 'webextension-polyfill' import { CompanionState } from '../../types/companion.js' import { IFilter, IRegexFilter, RegexFilter } from './baseRegexFilter.js' @@ -284,8 +285,10 @@ function saveAndGenerateRule ( regexSubstitution: string, excludedInitiatorDomains: string[] = [] ): browser.DeclarativeNetRequest.Rule { - // We need to generate a random ID for the rule. - const id = Math.floor(Math.random() * 29999) + // We need to generate a positive number as an id. + const id = fastHashCode(`${regexFilter}:${regexSubstitution}:${excludedInitiatorDomains.join(':')}`, { + forcePositive: true + }) // We need to save the regex filter and ID to check if the rule already exists later. savedRegexFilters.set(regexFilter, { id, regexSubstitution }) return generateAddRule(id, regexFilter, regexSubstitution, excludedInitiatorDomains) diff --git a/package-lock.json b/package-lock.json index b147e047e..abecff204 100644 --- a/package-lock.json +++ b/package-lock.json @@ -58,6 +58,7 @@ "css-loader": "6.7.2", "download-cli": "1.1.1", "exponential-backoff": "^3.1.0", + "fast-hash-code": "^2.1.0", "file-loader": "6.2.0", "fs-promise": "2.0.3", "get-firefox": "5.0.0", @@ -8869,6 +8870,12 @@ "node": ">=8.6.0" } }, + "node_modules/fast-hash-code": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/fast-hash-code/-/fast-hash-code-2.1.0.tgz", + "integrity": "sha512-hJ/kSGwlKevFdK7ff1wv6H5m5zINYnScp2/uX4G24OCvGvVM/3/UmIpdNkMW3x6T6CMSN1TG94nMSjiqHHVg8A==", + "dev": true + }, "node_modules/fast-json-patch": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/fast-json-patch/-/fast-json-patch-2.2.1.tgz", @@ -28064,6 +28071,12 @@ "micromatch": "^4.0.4" } }, + "fast-hash-code": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/fast-hash-code/-/fast-hash-code-2.1.0.tgz", + "integrity": "sha512-hJ/kSGwlKevFdK7ff1wv6H5m5zINYnScp2/uX4G24OCvGvVM/3/UmIpdNkMW3x6T6CMSN1TG94nMSjiqHHVg8A==", + "dev": true + }, "fast-json-patch": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/fast-json-patch/-/fast-json-patch-2.2.1.tgz", diff --git a/package.json b/package.json index 79fa3129e..8f0cad3bd 100644 --- a/package.json +++ b/package.json @@ -111,6 +111,7 @@ "css-loader": "6.7.2", "download-cli": "1.1.1", "exponential-backoff": "^3.1.0", + "fast-hash-code": "^2.1.0", "file-loader": "6.2.0", "fs-promise": "2.0.3", "get-firefox": "5.0.0",