Skip to content

Commit

Permalink
Accept Cookie Banners if IP not in EU
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Friedli committed Mar 17, 2022
1 parent 993974f commit 66e5d04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ There are some env variables that mus be set.

`PUPPETEER_TIMEOUT`: Default `10000` Puppeteer default timeout time in ms

`COOKIE_BANNER_TIMEOUT`: Default `3000` Timeout to wait for Cookie Banner (does not show up in Switzerland 👾)

`BAKERY_LOG_LEVEL`: Default not set. Allows to define the log level as one from `trace, debug, info, warn, error and fatal (plus all and off)`.

# Run It
Expand Down
27 changes: 26 additions & 1 deletion helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const puppeterDefaultOpts = {
'--no-sandbox',
],
};
const cookieBannerSelector = '#onetrust-accept-btn-handler';
const cookieBannerTimeout = parseInt(process.env.COOKIE_BANNER_TIMEOUT, 10) || 3000;
const emailInputSelector = '#email';
const nextBtnSelector = '#first-step-continue-btn';
const passwordInputSelector = 'input[type=password]';
Expand All @@ -37,6 +39,25 @@ const requestOptions = {

const userAgent = new UserAgent();

/**
* Wait for cookie banner and accept all
* If not needed skip it
* @param {*} page
*/
async function acceptCookieBannerIfNeeded(page) {
try {
if (await page.waitForSelector(
cookieBannerSelector,
{ timeout: cookieBannerTimeout },
)) {
log.warn('accepting all cookies ');
await page.click(cookieBannerSelector);
}
} catch (e) {
log.warn('Cookie banner not found ', e);
}
}

/**
* register a new account using the provided credentials
* The registration process will be done using puppeteer
Expand All @@ -54,8 +75,10 @@ async function registerAccount(email, password) {

log.debug('go to ', profileUrl);
await page.goto(profileUrl);
await page.waitForSelector(emailInputSelector, visibleSelectorOption);

await acceptCookieBannerIfNeeded(page);

await page.waitForSelector(emailInputSelector, visibleSelectorOption);
log.debug('type email ', profileUrl);
await page.type(emailInputSelector, email);

Expand Down Expand Up @@ -100,6 +123,8 @@ async function confirmAccount(confirmUrl, email, password) {
log.debug('visit profile url ', confirmUrl);
await page.goto(profileUrl);

await acceptCookieBannerIfNeeded(page);

log.debug('type email for login ', email);
await page.waitForSelector(emailInputSelector, visibleSelectorOption);
await page.type(emailInputSelector, email);
Expand Down

0 comments on commit 66e5d04

Please sign in to comment.