From dabe851af6ccf32572bd74cc3a7993f05b4833c5 Mon Sep 17 00:00:00 2001 From: Norman Chan Date: Sun, 5 Jun 2022 17:07:22 -0400 Subject: [PATCH 1/2] Add synthetic nav to http driver --- pkg/drivers/http/page.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/drivers/http/page.go b/pkg/drivers/http/page.go index d3a93ce7..54fcfc94 100644 --- a/pkg/drivers/http/page.go +++ b/pkg/drivers/http/page.go @@ -2,9 +2,11 @@ package http import ( "context" - "github.com/MontFerret/ferret/pkg/runtime/events" "hash/fnv" + "github.com/MontFerret/ferret/pkg/runtime/events" + "github.com/MontFerret/ferret/pkg/stdlib/html" + "github.com/PuerkitoBio/goquery" "github.com/MontFerret/ferret/pkg/drivers" @@ -211,8 +213,20 @@ func (p *HTMLPage) WaitForFrameNavigation(_ context.Context, _ drivers.HTMLDocum return core.ErrNotSupported } -func (p *HTMLPage) Navigate(_ context.Context, _ values.String) error { - return core.ErrNotSupported +func (p *HTMLPage) Navigate(ctx context.Context, url values.String) error { + page, err := html.Open(ctx, url) + if err != nil { + return err + } + + pp := page.(*HTMLPage) + + p.document = pp.document + p.cookies = pp.cookies + p.frames = pp.frames + p.response = pp.response + + return nil } func (p *HTMLPage) NavigateBack(_ context.Context, _ values.Int) (values.Boolean, error) { From edfc6af9bff921712fd410243727addd9c95bd9f Mon Sep 17 00:00:00 2001 From: Norman Chan Date: Mon, 6 Jun 2022 16:43:45 -0400 Subject: [PATCH 2/2] Use http driver instead of stdlib --- pkg/drivers/http/driver.go | 4 ++-- pkg/drivers/http/page.go | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/drivers/http/driver.go b/pkg/drivers/http/driver.go index 805731d8..37c1a691 100644 --- a/pkg/drivers/http/driver.go +++ b/pkg/drivers/http/driver.go @@ -137,7 +137,7 @@ func (drv *Driver) Open(ctx context.Context, params drivers.Params) (drivers.HTM Headers: drivers.NewHTTPHeadersWith(resp.Header), } - return NewHTMLPage(doc, params.URL, r, cookies) + return NewHTMLPage(doc, params.URL, r, cookies, drv) } func (drv *Driver) Parse(_ context.Context, params drivers.ParseParams) (drivers.HTMLPage, error) { @@ -149,7 +149,7 @@ func (drv *Driver) Parse(_ context.Context, params drivers.ParseParams) (drivers return nil, errors.Wrap(err, "failed to parse a document") } - return NewHTMLPage(doc, "#blank", drivers.HTTPResponse{}, nil) + return NewHTMLPage(doc, "#blank", drivers.HTTPResponse{}, nil, drv) } func (drv *Driver) Close() error { diff --git a/pkg/drivers/http/page.go b/pkg/drivers/http/page.go index 54fcfc94..65f83337 100644 --- a/pkg/drivers/http/page.go +++ b/pkg/drivers/http/page.go @@ -5,7 +5,6 @@ import ( "hash/fnv" "github.com/MontFerret/ferret/pkg/runtime/events" - "github.com/MontFerret/ferret/pkg/stdlib/html" "github.com/PuerkitoBio/goquery" @@ -16,6 +15,7 @@ import ( ) type HTMLPage struct { + driver *Driver document *HTMLDocument cookies *drivers.HTTPCookies frames *values.Array @@ -27,6 +27,7 @@ func NewHTMLPage( url string, response drivers.HTTPResponse, cookies *drivers.HTTPCookies, + driver *Driver, ) (*HTMLPage, error) { doc, err := NewRootHTMLDocument(qdoc, url) @@ -39,6 +40,7 @@ func NewHTMLPage( p.cookies = cookies p.frames = nil p.response = response + p.driver = driver return p, nil } @@ -98,6 +100,7 @@ func (p *HTMLPage) Copy() core.Value { p.document.GetURL().String(), p.response, cookies, + p.driver, ) if err != nil { @@ -214,7 +217,9 @@ func (p *HTMLPage) WaitForFrameNavigation(_ context.Context, _ drivers.HTMLDocum } func (p *HTMLPage) Navigate(ctx context.Context, url values.String) error { - page, err := html.Open(ctx, url) + page, err := p.driver.Open(ctx, drivers.Params{ + URL: url.String(), + }) if err != nil { return err }