@cliqz/autoconsent
Version:
This is a library of rules for navigating through common consent popups on the web. These rules can be run in a Firefox webextension, or in a puppeteer orchestrated headless browser. Using these rules, opt-in and opt-out options can be selected automatica
62 lines (61 loc) • 1.76 kB
JavaScript
export default class TabConsent {
constructor(tab, ruleCheckPromise) {
this.tab = tab;
this.optOutStatus = null;
this.checked = ruleCheckPromise;
ruleCheckPromise.then(rule => this.rule = rule);
}
getCMPName() {
if (this.rule) {
return this.rule.name;
}
return null;
}
async isPopupOpen(retries = 1, interval = 1000) {
const isOpen = await this.rule.detectPopup(this.tab);
if (!isOpen && retries > 0) {
return new Promise((resolve) => setTimeout(() => resolve(this.isPopupOpen(retries - 1, interval)), interval));
}
return isOpen;
}
async doOptOut() {
try {
this.optOutStatus = await this.rule.optOut(this.tab);
return this.optOutStatus;
}
catch (e) {
this.optOutStatus = e;
throw e;
}
finally {
if (!this.rule.isHidingRule) {
if (this.getCMPName().startsWith('com_')) {
this.tab.wait(5000).then(() => this.tab.undoHideElements());
}
else {
await this.tab.undoHideElements();
}
}
}
}
async doOptIn() {
try {
return this.rule.optIn(this.tab);
}
finally {
if (!this.rule.isHidingRule) {
await this.tab.undoHideElements();
}
}
}
hasTest() {
return !!this.rule.hasSelfTest;
}
async testOptOutWorked() {
return this.rule.test(this.tab);
}
async applyCosmetics(selectors) {
const hidden = await this.tab.hideElements(selectors);
return hidden;
}
}