Support setting page content
OrKoN opened this issue · 5 comments
Puppeteer allows setting document content via https://pptr.dev/api/puppeteer.page.setcontent The implementation is based on document.open + document.write. With document.write being deprecated, it would good to offer a way to do this over WebDriver BiDi. CDP provides a method to do this https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-setDocumentContent
I am not quite convinced that would be a useful addition to the standard but I wonder if others have some feedback.
I guess the question is whether the semantics are different from document.open / document.write
and if so what they actually are. It seems somewhat unlikely that document.write
will be removed from the platform, so it seems OK to require that people who want to overwrite the current document in-place keep using that.
There are some observable side-effects in Chrome https://html.spec.whatwg.org/#document-written-scripts-intervention / https://chromestatus.com/feature/5718547946799104 E.g., users might get warning console message when using scripts in document.write.
Note that there is a strong hint that this API shouldn't be used:
https://html.spec.whatwg.org/#document.write()
The Browser Testing and Tools Working Group just discussed Support setting page content
.
The full IRC log of that discussion
<simonstewart> Topic: Support setting page content<simonstewart> github: https://github.com//issues/759
<simonstewart> simonstewart: why couldn't you just do this by executing JS?
<simonstewart> AutomatedTester: this is something that puppeteer has, but it depends on `document.write` which is deprecated.
<simonstewart> AutomatedTester: the only major use case is maybe for WPT, but I can't see ordinary testers wanting to do this.
<simonstewart> AutomatedTester: for component testing, people load the components into the page rather than just injecting it in because they need styling and additional content
<gsnedders> +1 for simonstewart's comment ("why couldn't you just do this by executing JS"); seems like it should be the same as navigating to about:blank and just calling document.open/write?
<simonstewart> jgraham: does puppeteer use this a lot?
<simonstewart> sadym: I'm not sure
<simonstewart> <debate about whether regular users would do this>
<simonstewart> simonstewart: what is replacing `document.write`?
<simonstewart> gsnedders: nothing. You don't want to modify the parser state while parsing
<simonstewart> AutomatedTester: what about navigating to a data URL?
<simonstewart> gsnedders: it may well have a different origin
<simonstewart> gsnedders: what are the semantics of this in CDP?
<simonstewart> gsnedders: do those semantics matter?
<simonstewart> simonstewart: could you use `document.outerHTML`?
<simonstewart> <general agreement that it's not the right thing to do>
<simonstewart> jgraham: does someone in the puppeteer team need this now, rather than just calling "executeScript"?
<simonstewart> sadym: how far is `document.write` from being removed?
<simonstewart> gsnedders: impossibly far away
<simonstewart> jgraham: it might be hooked up to some logging saying "this is deprecated, please stop", but implementations could handle that by treating it differently if it came from a sandbox
<simonstewart> jgraham: if we had a special command in gecko, it'd just be calling the JS
<simonstewart> sadym: I don't have a firm answer yet
<simonstewart> simonstewart: so there's general agreement this isn't something that needs to be done just yet?
<AutomatedTester> q?
<simonstewart> jgraham: yes, but if puppeteer come back and say it's a priority, no-one is against adding it
<AutomatedTester> RRSAgent: make minutes
<RRSAgent> I have made the request to generate https://www.w3.org/2024/09/27-webdriver-minutes.html AutomatedTester
AutomatedTester: what about navigating to a data URL?
gsnedders: it may well have a different origin
What about using a Blob instead? Would that have similar side-effects?
jgraham: yes, but if puppeteer come back and say it's a priority, no-one is against adding it
@jgraham, this request is coming from Playwright as P1 because it's used a lot. The current solution is to use script execution for now by using it from within a sandbox (which currently fails in Firefox, see https://bugzilla.mozilla.org/show_bug.cgi?id=1918288).
Once script execution will work across supported browsers maybe we could leave it that the client just calls the required steps within a single script evaluation call?