Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use beforeScreenshot parameter for intercepting XHR #210

Open
justinherter opened this issue Aug 11, 2023 · 1 comment
Open

Unable to use beforeScreenshot parameter for intercepting XHR #210

justinherter opened this issue Aug 11, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@justinherter
Copy link

justinherter commented Aug 11, 2023

It seems that beforeScreenshot (when provided) is called after rendering the provided html, and just before the screenshot as seen below. However we have limited control of the Page prior to rendering. If I move the beforeScreenshot call prior to the setContent, this enables us to do things like intercept HTTP/XHR requests.

  await page.setContent(screenshot.html, { waitUntil });
  const element = await page.$(screenshot.selector);
  if (!element) {
    throw Error("No element matches selector: " + screenshot.selector);
  }

  if (isFunction(beforeScreenshot)) {
    await beforeScreenshot(page);
  }

  const buffer = await element.screenshot({
    path: screenshot.output,
    type: screenshot.type,
    omitBackground: screenshot.transparent,
    encoding: screenshot.encoding,
    quality: screenshot.quality,
  });

I would like to propose the addition of a beforeRendering parameter that allows for more control of the page prior to rendering. This example intercepts XHR requests.

beforeRendering: (page: Page): void => {
  page.on('request', (interceptedRequest) => {
    if (interceptedRequest.url().endsWith('.jpg')) {
      interceptedRequest.respond({
        status: 200,
        contentType: 'image/jpeg',
        body: Buffer.from(localResource.content, 'base64'),
      });
    } 
    else { interceptedRequest.continue(); }
  });
};

If you are open to it i can submit a PR, as I would rather use this lib as built rather than forking it. Alternatively we could execute the beforeScreenshot prior to setting the content. Im not sure of the implications in doing so but it has proven the concept.
Thanks!

@frinyvonnick
Copy link
Owner

Hi @justinherter 👋 Thank you for opening this issue. I like the idea of a beforeRendering function 👍 Feel free to open a pull request 😄

@frinyvonnick frinyvonnick added the enhancement New feature or request label Aug 13, 2023
@justinherter justinherter changed the title Unable to use beforeScreenshot parameter for intercepting HXR Unable to use beforeScreenshot parameter for intercepting XHR Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants