Created
August 28, 2018 11:36
-
-
Save canhlinh/c1320fc2681f2b5ceac52557311baa2a to your computer and use it in GitHub Desktop.
screenshot golang osx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/base64" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"os/exec" | |
"sync" | |
chrome "github.com/mkenney/go-chrome/tot" | |
"github.com/mkenney/go-chrome/tot/emulation" | |
"github.com/mkenney/go-chrome/tot/page" | |
"github.com/mkenney/go-chrome/tot/socket" | |
) | |
func main() { | |
// Define a chrome instance with remote debugging enabled. | |
browser := chrome.New( | |
// See https://developers.google.com/web/updates/2017/04/headless-chrome#cli | |
// for details about startup flags | |
&chrome.Flags{ | |
"addr": "localhost", | |
"disable-extensions": nil, | |
"disable-gpu": nil, | |
"headless": nil, | |
"hide-scrollbars": nil, | |
"no-first-run": nil, | |
"no-sandbox": nil, | |
"port": 9222, | |
"remote-debugging-address": "0.0.0.0", | |
"remote-debugging-port": 9222, | |
}, | |
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", // Path to Chromeium binary | |
"/tmp", // Set the Chromium working directory | |
"/dev/null", // Ignore internal Chromium output, set to empty string for os.Stdout | |
"/dev/null", // Ignore internal Chromium errors, set to empty string for os.Stderr | |
) | |
// Start the chrome process. | |
if err := browser.Launch(); nil != err { | |
panic(err) | |
} | |
wg := &sync.WaitGroup{} | |
for i := 0; i < 10; i++ { | |
wg.Add(1) | |
p := os.TempDir() + fmt.Sprintf("%d", i) + "example.png" | |
go func() { | |
defer wg.Done() | |
// Open a tab and navigate to the URL you want to screenshot. | |
tab, err := browser.NewTab("https://d1u06kz0k4cl3m.cloudfront.net/04a327.svg") | |
if nil != err { | |
panic(err) | |
} | |
// Enable Page events for this tab. | |
if enableResult := <-tab.Page().Enable(); nil != enableResult.Err { | |
panic(enableResult.Err) | |
} | |
// Create a channel to receive the screenshot data generated by the | |
// event handler. | |
results := make(chan *page.CaptureScreenshotResult) | |
// Create an event handler that executes when the page load event is | |
// received. | |
loadEventHandler := socket.NewEventHandler( | |
"Page.loadEventFired", | |
// This function will generate a screenshot and write the data | |
// to the results channel. | |
func(response *socket.Response) { | |
// Set the device emulation parameters. | |
overrideResult := <-tab.Emulation().SetDeviceMetricsOverride( | |
&emulation.SetDeviceMetricsOverrideParams{ | |
Width: 1440, | |
Height: 1440, | |
ScreenOrientation: &emulation.ScreenOrientation{ | |
Type: emulation.OrientationType.PortraitPrimary, | |
Angle: 90, | |
}, | |
}, | |
) | |
if nil != overrideResult.Err { | |
panic(overrideResult.Err) | |
} | |
// Capture a screenshot of the current state of the current | |
// page. | |
screenshotResult := <-tab.Page().CaptureScreenshot( | |
&page.CaptureScreenshotParams{ | |
Format: page.Format.Png, | |
}, | |
) | |
if nil != screenshotResult.Err { | |
return | |
} | |
results <- screenshotResult | |
}, | |
) | |
// Add the handler to the stack for this tab. Multiple handlers can | |
// listen to the same event. | |
tab.AddEventHandler(loadEventHandler) | |
// Wait for the handler to fire | |
result := <-results | |
// Decode the base64 encoded image data | |
data, err := base64.StdEncoding.DecodeString(result.Data) | |
if nil != err { | |
panic(err) | |
} | |
// Write the generated image to a file | |
err = ioutil.WriteFile(p, data, 0644) | |
if nil != err { | |
panic(err) | |
} | |
exec.Command("open", p).Run() | |
fmt.Println("Finished rendering example.jpg") | |
}() | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment