2018-09-18 22:42:38 +02:00
|
|
|
package browser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2018-09-24 03:11:13 +02:00
|
|
|
"github.com/mafredri/cdp"
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/mafredri/cdp/devtool"
|
2018-09-24 03:11:13 +02:00
|
|
|
"github.com/mafredri/cdp/protocol/target"
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/mafredri/cdp/rpcc"
|
2018-09-24 03:11:13 +02:00
|
|
|
"github.com/mafredri/cdp/session"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"sync"
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
2018-09-24 03:11:13 +02:00
|
|
|
type CdpDriver struct {
|
|
|
|
sync.Mutex
|
|
|
|
dev *devtool.DevTools
|
|
|
|
conn *rpcc.Conn
|
|
|
|
client *cdp.Client
|
|
|
|
session *session.Manager
|
|
|
|
contextID target.BrowserContextID
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-24 03:11:13 +02:00
|
|
|
func NewDriver(address string) *CdpDriver {
|
|
|
|
drv := new(CdpDriver)
|
|
|
|
drv.dev = devtool.New(address)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-24 03:11:13 +02:00
|
|
|
return drv
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (drv *CdpDriver) GetDocument(ctx context.Context, url string) (values.HtmlNode, error) {
|
2018-09-24 03:11:13 +02:00
|
|
|
err := drv.init(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, DefaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-09-24 03:11:13 +02:00
|
|
|
// Create a new target belonging to the browser context, similar
|
|
|
|
// to opening a new tab in an incognito window.
|
|
|
|
createTargetArgs := target.NewCreateTargetArgs(url).SetBrowserContextID(drv.contextID)
|
|
|
|
createTarget, err := drv.client.Target.CreateTarget(ctx, createTargetArgs)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-24 03:11:13 +02:00
|
|
|
// Connect to target using the existing websocket connection.
|
|
|
|
conn, err := drv.session.Dial(ctx, createTarget.TargetID)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-25 23:58:57 +02:00
|
|
|
return LoadHtmlDocument(ctx, conn, url)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (drv *CdpDriver) Close() error {
|
2018-09-24 03:11:13 +02:00
|
|
|
drv.Lock()
|
|
|
|
defer drv.Unlock()
|
|
|
|
|
|
|
|
if drv.session != nil {
|
|
|
|
drv.session.Close()
|
|
|
|
|
|
|
|
return drv.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (drv *CdpDriver) init(ctx context.Context) error {
|
|
|
|
drv.Lock()
|
|
|
|
defer drv.Unlock()
|
|
|
|
|
|
|
|
if drv.session == nil {
|
|
|
|
ver, err := drv.dev.Version(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to initialize driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
bconn, err := rpcc.DialContext(ctx, ver.WebSocketDebuggerURL)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to initialize driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
bc := cdp.NewClient(bconn)
|
|
|
|
|
|
|
|
sess, err := session.NewManager(bc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
bconn.Close()
|
|
|
|
|
|
|
|
return errors.Wrap(err, "failed to initialize driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
createCtx, err := bc.Target.CreateBrowserContext(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
bconn.Close()
|
|
|
|
sess.Close()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
drv.conn = bconn
|
|
|
|
drv.client = bc
|
|
|
|
drv.session = sess
|
|
|
|
drv.contextID = createCtx.BrowserContextID
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
return nil
|
|
|
|
}
|