-
Notifications
You must be signed in to change notification settings - Fork 38
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
base: master
Are you sure you want to change the base?
Conversation
screenshot.js
Outdated
const fileUrl = require('file-url'); | ||
const isUrl = require('is-url-superb'); | ||
|
||
module.exports = class Screenshot { |
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.
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?
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.
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() |
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.
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'); |
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.
fs.writeFileSync('google.com-1024x768.png', data);
@@ -51,38 +51,19 @@ Default: `false` | |||
|
|||
Crop to the set height. | |||
|
|||
##### delay |
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.
Why was this removed? We could use delay to manually delay the capturing after opening the url, no?
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.
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
.
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.
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 :).
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 would use this. For example, on my website, I load the "latest blog post" widget async, so it's loaded a second after DOMContentLoaded
.
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 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.
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.
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 |
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.
We could use page.addScriptTag(url)
.
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.
Yup. In conjunction with injectFile(path)
.
|
||
##### selector | ||
|
||
Type: `string` | ||
|
||
Capture a specific DOM element. | ||
|
||
##### css |
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.
Weird that we don't have a replacement for this... Should we open a new issue in Puppeteer?
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.
Yes, open an issue on Puppeteer. In the meantime, we can use page.addScriptTag(url)
to inject the CSS.
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.
We can probably do this in page.evaluate()
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.
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
.
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.
PR sent puppeteer/puppeteer#947
@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(); | ||
} |
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.
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.
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 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 => { |
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 wonder if we should just make the size
argument part of the options object with a good default.
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.
Yup, let's do that.
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 agree. And it would be pretty future proof for a foreseeable future. |
I'm super excited we can finally get rid of PhantomJS! 🙌 |
Now uses |
d350873
to
f31a09b
Compare
index.js
Outdated
const { | ||
cookies, crop, format, headers, height, hide, keepAlive, password, scale, | ||
script, selector, timeout, transparent, userAgent, username, width | ||
} = opts; |
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.
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.
By the way, should we consider renaming this to |
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; |
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 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);
Added a PR to Puppeteer to make injecting js and css much easier. puppeteer/puppeteer#996 |
What's left to do here? |
Probably some small things to edge out. I'll take a look in the weekend. |
Any news on this? I'd rather not have to fork to get around some PhantomJS wonkiness. |
I can help with this if needed! |
@cluk3 Help is most welcome. Open a PR based on this one. I'll review ;) |
Probably still needs some work:
script
optionstyle
optionpage
frompuppeteer
to userskeepAlive
option with a.close
method on the returned promise that lets users kill it manually (good when capturing many pages).