Skip to content

Commit 92c55c3

Browse files
authored
feat(mv3): ➕ Adding hash function to generate predict… (#1273)
feat(mv3): ➕ Adding hash function to generate predictable ids. Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>
1 parent a91bd9a commit 92c55c3

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

add-on/src/lib/redirect-handler/blockOrObserve.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import debug from 'debug'
2+
import { fastHashCode } from 'fast-hash-code'
23
import browser from 'webextension-polyfill'
34
import { CompanionState } from '../../types/companion.js'
45
import { IFilter, IRegexFilter, RegexFilter } from './baseRegexFilter.js'
@@ -284,8 +285,10 @@ function saveAndGenerateRule (
284285
regexSubstitution: string,
285286
excludedInitiatorDomains: string[] = []
286287
): browser.DeclarativeNetRequest.Rule {
287-
// We need to generate a random ID for the rule.
288-
const id = Math.floor(Math.random() * 29999)
288+
// We need to generate a positive number as an id.
289+
const id = fastHashCode(`${regexFilter}:${regexSubstitution}:${excludedInitiatorDomains.join(':')}`, {
290+
forcePositive: true
291+
})
289292
// We need to save the regex filter and ID to check if the rule already exists later.
290293
savedRegexFilters.set(regexFilter, { id, regexSubstitution })
291294
return generateAddRule(id, regexFilter, regexSubstitution, excludedInitiatorDomains)

package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"css-loader": "6.7.2",
112112
"download-cli": "1.1.1",
113113
"exponential-backoff": "^3.1.0",
114+
"fast-hash-code": "^2.1.0",
114115
"file-loader": "6.2.0",
115116
"fs-promise": "2.0.3",
116117
"get-firefox": "5.0.0",

0 commit comments

Comments
 (0)