Skip to content
Snippets Groups Projects

Merge react-branch into main.

Merged Jonas Kjærandsen requested to merge React-Branch into main
1 file
+ 39
40
Compare changes
  • Side-by-side
  • Inline
+ 39
40
package utils
package utils
import (
import (
"strings"
"context"
"context"
"io/ioutil"
"log"
"log"
 
"strings"
//"github.com/chromedp/cdproto/page"
//"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp"
@@ -14,7 +13,7 @@ import (
@@ -14,7 +13,7 @@ import (
func ScreenshotURL(url string, FrondendResponse *ResultFrontendResponse){
func ScreenshotURL(url string, FrondendResponse *ResultFrontendResponse){
screenshot, cancel := chromedp.NewContext(context.Background())
screenshot, cancel := chromedp.NewContext(context.Background())
defer cancel()
defer cancel()
var SearchURL string
var SearchURL string
if strings.Contains(url, "https://") {
if strings.Contains(url, "https://") {
@@ -22,7 +21,7 @@ func ScreenshotURL(url string, FrondendResponse *ResultFrontendResponse){
@@ -22,7 +21,7 @@ func ScreenshotURL(url string, FrondendResponse *ResultFrontendResponse){
} else if strings.Contains(url, "http://") {
} else if strings.Contains(url, "http://") {
SearchURL = url
SearchURL = url
} else {
} else {
SearchURL = "https://" + url
SearchURL = "https://" + url
}
}
@@ -52,50 +51,50 @@ func screenshotTasks(url string, imageBuf *[]byte) chromedp.Tasks {
@@ -52,50 +51,50 @@ func screenshotTasks(url string, imageBuf *[]byte) chromedp.Tasks {
}
}
*/
*/
func ScreenshotURL(url string, Response *ResultFrontendResponse){
func ScreenshotURL(url string, Response *ResultFrontendResponse) {
 
 
var SearchURL string
var SearchURL string
if strings.Contains(url, "https://") {
if strings.Contains(url, "https://") {
SearchURL = url
SearchURL = url
} else if strings.Contains(url, "http://") {
} else if strings.Contains(url, "http://") {
SearchURL = url
SearchURL = url
} else {
} else {
SearchURL = "https://" + url
SearchURL = "https://" + url
}
}
ctx, cancel := chromedp.NewContext(
ctx, cancel := chromedp.NewContext(
context.Background(),
context.Background(),
// chromedp.WithDebugf(log.Printf),
// chromedp.WithDebugf(log.Printf),
)
)
defer cancel()
defer cancel()
// 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
// capture screenshot of an element
if err := chromedp.Run(ctx, fullScreenshot(SearchURL, 90, &screenshotbuf)); err != nil {
var screenshotbuf []byte
log.Fatal(err)
}
/*
if err := ioutil.WriteFile("fullScreenshot.png", screenshotbuf, 0o644); err != nil {
if err := chromedp.Run(ctx, elementScreenshot(`https://pkg.go.dev/`, `img.Homepage-logo`, &screenshotbuf)); err != nil {
log.Fatal(err)
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 := ioutil.WriteFile("fullScreenshot.png", screenshotbuf, 0o644); err != nil {
 
log.Fatal(err)
 
}*/
log.Printf("wrote elementScreenshot.png and fullScreenshot.png")
log.Printf("wrote elementScreenshot.png and fullScreenshot.png")
Response.Screenshot = screenshotbuf
Response.Screenshot = screenshotbuf
}
}
// fullScreenshot takes a screenshot of the entire browser viewport.
// fullScreenshot takes a screenshot of the entire browser viewport.
@@ -103,8 +102,8 @@ Response.Screenshot = screenshotbuf
@@ -103,8 +102,8 @@ Response.Screenshot = screenshotbuf
// Note: chromedp.FullScreenshot overrides the device's emulation settings. Use
// Note: chromedp.FullScreenshot overrides the device's emulation settings. Use
// device.Reset to reset the emulation and viewport settings.
// device.Reset to reset the emulation and viewport settings.
func fullScreenshot(urlstr string, quality int, res *[]byte) chromedp.Tasks {
func fullScreenshot(urlstr string, quality int, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.Navigate(urlstr),
chromedp.FullScreenshot(res, quality),
chromedp.FullScreenshot(res, quality),
 
}
}
}
}
\ No newline at end of file
Loading