pressly / screenshot-nodejs Goto Github PK
View Code? Open in Web Editor NEWScreenshot as a service: GoogleChrome/puppeteer Node.js REST API server + Golang client
License: MIT License
Screenshot as a service: GoogleChrome/puppeteer Node.js REST API server + Golang client
License: MIT License
resp, body, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("Couldn't take png screenshot")
}
reader := bytes.NewReader(body)
You can pass resp.Body
(which is io.Reader itself) down to the callers right away.
resp, err := c.Do(req)
// return nil, error
// return resp.Body, nil
I noticed we're using node:8-slim
as a base image. That's fine, and will work great.
But I wonder, if we could potentially switch to
In golang, we usually put the client pkg into the top-level directory (unless the project is too big and has multiple clients, of course). Imho you can get rid of the lib/
subdirectory :)
If you wanted to implement CLI (a terminal program that downloads screenshots from a remote server), it'd live in /cmd/screenshot/main.go
.
When the client pkg is in the top-level directory, the imports are as easy as
import (
"github.com/pressly/screenshot"
)
func main() {
screenshot, _ := screenshot.New(...)
// ...
}
Uuups
https://github.com/pressly/screenshot/blob/master/screenshot
Can we remove the file from the history and force-push the master branch?
Error -
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Failed to launch chrome!
/app/node_modules/puppeteer/.local-chromium/linux-515411/chrome-linux/chrome: error while loading shared libraries: libgconf-2.so.4: cannot open shared object file: No such file or directory
Things are still working outside of docker.
Even the example puppeteer code on puppeteer's github doesn't seem to work, so it seems like a puppeteer / chromium issue. Can't figure out if it's just on my computer or if it's legit right now.
GET localhost:{port}/png?url=https://www.google.com&window=500x500&crop=75x80&x=350&y=190
localhost:{port}/png?window=500x500&crop=75x80&x=350&y=190&url=https://www.google.com?some=third&party=arguments
gonna work?url
argument to be the last one in all the examples?https://github.com/pressly/screenshot/blob/master/cmd/screenshot/main.go#L15-L39
var window [2]int
https://github.com/pressly/screenshot/blob/master/cmd/screenshot/main.go#L16-L18 with WindowWidth int
and WindowHeight int
on the ScreenshotOptions{}
structScreenshotOptions
to Page
or Options
?PdfOptions
- I'm fine with a single argument to PDF()
as long as the PdfOptions
embed the above ScreenshotOptions{}
io.ReadCloser
, so the consumer can close the BodyThis would allow us to get rid of the Node.js server wrapper. And we could write the whole service in Go only, effectively reimplement the Puppetteer.
Pros:
I'll look into feasibility of this option.
example github, amazon, many more
somehow setting headers on puppeteer breaks things - https://github.com/pressly/screenshot/blob/master/server/src/browser.ts#L37
will need to find a better way to forward headers
Create a Golang HTTP client for accessing the screenshot endpoints.
screenshot, err := screenshot.New("https://screenshot-api.example.com:8787")
/*io.Reader, error*/
pngBytesReader, err := screenshot.PNG(ctx, websiteUrl, screenshot.Page{/*Headers, Window, Crop, X, Y, WaitUntil */})
jpegBytesReader, err := screenshot.JPEG(ctx, websiteUrl, screenshot.Page{/*Headers, Window, Crop, X, Y, WaitUntil */}, 80 /*JPEG quality .. 0-100*/)
pdfBytesReader, err := screenshot.PDF(ctx, websiteUrl, screenshot.Page{/*Headers, Window, Crop, X, Y*/}, screenshot.PDFOptions{/*Format etc. */}) // see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
@MarcelRusu do you prefer a separate repository for client(s) and server?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.