Skip to content

Commit

Permalink
Prevent duplicate cmpDetected and popupFound for filterlists
Browse files Browse the repository at this point in the history
  • Loading branch information
muodov committed Nov 21, 2024
1 parent 4bb6ed9 commit 778a944
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
1 change: 1 addition & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ sequenceDiagram
Note right of CS: wait for DOMContentLoaded
opt if filterlist enabled and filterlist selectors matched
CS -->> BG: cmpDetected (with cmp: "filterList")
CS -->> BG: popupFound (with cmp: "filterList")
end
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export type LifecycleState =

export type ConsentState = {
cosmeticFiltersOn: boolean; // true if cosmetic filter rules are currently applied.
filterListReported: boolean; // true if the cosmetic filter list has been reported to the user.
lifecycle: LifecycleState; // What point in the autoconsent lifecycle this script is at.
prehideOn: boolean; // If the script is currently hiding preHide elements.
findCmpAttempts: number; // Number of times we tried to find CMPs in this frame.
Expand Down
51 changes: 30 additions & 21 deletions lib/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class AutoConsent {
foundCmp: AutoCMP = null;
state: ConsentState = {
cosmeticFiltersOn: false,
filterListReported: false,
lifecycle: 'loading',
prehideOn: false,
findCmpAttempts: 0,
Expand Down Expand Up @@ -476,17 +477,17 @@ export default class AutoConsent {
}

setTimeout(() => {
// this may be a false positive: sometimes filters hide unrelated elements that are not cookie pop-ups
const cosmeticFiltersWorked = this.domActions.elementVisible(getFilterlistSelectors(styles), 'any');
if (cosmeticFiltersWorked) {
logsConfig?.lifecycle && console.log('Prehide cosmetic filters matched', location.href);
this.sendContentMessage({
type: 'popupFound',
url: location.href,
cmp: 'filterList',
});
} else {
logsConfig?.lifecycle && console.log("Prehide cosmetic filters didn't match", location.href);
if (this.state.cosmeticFiltersOn && !this.state.filterListReported) {
// if the cosmetic filters are actually working, report the hidden popup to the background.
// This may still be overridden later if an autoconsent rule matches.
// this may be a false positive: sometimes filters hide unrelated elements that are not cookie pop-ups
const cosmeticFiltersWorked = this.domActions.elementVisible(getFilterlistSelectors(styles), 'any');
if (cosmeticFiltersWorked) {
logsConfig?.lifecycle && console.log('Prehide cosmetic filters matched', location.href);
this.reportFilterlist();
} else {
logsConfig?.lifecycle && console.log("Prehide cosmetic filters didn't match", location.href);
}
}
}, 1000);

Expand All @@ -508,6 +509,20 @@ export default class AutoConsent {
this.domActions.removeStyleSheet(this.cosmeticStyleSheet);
}

reportFilterlist() {
this.sendContentMessage({
type: 'cmpDetected',
url: location.href,
cmp: 'filterList',
});
this.sendContentMessage({
type: 'popupFound',
cmp: 'filterList',
url: location.href,
});
this.updateState({ filterListReported: true });
}

filterListFallback() {
if (!this.filtersEngine) {
this.updateState({ lifecycle: 'nothingDetected' });
Expand All @@ -530,16 +545,10 @@ export default class AutoConsent {
this.applyCosmeticFilters(cosmeticStyles); // do not wait for it to finish
logsConfig?.lifecycle && console.log('Keeping cosmetic filters', location.href);
this.updateState({ lifecycle: 'cosmeticFiltersDetected' });
this.sendContentMessage({
type: 'cmpDetected',
url: location.href,
cmp: 'filterList',
});
this.sendContentMessage({
type: 'popupFound',
cmp: 'filterList',
url: location.href,
});
if (!this.state.filterListReported) {
this.reportFilterlist();
}

this.sendContentMessage({
type: 'optOutResult',
cmp: 'filterList',
Expand Down

0 comments on commit 778a944

Please sign in to comment.