Skip to content

Instantly share code, notes, and snippets.

@parsibox
Created November 10, 2023 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsibox/a377be6b3e8e7fa8dce1873439d8592b to your computer and use it in GitHub Desktop.
Save parsibox/a377be6b3e8e7fa8dce1873439d8592b to your computer and use it in GitHub Desktop.
golang Screenshot
package main
import (
"context"
"io/ioutil"
"log"
"time"
"github.com/chromedp/chromedp"
)
func main() {
// Create a new context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// Navigate to the desired URL
err := chromedp.Run(ctx, chromedp.Navigate("https://erp.sjau.ac.ir/Hermes"))
if err != nil {
log.Fatal(err)
}
// Wait for the page to load
time.Sleep(10 * time.Second)
/*
var width int64
err = chromedp.Run(ctx,
chromedp.Evaluate(`window.innerWidth`, &width),
)
if err != nil {
log.Fatal(err)
}
var size int64
err = chromedp.Run(ctx, chromedp.Evaluate(`document.body.scrollHeight`, &size))
if err != nil {
log.Fatal(err)
}
fmt.Println(width)
fmt.Println(size)
*/
// Set the viewport size to the dimensions of the loaded page
err = chromedp.Run(ctx, chromedp.EmulateViewport(1500, 1300))
if err != nil {
log.Fatal(err)
}
// Capture a screenshot of the entire page
var buf []byte
err = chromedp.Run(ctx, chromedp.FullScreenshot(&buf, 100))
if err != nil {
log.Fatal(err)
}
// Save the screenshot to a file
err = ioutil.WriteFile("screenshot.png", buf, 0644)
if err != nil {
log.Fatal(err)
}
log.Println("Screenshot captured and saved to screenshot.png")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment