Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace PhantomJS with Puppeteer #46

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Replace PhantomJS with Puppeteer #46

wants to merge 10 commits into from

Conversation

kevva
Copy link
Owner

@kevva kevva commented Sep 24, 2017

Probably still needs some work:

  • Make sure we handle errors
  • Add script option
  • Add style option
  • Add some sort of hook that exposes page from puppeteer to users
  • Add keepAlive option with a .close method on the returned promise that lets users kill it manually (good when capturing many pages).
  • Code improvements?

screenshot.js Outdated
const fileUrl = require('file-url');
const isUrl = require('is-url-superb');

module.exports = class Screenshot {
Copy link
Owner Author

@kevva kevva Sep 24, 2017

Choose a reason for hiding this comment

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

Does this class makes sense? I did it so that I wouldn't have to pass around page everywhere if I kept it functional. If we keep it, should we export it too maybe?

Copy link
Collaborator

@SamVerschueren SamVerschueren left a comment

Choose a reason for hiding this comment

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

Haven't reviewed everything yet, don't have time now but already gave some remarks.

index.js Outdated
}

return puppeteer.launch()
.then(browser => browser.newPage()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add an extra ) at the end

readme.md Outdated

stream.pipe(fs.createWriteStream('google.com-1024x768.png'));
screenshot('http://google.com', '1024x768', {crop: true}).then(data => {
fs.writeFileSync('google.com-1024x768.png');
Copy link
Collaborator

Choose a reason for hiding this comment

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

fs.writeFileSync('google.com-1024x768.png', data);

@@ -51,38 +51,19 @@ Default: `false`

Crop to the set height.

##### delay
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why was this removed? We could use delay to manually delay the capturing after opening the url, no?

Copy link
Owner Author

@kevva kevva Sep 24, 2017

Choose a reason for hiding this comment

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

This was added as a hack to wait for elements or other stuff to finish. We do this automatically now. Although there probably is some option for this in puppeteer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It could be used by users as well to make sure an animation has ended. If you know a certain animation takes 2 seconds, someone could use delay: 2 to make sure he captures after the animation took place.

I never used it to be honest, but just finding a use case :).

Copy link
Contributor

@sindresorhus sindresorhus Sep 25, 2017

Choose a reason for hiding this comment

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

I would use this. For example, on my website, I load the "latest blog post" widget async, so it's loaded a second after DOMContentLoaded.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I think we should switch to a wait/waitFor option instead that takes a selector, a number or a function and uses this method https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagewaitforselectororfunctionortimeout-options-args.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, good idea, but I also like to keep the delay. Sometimes I just want a screenshot of a random website and ensure it’s fully loaded first.


Apply custom CSS to the webpage. Specify some CSS or the path to a CSS file.

##### script
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could use page.addScriptTag(url).

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yup. In conjunction with injectFile(path).


##### selector

Type: `string`

Capture a specific DOM element.

##### css
Copy link
Collaborator

Choose a reason for hiding this comment

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

Weird that we don't have a replacement for this... Should we open a new issue in Puppeteer?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, open an issue on Puppeteer. In the meantime, we can use page.addScriptTag(url) to inject the CSS.

Copy link
Owner Author

Choose a reason for hiding this comment

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

We can probably do this in page.evaluate() too.

Copy link
Collaborator

@SamVerschueren SamVerschueren Sep 27, 2017

Choose a reason for hiding this comment

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

Filed an issue puppeteer/puppeteer#892.

Just tested things out, we could do something like this with page.evaluate.

const headHandle = await page.$('head');

const result = await page.evaluate(head => {
 head.innerHTML += '<style>body { color: red; }</style>';
}, headHandle);

If css is a file path, we could just read the data with fs.readFile and pass in the contents with page.evaluate until, maybe in the future, we can replace that with a addStyleTag.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@sindresorhus
Copy link
Contributor

sindresorhus commented Sep 24, 2017

@kevva What do you think about targeting Node.js 8? So we can use async/await. Would be so much nicer. It's going to be a major release anyway. I plan to do that in the next major of Pageres.

screenshot.js Outdated
setHeaders(headers) {
if (typeof headers !== 'object') {
return Promise.resolve();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't make sense to make this optional (Same with the other methods). If a user calls setHeaders() they should be expected to provide some.

Copy link
Owner Author

@kevva kevva Sep 24, 2017

Choose a reason for hiding this comment

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

I don't disagree. I mainly did this for internal use to keep the code as clean as possible. If we target Node.js 8 I think we can get rid of the class altogether. I don't see why users would need these methods in this module.

const stream = screenshot('http://google.com', '1024x768', {crop: true});

stream.pipe(fs.createWriteStream('google.com-1024x768.png'));
screenshot('http://google.com', '1024x768', {crop: true}).then(data => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should just make the size argument part of the options object with a good default.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yup, let's do that.

Copy link
Collaborator

Choose a reason for hiding this comment

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

👌

@kevva
Copy link
Owner Author

kevva commented Sep 24, 2017

@kevva What do you think about targeting Node.js 8? So we can use async/await. Would be so much nicer. It's going to be a major release anyway. I plan to do that in the next major of Pageres.

👍 I agree. And it would be pretty future proof for a foreseeable future.

@sindresorhus
Copy link
Contributor

I'm super excited we can finally get rid of PhantomJS! 🙌

@kevva
Copy link
Owner Author

kevva commented Sep 24, 2017

Now uses async/await. Some tests are still failing though.

@kevva kevva force-pushed the puppeteer branch 2 times, most recently from d350873 to f31a09b Compare September 24, 2017 22:44
index.js Outdated
const {
cookies, crop, format, headers, height, hide, keepAlive, password, scale,
script, selector, timeout, transparent, userAgent, username, width
} = opts;
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels like overusing destructuring just for the sake of it. And using opts.cookies in the code makes it more explicit it's coming from a user option.

@SamVerschueren
Copy link
Collaborator

By the way, should we consider renaming this to screenshot instead of screenshot-stream, it's not a stream anymore :p.

@kevva
Copy link
Owner Author

kevva commented Oct 3, 2017

By the way, should we consider renaming this to screenshot instead of screenshot-stream, it's not a stream anymore :p.

If we can get the name, yes. But it hasn't been updated in five years so shouldn't be impossible.

index.js Outdated
await page.goto(uri, opts);

if (script) {
const fn = isUrl(script) ? page.addScriptTag : script.endsWith('.js') ? page.injectFile : page.evaluate;
Copy link
Collaborator

@SamVerschueren SamVerschueren Oct 4, 2017

Choose a reason for hiding this comment

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

I wonder if we could write this line like this

const fn = isUrl(script) || script.endsWith('.js') ? page.addScriptTag : page.evaluate;

A script tag works for a local file as well right? If not, I think we should wrap the second block in parentheses for readability.

const fn = isUrl(script) ? page.addScriptTag : (script.endsWith('.js') ? page.injectFile : page.evaluate);

@SamVerschueren
Copy link
Collaborator

Added a PR to Puppeteer to make injecting js and css much easier. puppeteer/puppeteer#996

@sindresorhus
Copy link
Contributor

What's left to do here?

@kevva
Copy link
Owner Author

kevva commented Mar 9, 2018

Probably some small things to edge out. I'll take a look in the weekend.

@c0bra
Copy link

c0bra commented Apr 30, 2018

Any news on this? I'd rather not have to fork to get around some PhantomJS wonkiness.

@cluk3
Copy link

cluk3 commented Oct 28, 2018

I can help with this if needed!

@sindresorhus
Copy link
Contributor

@cluk3 Help is most welcome. Open a PR based on this one. I'll review ;)

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

Successfully merging this pull request may close these issues.

None yet

5 participants