From a2f1eb2810007b702a6813c8797a602175808128 Mon Sep 17 00:00:00 2001 From: mantou <709922234@qq.com> Date: Wed, 23 Feb 2022 10:51:45 +0800 Subject: [PATCH 1/2] Support ShadowDOM Use `getInnerHTML` --- src/lib/browser/Page.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib/browser/Page.ts b/src/lib/browser/Page.ts index f7ead86c..bcfa344e 100644 --- a/src/lib/browser/Page.ts +++ b/src/lib/browser/Page.ts @@ -271,7 +271,23 @@ export class BrowserPage { return await promiseWithTimeout( (async (): Promise => { const start = Date.now(); - const content = await this.#ref?.content(); + // `getInnerHTML` unstable: https://github.com/whatwg/html/issues/8867 + const content = await this.#ref?.evaluate(() => { + const doctype = document.doctype + ? new XMLSerializer().serializeToString(document.doctype) + : ''; + if (!('getInnerHTML' in Element.prototype)) { + return `${doctype}${document.documentElement.outerHTML}`; + } + const body = (document.body as any).getInnerHTML(); + const uid = crypto.randomUUID(); + document.body.innerHTML = uid; + return `${doctype}${document.documentElement.outerHTML.replace( + uid, + body + )}`; + }); + stats.timing('renderscript.renderBody', Date.now() - start, { browser: this.#engine as string, }); From 8d6104fc017e8e31762c5bbda7be67ddf90b43cd Mon Sep 17 00:00:00 2001 From: mantou <709922234@qq.com> Date: Fri, 12 Apr 2024 19:44:35 +0800 Subject: [PATCH 2/2] chore: use `getHTML` --- src/lib/browser/Page.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/browser/Page.ts b/src/lib/browser/Page.ts index bcfa344e..b96a84fc 100644 --- a/src/lib/browser/Page.ts +++ b/src/lib/browser/Page.ts @@ -271,15 +271,15 @@ export class BrowserPage { return await promiseWithTimeout( (async (): Promise => { const start = Date.now(); - // `getInnerHTML` unstable: https://github.com/whatwg/html/issues/8867 const content = await this.#ref?.evaluate(() => { const doctype = document.doctype ? new XMLSerializer().serializeToString(document.doctype) : ''; - if (!('getInnerHTML' in Element.prototype)) { + // https://html.spec.whatwg.org/#dom-parsing-and-serialization + if (!('getHTML' in Element.prototype)) { return `${doctype}${document.documentElement.outerHTML}`; } - const body = (document.body as any).getInnerHTML(); + const body = (document.body as any).getHTML({ serializableShadowRoots: true }); const uid = crypto.randomUUID(); document.body.innerHTML = uid; return `${doctype}${document.documentElement.outerHTML.replace(