Skip to content

Commit

Permalink
Make sure the heuristics are tested after a CMP is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
muodov committed Nov 29, 2024
1 parent 818b851 commit 267ee71
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,6 @@ export default class AutoConsent {
this.updateState({ findCmpAttempts: this.state.findCmpAttempts + 1 });
const foundCMPs: AutoCMP[] = [];

if (this.config.enableHeuristicDetection) {
const { patterns, snippets } = checkHeuristicPatterns();
if (
patterns.length > 0 &&
(patterns.length !== this.state.heuristicPatterns.length || this.state.heuristicPatterns.some((p, i) => p !== patterns[i]))
) {
logsConfig.lifecycle && console.log('Heuristic patterns found', patterns, snippets);
this.updateState({ heuristicPatterns: patterns, heuristicSnippets: snippets }); // we don't care about previously found patterns
}
}

for (const cmp of this.rules) {
try {
if (!cmp.checkRunContext()) {
Expand All @@ -258,6 +247,18 @@ export default class AutoConsent {
}
}

// to avoid race conditions, make sure to test for heuristic patterns only after all CMPs have been detected
if (this.config.enableHeuristicDetection) {
const { patterns, snippets } = checkHeuristicPatterns();
if (
patterns.length > 0 &&
(patterns.length !== this.state.heuristicPatterns.length || this.state.heuristicPatterns.some((p, i) => p !== patterns[i]))
) {
logsConfig.lifecycle && console.log('Heuristic patterns found', patterns, snippets);
this.updateState({ heuristicPatterns: patterns, heuristicSnippets: snippets }); // we don't care about previously found patterns
}
}

if (foundCMPs.length === 0 && retries > 0) {
await this.domActions.wait(500);
return this.findCmp(retries - 1);
Expand Down

0 comments on commit 267ee71

Please sign in to comment.