Skip to content
Snippets Groups Projects
Commit c48bc7d8 authored by Jonas Kjærandsen's avatar Jonas Kjærandsen
Browse files

Screenshot commenting and removal of unused code

parent d14202b2
No related branches found
No related tags found
No related merge requests found
...@@ -5,56 +5,15 @@ import ( ...@@ -5,56 +5,15 @@ import (
"log" "log"
"strings" "strings"
//"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
) )
/* // ScreenshotURL takes a url and returns a screenshot of the page found at the url using a headless chrome instance
func ScreenshotURL(url string, FrondendResponse *ResultFrontendResponse){
screenshot, cancel := chromedp.NewContext(context.Background())
defer cancel()
var SearchURL string
if strings.Contains(url, "https://") {
SearchURL = url
} else if strings.Contains(url, "http://") {
SearchURL = url
} else {
SearchURL = "https://" + url
}
var imageBuf []byte
if err := chromedp.Run(screenshot, screenshotTasks(SearchURL, &imageBuf)); err != nil {
log.Fatal(err)
}
FrondendResponse.Screenshot = imageBuf
if err := ioutil.WriteFile("screenshotTest", imageBuf, 9544); err != nil {
log.Fatal(err)
}
}
func screenshotTasks(url string, imageBuf *[]byte) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(url),
chromedp.ActionFunc(func(screenshot context.Context) (err error) {
*imageBuf, _, err = page.PrintToPDF().WithPrintBackground(false).Do(screenshot)
*imageBuf, _, err = page.
return err
}),
}
}
*/
func ScreenshotURL(url string, Response *ResultFrontendResponse) { func ScreenshotURL(url string, Response *ResultFrontendResponse) {
var SearchURL string var SearchURL string
// Checks that http or https is included in the search url, adds http if not
if strings.Contains(url, "https://") { if strings.Contains(url, "https://") {
SearchURL = url SearchURL = url
...@@ -74,34 +33,18 @@ func ScreenshotURL(url string, Response *ResultFrontendResponse) { ...@@ -74,34 +33,18 @@ func ScreenshotURL(url string, Response *ResultFrontendResponse) {
// capture screenshot of an element // capture screenshot of an element
var screenshotbuf []byte var screenshotbuf []byte
/* // Take the screenshot using the screenScreenshot function
if err := chromedp.Run(ctx, elementScreenshot(`https://pkg.go.dev/`, `img.Homepage-logo`, &screenshotbuf)); err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile("elementScreenshot.png", screenshotbuf, 0o644); err != nil {
log.Fatal(err)
}
*/
//////
// capture entire browser viewport, returning png with quality=90
/*if err := chromedp.Run(ctx, fullScreenshot(SearchURL, 90, &screenshotbuf)); err != nil {
log.Fatal(err)
}*/
if err := chromedp.Run(ctx, screenScreenshot(SearchURL, &screenshotbuf)); err != nil { if err := chromedp.Run(ctx, screenScreenshot(SearchURL, &screenshotbuf)); err != nil {
log.Fatal(err) log.Fatal(err)
} }
/*
if err := ioutil.WriteFile("fullScreenshot.png", screenshotbuf, 0o644); err != nil {
log.Fatal(err)
}*/
log.Printf("wrote elementScreenshot.png and fullScreenshot.png") log.Printf("Took a screenshot of a request url")
Response.Screenshot = screenshotbuf Response.Screenshot = screenshotbuf
} }
// Inspired by the emulation example at https://github.com/chromedp/examples/blob/master/emulate/main.go // Inspired by the emulation example at https://github.com/chromedp/examples/blob/master/emulate/main.go
// elementScreenshot takes a screenshot of a specific element. // elementScreenshot takes a screenshot of a specific element.
// The image is taken with a 4 by 3 aspect ratio and a resolution of 1280*960p
func screenScreenshot(urlstr string, res *[]byte) chromedp.Tasks { func screenScreenshot(urlstr string, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{ return chromedp.Tasks{
chromedp.EmulateViewport(1280, 960), chromedp.EmulateViewport(1280, 960),
...@@ -109,14 +52,3 @@ func screenScreenshot(urlstr string, res *[]byte) chromedp.Tasks { ...@@ -109,14 +52,3 @@ func screenScreenshot(urlstr string, res *[]byte) chromedp.Tasks {
chromedp.CaptureScreenshot(res), chromedp.CaptureScreenshot(res),
} }
} }
// fullScreenshot takes a screenshot of the entire browser viewport.
//
// Note: chromedp.FullScreenshot overrides the device's emulation settings. Use
// device.Reset to reset the emulation and viewport settings.
func fullScreenshot(urlstr string, quality int, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.FullScreenshot(res, quality),
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment