puppeteer#Protocol TypeScript Examples

The following examples show how to use puppeteer#Protocol. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: puppeteer.ts    From epicgames-freegames-node with MIT License 6 votes vote down vote up
export function puppeteerCookieToToughCookieFileStore(
  puppetCookie: Protocol.Network.Cookie
): ToughCookieFileStore {
  const domain = puppetCookie.domain.replace(/^\./, '');
  const expires = new Date(puppetCookie.expires * 1000).toISOString();
  const { path, name } = puppetCookie;

  const tcfsCookie: ToughCookieFileStore = {
    [domain]: {
      [path]: {
        [name]: {
          key: name,
          value: puppetCookie.value,
          expires,
          domain,
          path,
          secure: puppetCookie.secure,
          httpOnly: puppetCookie.httpOnly,
          hostOnly: !puppetCookie.domain.startsWith('.'),
        },
      },
    },
  };
  return tcfsCookie;
}
Example #2
Source File: puppeteer.ts    From epicgames-freegames-node with MIT License 6 votes vote down vote up
export function puppeteerCookiesToToughCookieFileStore(
  puppetCookies: Protocol.Network.Cookie[]
): ToughCookieFileStore {
  const tcfs: ToughCookieFileStore = {};
  puppetCookies.forEach((puppetCookie) => {
    const temp = puppeteerCookieToToughCookieFileStore(puppetCookie);
    objectAssignDeep(tcfs, temp);
  });
  return tcfs;
}
Example #3
Source File: puppeteer.ts    From epicgames-freegames-node with MIT License 6 votes vote down vote up
export function toughCookieFileStoreToPuppeteerCookie(
  tcfs: ToughCookieFileStore
): Protocol.Network.CookieParam[] {
  const puppetCookies: Protocol.Network.CookieParam[] = [];
  Object.values(tcfs).forEach((domain) => {
    Object.values(domain).forEach((path) => {
      Object.values(path).forEach((tcfsCookie) => {
        puppetCookies.push({
          name: tcfsCookie.key,
          value: tcfsCookie.value,
          expires: tcfsCookie.expires ? new Date(tcfsCookie.expires).getTime() / 1000 : undefined,
          domain: `${!tcfsCookie.hostOnly ? '.' : ''}${tcfsCookie.domain}`,
          path: tcfsCookie.path,
          secure: tcfsCookie.secure,
          httpOnly: tcfsCookie.httpOnly,
          sameSite: 'Lax',
        });
      });
    });
  });
  return puppetCookies;
}
Example #4
Source File: request.ts    From epicgames-freegames-node with MIT License 6 votes vote down vote up
export function setPuppeteerCookies(username: string, newCookies: Protocol.Network.Cookie[]): void {
  const cookieJar = getCookieJar(username);
  newCookies.forEach((cookie) => {
    const domain = cookie.domain.replace(/^\./, '');
    const tcfsCookie = new tough.Cookie({
      key: cookie.name,
      value: cookie.value,
      expires: new Date(cookie.expires * 1000),
      domain,
      path: cookie.path,
      secure: cookie.secure,
      httpOnly: cookie.httpOnly,
      hostOnly: !cookie.domain.startsWith('.'),
    });
    try {
      cookieJar.setCookieSync(tcfsCookie, `https://${domain}`);
    } catch (err) {
      L.error({ tcfsCookie }, 'Error setting cookie');
      throw err;
    }
  });
}
Example #5
Source File: base.ts    From epicgames-freegames-node with MIT License 6 votes vote down vote up
protected async teardownPage(page: Page): Promise<void> {
    try {
      this.L.trace('Saving new cookies');
      const cdpClient = await page.target().createCDPSession();
      const currentUrlCookies = (await cdpClient.send('Network.getAllCookies')) as {
        cookies: Protocol.Network.Cookie[];
      };
      setPuppeteerCookies(this.email, currentUrlCookies.cookies);
      this.L.trace('Saved cookies, closing browser');
      await page.close();
    } catch (err) {
      await this.handlePageError(err, page);
    }
  }
Example #6
Source File: hcaptcha.ts    From epicgames-freegames-node with MIT License 6 votes vote down vote up
getCookieCache = async (): Promise<Protocol.Network.Cookie[] | null> => {
  try {
    await fs.access(HCAPTCHA_ACCESSIBILITY_CACHE_FILE, fs.constants.O_RDWR);
    const cookieData: Protocol.Network.Cookie[] = await fs.readJSON(
      HCAPTCHA_ACCESSIBILITY_CACHE_FILE
    );
    const cookieExpiryString = cookieData.find((c) => c.name === 'hc_accessibility')?.expires;
    if (!cookieExpiryString) return null;
    if (new Date(cookieExpiryString * 1000).getTime() < Date.now() + CACHE_BUFFER_MS) return null;
    return cookieData;
  } catch (err) {
    return null;
  }
}
Example #7
Source File: login.ts    From epicgames-freegames-node with MIT License 6 votes vote down vote up
async refreshLogin(page: Page): Promise<boolean> {
    await page.goto(STORE_CART_EN, {
      waitUntil: 'networkidle0',
    });
    const cdpClient = await page.target().createCDPSession();
    const currentUrlCookies = (await cdpClient.send('Network.getAllCookies')) as {
      cookies: Protocol.Network.Cookie[];
    };
    if (currentUrlCookies.cookies.find((c) => c.name === 'storeTokenExpires')) {
      return true;
    }
    return false;
  }
Example #8
Source File: puppet-account.ts    From epicgames-freegames-node with MIT License 6 votes vote down vote up
public async createAccount(): Promise<void> {
    this.L.info({ username: this.username, password: this.password, email: this.email });

    const hCaptchaCookies = await getHcaptchaCookies();
    const userCookies = await getCookiesRaw(this.email);
    const puppeteerCookies = toughCookieFileStoreToPuppeteerCookie(userCookies);
    this.L.debug('Logging in with puppeteer');
    const browser = await puppeteer.launch(launchArgs);
    const page = await browser.newPage();
    this.L.trace(getDevtoolsUrl(page));
    const cdpClient = await page.target().createCDPSession();
    await cdpClient.send('Network.setCookies', {
      cookies: [...puppeteerCookies, ...hCaptchaCookies],
    });
    await page.setCookie(...puppeteerCookies, ...hCaptchaCookies);
    await page.goto(
      `https://www.epicgames.com/id/register/epic?redirect_uri=${STORE_HOMEPAGE_EN}&client_id=${EPIC_CLIENT_ID}`,
      { waitUntil: 'networkidle0' }
    );
    await this.fillDOB(page);
    await this.fillSignUpForm(page);
    await this.handleCaptcha(page);
    await this.fillEmailVerificationForm(page);

    this.L.trace('Saving new cookies');
    const currentUrlCookies = (await cdpClient.send('Network.getAllCookies')) as {
      cookies: Protocol.Network.Cookie[];
    };
    await browser.close();
    setPuppeteerCookies(this.email, currentUrlCookies.cookies);
    this.L.info({ username: this.username, password: this.password, email: this.email });
  }
Example #9
Source File: hcaptcha.ts    From epicgames-freegames-node with MIT License 5 votes vote down vote up
setCookieCache = async (cookies: Protocol.Network.Cookie[]): Promise<void> => {
  await fs.writeJSON(HCAPTCHA_ACCESSIBILITY_CACHE_FILE, cookies);
}
Example #10
Source File: hcaptcha.ts    From epicgames-freegames-node with MIT License 5 votes vote down vote up
getHcaptchaCookies = async (): Promise<Protocol.Network.Cookie[]> => {
  const { hcaptchaAccessibilityUrl } = config;
  if (!hcaptchaAccessibilityUrl) {
    L.debug(
      'hcaptchaAccessibilityUrl not configured, captchas are less likely to be bypassed. Follow this guide to set it up: https://github.com/claabs/epicgames-freegames-node#hcaptcha-accessibility-cookies'
    );
    return [];
  }
  let cookieData = await getCookieCache();
  let browser;
  if (!cookieData) {
    try {
      L.debug('Setting hCaptcha accessibility cookies');
      browser = await safeLaunchBrowser(L);
      const page = await safeNewPage(browser, L);

      L.trace(getDevtoolsUrl(page));
      L.trace(`Navigating to ${hcaptchaAccessibilityUrl}`);
      await Promise.all([
        page.goto(hcaptchaAccessibilityUrl),
        page.waitForNavigation({ waitUntil: 'networkidle0' }),
      ]);
      L.trace(`Waiting for setAccessibilityCookie button`);
      const setCookieButton = (await page.waitForSelector(
        `button[data-cy='setAccessibilityCookie']:not([disabled])`
      )) as ElementHandle<HTMLButtonElement>;
      L.trace(`Clicking setAccessibilityCookie button`);
      await setCookieButton.click({ delay: 100 });
      try {
        const getCookieResp = await page.waitForResponse(
          (res) =>
            res.url() === 'https://accounts.hcaptcha.com/accessibility/get_cookie' &&
            res.request().method() === 'POST'
        );
        const getCookieStatus = getCookieResp.status();
        if (getCookieStatus !== 200) {
          const errorBody = await getCookieResp.json();
          L.debug(
            { status: getCookieStatus, errorBody },
            'Error from hCaptcha get_cookie request, continuing without hCaptcha accessibility cookies'
          );
        }
      } catch (err) {
        L.debug(err);
        L.warn(
          'No get cookie response recieved, continuing without hCaptcha accessibility cookies'
        );
      }
      L.trace(`Saving new cookies`);
      const cdpClient = await page.target().createCDPSession();
      const currentUrlCookies = (await cdpClient.send('Network.getAllCookies')) as {
        cookies: Protocol.Network.Cookie[];
      };
      await browser.close();
      cookieData = currentUrlCookies.cookies;
      await setCookieCache(cookieData);
    } catch (err) {
      L.warn(err);
      L.warn(
        'Setting the hCaptcha accessibility cookies encountered an error. Continuing without them...'
      );
      if (browser) await browser.close();
      return [];
    }
  }
  return cookieData;
}
Example #11
Source File: puppet-account.ts    From epicgames-freegames-node with MIT License 5 votes vote down vote up
public async deleteAccount(): Promise<void> {
    this.L.info({ email: this.email }, 'Deleting account');

    const hCaptchaCookies = await getHcaptchaCookies();
    const userCookies = await getCookiesRaw(this.email);
    const puppeteerCookies = toughCookieFileStoreToPuppeteerCookie(userCookies);
    this.L.debug('Logging in with puppeteer');
    const browser = await puppeteer.launch(launchArgs);
    const page = await browser.newPage();
    this.L.trace(getDevtoolsUrl(page));
    const cdpClient = await page.target().createCDPSession();
    await cdpClient.send('Network.setCookies', {
      cookies: [...puppeteerCookies, ...hCaptchaCookies],
    });
    await page.setCookie(...puppeteerCookies, ...hCaptchaCookies);
    await page.goto(`https://www.epicgames.com/account/personal`, { waitUntil: 'networkidle0' });
    this.L.trace('Waiting for deleteButton');
    const deleteButton = (await page.waitForXPath(
      `//button[contains(., 'Request Account Delete')]`
    )) as ElementHandle<HTMLButtonElement>;
    this.L.trace('Clicking deleteButton');
    await deleteButton.click({ delay: 100 });
    this.L.trace('Waiting for securityCodeInput');
    const securityCodeInput = (await page.waitForSelector(
      `input[name='security-code']`
    )) as ElementHandle<HTMLInputElement>;
    const code = await this.getActionVerification();
    this.L.trace('Filling securityCodeInput');
    await securityCodeInput.type(code);
    this.L.trace('Waiting for confirmButton');
    const confirmButton = (await page.waitForXPath(
      `//button[contains(., 'Confirm Delete Request')]`
    )) as ElementHandle<HTMLButtonElement>;
    this.L.trace('Clicking confirmButton');
    await confirmButton.click({ delay: 100 });
    this.L.trace('Waiting for skipSurveyButton');
    const skipSurveyButton = (await page.waitForSelector(
      `button#deletion-reason-skip`
    )) as ElementHandle<HTMLButtonElement>;
    this.L.trace('Clicking skipSurveyButton');
    await skipSurveyButton.click({ delay: 100 });
    await page.waitForSelector(`div.account-deletion-request-success-modal`);
    this.L.debug('Account deletion successful');

    this.L.trace('Saving new cookies');
    const currentUrlCookies = (await cdpClient.send('Network.getAllCookies')) as {
      cookies: Protocol.Network.Cookie[];
    };
    await browser.close();
    setPuppeteerCookies(this.email, currentUrlCookies.cookies);
  }