diff --git a/utils/screenShot.go b/utils/screenShot.go index e536ed91d69561cb4445bb09f34bcc72fae88d5d..1793c08a03c18c9519989fdb70f0359965eed12d 100644 --- a/utils/screenShot.go +++ b/utils/screenShot.go @@ -5,56 +5,15 @@ import ( "log" "strings" - //"github.com/chromedp/cdproto/page" "github.com/chromedp/chromedp" ) -/* -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 - }), - } -} - -*/ +// ScreenshotURL takes a url and returns a screenshot of the page found at the url using a headless chrome instance func ScreenshotURL(url string, Response *ResultFrontendResponse) { var SearchURL string + // Checks that http or https is included in the search url, adds http if not if strings.Contains(url, "https://") { SearchURL = url @@ -74,34 +33,18 @@ func ScreenshotURL(url string, Response *ResultFrontendResponse) { // capture screenshot of an element var screenshotbuf []byte - /* - 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) - }*/ + // Take the screenshot using the screenScreenshot function if err := chromedp.Run(ctx, screenScreenshot(SearchURL, &screenshotbuf)); err != nil { 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 } // Inspired by the emulation example at https://github.com/chromedp/examples/blob/master/emulate/main.go // 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 { return chromedp.Tasks{ chromedp.EmulateViewport(1280, 960), @@ -109,14 +52,3 @@ func screenScreenshot(urlstr string, res *[]byte) chromedp.Tasks { 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), - } -}