Skip to content

Commit

Permalink
feat(mv3): ➕ Adding hash function to generate predict… (#1273)
Browse files Browse the repository at this point in the history
feat(mv3): ➕ Adding hash function to generate predictable ids.

Signed-off-by: Nishant Arora <[email protected]>
  • Loading branch information
whizzzkid authored Sep 15, 2023
1 parent a91bd9a commit 92c55c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions add-on/src/lib/redirect-handler/blockOrObserve.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 92c55c3

Please sign in to comment.