-
Notifications
You must be signed in to change notification settings - Fork 574
setHtml() and getHtml() API #112
setHtml() and getHtml() API #112
Conversation
@@ -114,6 +117,10 @@ export default class LocalRuntime { | |||
return scrollTo(this.client, x, y) | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the intent of this private handler is for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was following the same pattern as similar methods:
private async goto(url: string): Promise<void> { ... }
private async click(selector: string): Promise<void> { ... }
private async scrollTo<T>(x: number, y: number): Promise<void> { ... }
private async setHtml(html: string): Promise<void> { ... } // new method in this PR
src/api.ts
Outdated
@@ -142,6 +142,12 @@ export default class Chromeless<T extends any> implements Promise<T> { | |||
return this | |||
} | |||
|
|||
setDocumentContent(html: string): Chromeless<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit against naming public methods the same thing as their counterparts in CDP. It (in my head at least) feels like it "locks" us a bit to the implementation details of CPD as opposed to just offering a feature regardless of how it's implemented.
I'm not too strongly tied to this opinion, but wanted to gather folks' thoughts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely see your point. I'm open to other suggestions for names for this method. Perhaps setHTML()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do a setHtml()
then.. we need a getHtml()
, too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually do think there should be a getHtml()
method. That way, the example usage for setHtml()
could look more like this:
const html = await chromeless
.setHtml('<h1>Hello world!</h1>')
.getHtml()
console.log(html) // <html><head></head><body><h1>Hello world!</h1></body></html>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed setDocumentContent()
to setHtml()
in 1f9a300.
My next step is to implement getHtml()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, setHtml()
and getHtml()
are now implemented.
const html = await chromeless
.setHtml('<h1>Hello world!</h1>')
.getHtml()
console.log(html) // <html><head></head><body><h1>Hello world!</h1></body></html>
1811770
to
0e274ce
Compare
Looks like the |
Looks good from my side. If nothing speaks against it, we can merge |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation looks good to me. However, I have two documentation-related requests:
- please also update the API TOC in the project's main README
- please update the Unreleased section in the CHANGELOG. I think this qualifies as a notable addition. (Please mention this PR setHtml() and getHtml() API #112 and Add .html method #74)
cde8587
to
bee325a
Compare
@adieuadieu I have made your requested changes to the two markdown files. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seangransee thank you!
Thanks @seangransee! |
This implements an API at
setHtml()
to call Page.setDocumentContent, allowing the user to pass arbitrary HTML to Chrome instead of calling a URL.It also implements an API at
getHtml()
that allows the user to get the page's HTML.Example usage:
Running this logs the following content:
Another example of
getHtml()
, on an actual web page: