Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

setHtml() and getHtml() API #112

Merged
merged 7 commits into from
Aug 1, 2017

Conversation

seangransee
Copy link
Contributor

@seangransee seangransee commented Jul 31, 2017

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:

const { Chromeless } = require('chromeless')

async function run() {
  const chromeless = new Chromeless()

  const html = await chromeless
    .setHtml('<h1>Hello world!</h1>')
    .getHtml()

  console.log(html)

  await chromeless.end()
}

run().catch(console.error.bind(console))

Running this logs the following content:

<html><head></head><body><h1>Hello world!</h1></body></html>

Another example of getHtml(), on an actual web page:

chromeless
  .goto('http://www.google.com/')
  .getHtml()

@@ -114,6 +117,10 @@ export default class LocalRuntime {
return scrollTo(this.client, x, y)
}

Copy link
Contributor

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?

Copy link
Contributor Author

@seangransee seangransee Jul 31, 2017

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> {
Copy link
Contributor

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

Copy link
Contributor Author

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()?

Copy link
Collaborator

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? :trollface:

Copy link
Contributor Author

@seangransee seangransee Jul 31, 2017

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>

Copy link
Contributor Author

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().

Copy link
Contributor Author

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>

@seangransee seangransee force-pushed the set-document-content branch from 1811770 to 0e274ce Compare July 31, 2017 19:51
@seangransee seangransee changed the title setDocumentContent() API setHtml() API Jul 31, 2017
@seangransee seangransee changed the title setHtml() API setHtml() and getHtml() API Jul 31, 2017
@seangransee seangransee mentioned this pull request Jul 31, 2017
@seangransee
Copy link
Contributor Author

Looks like the getHtml() method I implemented here was requested in #74, so that issue can be closed if this PR gets merged.

@timsuchanek
Copy link
Contributor

Looks good from my side. If nothing speaks against it, we can merge

Copy link
Collaborator

@adieuadieu adieuadieu left a 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:

@seangransee seangransee force-pushed the set-document-content branch from cde8587 to bee325a Compare August 1, 2017 14:20
@seangransee
Copy link
Contributor Author

@adieuadieu I have made your requested changes to the two markdown files.

Copy link
Collaborator

@adieuadieu adieuadieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seangransee thank you!

@adieuadieu adieuadieu merged commit 6c2a4d5 into schickling:master Aug 1, 2017
@joelgriffith
Copy link
Contributor

Thanks @seangransee!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants