Skip to content

Commit

Permalink
fix(galer): make outgoing requests captured (#142)
Browse files Browse the repository at this point in the history
* feat(galer): add `MergeSlices` func

Signed-off-by: Dwi Siswanto <[email protected]>

* fix(galer): make outgoing requests captured

Signed-off-by: Dwi Siswanto <[email protected]>

---------

Signed-off-by: Dwi Siswanto <[email protected]>
  • Loading branch information
dwisiswant0 authored Oct 24, 2024
1 parent d2f17f4 commit 68a88a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/galer/galer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func New(cfg *Config) *Config {

// Crawl to navigate to the URL & dump URLs on it
func (cfg *Config) Crawl(URL string) ([]string, error) {
var res []string
var res, reqs []string

if !IsURI(URL) {
return nil, errors.New("cannot parse URL")
Expand All @@ -46,17 +46,26 @@ func (cfg *Config) Crawl(URL string) ([]string, error) {
break
}

if !slices.Contains(res, url) {
res = append(res, url)
if url == URL {
break
}

if !slices.Contains(reqs, url) {
reqs = append(reqs, url)
}
}
})

err := chromedp.Run(ctx, chromedp.Navigate(URL), chromedp.Evaluate(script, &res))
err := chromedp.Run(ctx,
chromedp.Navigate(URL),
chromedp.Evaluate(script, &res),
)
if err != nil {
return nil, err
}

res = MergeSlices(res, reqs)

return res, nil
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/galer/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ func IsURI(s string) bool {

return true
}

// MergeSlices merges two slices of the same type into a
// single slice, removing duplicates.
func MergeSlices[T1 comparable, T2 []T1](v1, v2 T2) T2 {
uniq := make(map[T1]struct{})
for _, v := range v1 {
uniq[v] = struct{}{}
}

for v := range uniq {
v2 = append(v2, v)
}

return v2
}

0 comments on commit 68a88a5

Please sign in to comment.