Skip to content

Instantly share code, notes, and snippets.

@jgontrum
Created March 6, 2021 11:05
Show Gist options
  • Save jgontrum/5a9060e40c7fc04c2c3bae3f1a9b28ad to your computer and use it in GitHub Desktop.
Save jgontrum/5a9060e40c7fc04c2c3bae3f1a9b28ad to your computer and use it in GitHub Desktop.
async renderPageToHtml(page: Page): Promise<string> {
const iframes = await page.$$("iframe");
for (const iframe of iframes) {
const frame = await iframe.contentFrame();
if (!frame) continue;
const context = await frame.executionContext();
const res = await context.evaluate(() => {
const el = document.querySelector("*");
if (el) return el.outerHTML;
});
if (res) {
await iframe.evaluate((a, res) => {
a.innerHTML = res;
}, res);
}
}
return await page.evaluate(() => new XMLSerializer().serializeToString(document));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment