Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DCSG2900-ThreatTotal
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Johannes Barstad
DCSG2900-ThreatTotal
Commits
c48bc7d8
Commit
c48bc7d8
authored
2 years ago
by
Jonas Kjærandsen
Browse files
Options
Downloads
Patches
Plain Diff
Screenshot commenting and removal of unused code
parent
d14202b2
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
utils/screenShot.go
+5
-73
5 additions, 73 deletions
utils/screenShot.go
with
5 additions
and
73 deletions
utils/screenShot.go
+
5
−
73
View file @
c48bc7d8
...
...
@@ -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
),
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment