2018-09-26 22:03:06 -04:00
|
|
|
package dynamic
|
|
|
|
|
|
|
|
import (
|
2018-10-08 20:20:40 -04:00
|
|
|
"bytes"
|
2018-09-26 22:03:06 -04:00
|
|
|
"context"
|
2018-11-12 14:53:36 -05:00
|
|
|
"errors"
|
2018-10-07 21:32:30 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/html/common"
|
2018-11-12 14:53:36 -05:00
|
|
|
"github.com/MontFerret/ferret/pkg/html/dynamic/eval"
|
2018-10-07 21:32:30 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/html/dynamic/events"
|
2018-09-26 22:03:06 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2018-10-08 20:20:40 -04:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2018-09-26 22:03:06 -04:00
|
|
|
"github.com/mafredri/cdp"
|
|
|
|
"github.com/mafredri/cdp/protocol/dom"
|
|
|
|
"github.com/mafredri/cdp/protocol/page"
|
2018-11-12 14:53:36 -05:00
|
|
|
"github.com/mafredri/cdp/protocol/runtime"
|
2018-09-26 22:03:06 -04:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-10-06 22:33:39 -04:00
|
|
|
"strings"
|
2018-09-26 22:03:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type batchFunc = func() error
|
|
|
|
|
|
|
|
func runBatch(funcs ...batchFunc) error {
|
|
|
|
eg := errgroup.Group{}
|
|
|
|
|
|
|
|
for _, f := range funcs {
|
|
|
|
eg.Go(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
return eg.Wait()
|
|
|
|
}
|
|
|
|
|
2018-10-11 12:39:03 -04:00
|
|
|
func getRootElement(ctx context.Context, client *cdp.Client) (*dom.GetDocumentReply, error) {
|
|
|
|
d, err := client.DOM.GetDocument(ctx, dom.NewGetDocumentArgs().SetDepth(1))
|
2018-10-07 21:23:36 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-11 12:39:03 -04:00
|
|
|
return nil, err
|
2018-10-07 21:23:36 -04:00
|
|
|
}
|
|
|
|
|
2018-10-11 12:39:03 -04:00
|
|
|
return d, nil
|
2018-10-07 21:23:36 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 22:03:06 -04:00
|
|
|
func parseAttrs(attrs []string) *values.Object {
|
|
|
|
var attr values.String
|
|
|
|
|
|
|
|
res := values.NewObject()
|
|
|
|
|
|
|
|
for _, el := range attrs {
|
2018-10-06 22:33:39 -04:00
|
|
|
el = strings.TrimSpace(el)
|
2018-09-26 22:03:06 -04:00
|
|
|
str := values.NewString(el)
|
|
|
|
|
|
|
|
if common.IsAttribute(el) {
|
|
|
|
attr = str
|
|
|
|
res.Set(str, values.EmptyString)
|
|
|
|
} else {
|
|
|
|
current, ok := res.Get(attr)
|
|
|
|
|
|
|
|
if ok {
|
2018-10-06 22:33:39 -04:00
|
|
|
if current.String() != "" {
|
|
|
|
res.Set(attr, current.(values.String).Concat(values.SpaceString).Concat(str))
|
|
|
|
} else {
|
|
|
|
res.Set(attr, str)
|
|
|
|
}
|
2018-09-26 22:03:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-10-11 12:39:03 -04:00
|
|
|
func loadInnerHTML(ctx context.Context, client *cdp.Client, id *HTMLElementIdentity) (values.String, error) {
|
2018-11-12 14:53:36 -05:00
|
|
|
var objID runtime.RemoteObjectID
|
2018-09-27 00:26:56 -04:00
|
|
|
|
2018-10-11 12:39:03 -04:00
|
|
|
if id.objectID != "" {
|
2018-11-12 14:53:36 -05:00
|
|
|
objID = id.objectID
|
2018-10-11 12:39:03 -04:00
|
|
|
} else if id.backendID > 0 {
|
2018-11-12 14:53:36 -05:00
|
|
|
repl, err := client.DOM.ResolveNode(ctx, dom.NewResolveNodeArgs().SetBackendNodeID(id.backendID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if repl.Object.ObjectID == nil {
|
|
|
|
return "", errors.New("unable to resolve node")
|
|
|
|
}
|
|
|
|
|
|
|
|
objID = *repl.Object.ObjectID
|
2018-10-11 12:39:03 -04:00
|
|
|
} else {
|
2018-11-12 14:53:36 -05:00
|
|
|
repl, err := client.DOM.ResolveNode(ctx, dom.NewResolveNodeArgs().SetNodeID(id.nodeID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if repl.Object.ObjectID == nil {
|
|
|
|
return "", errors.New("unable to resolve node")
|
|
|
|
}
|
|
|
|
|
|
|
|
objID = *repl.Object.ObjectID
|
2018-09-27 00:26:56 -04:00
|
|
|
}
|
|
|
|
|
2018-11-12 14:53:36 -05:00
|
|
|
res, err := eval.Property(ctx, client, objID, "innerHTML")
|
2018-10-08 20:20:40 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-11 12:39:03 -04:00
|
|
|
return "", err
|
2018-10-08 20:20:40 -04:00
|
|
|
}
|
|
|
|
|
2018-11-12 14:53:36 -05:00
|
|
|
return values.NewString(res.String()), err
|
2018-10-08 20:20:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseInnerText(innerHTML string) (values.String, error) {
|
|
|
|
buff := bytes.NewBuffer([]byte(innerHTML))
|
|
|
|
|
|
|
|
parsed, err := goquery.NewDocumentFromReader(buff)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewString(parsed.Text()), nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 12:39:03 -04:00
|
|
|
func createChildrenArray(nodes []dom.Node) []*HTMLElementIdentity {
|
|
|
|
children := make([]*HTMLElementIdentity, len(nodes))
|
2018-09-27 00:26:56 -04:00
|
|
|
|
|
|
|
for idx, child := range nodes {
|
2018-10-11 12:39:03 -04:00
|
|
|
children[idx] = &HTMLElementIdentity{
|
|
|
|
nodeID: child.NodeID,
|
|
|
|
backendID: child.BackendNodeID,
|
2018-09-26 22:03:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 12:39:03 -04:00
|
|
|
return children
|
2018-09-26 22:03:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func contextWithTimeout() (context.Context, context.CancelFunc) {
|
|
|
|
return context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForLoadEvent(ctx context.Context, client *cdp.Client) error {
|
|
|
|
loadEventFired, err := client.Page.LoadEventFired(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = loadEventFired.Recv()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadEventFired.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEventBroker(client *cdp.Client) (*events.EventBroker, error) {
|
2018-10-15 17:17:15 -04:00
|
|
|
var err error
|
|
|
|
var onLoad page.LoadEventFiredClient
|
|
|
|
var onReload dom.DocumentUpdatedClient
|
|
|
|
var onAttrModified dom.AttributeModifiedClient
|
|
|
|
var onAttrRemoved dom.AttributeRemovedClient
|
|
|
|
var onChildCountUpdated dom.ChildNodeCountUpdatedClient
|
|
|
|
var onChildNodeInserted dom.ChildNodeInsertedClient
|
|
|
|
var onChildNodeRemoved dom.ChildNodeRemovedClient
|
2018-09-26 22:03:06 -04:00
|
|
|
ctx := context.Background()
|
2018-10-15 17:17:15 -04:00
|
|
|
|
|
|
|
onLoad, err = client.Page.LoadEventFired(ctx)
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:17:15 -04:00
|
|
|
onReload, err = client.DOM.DocumentUpdated(ctx)
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-15 17:17:15 -04:00
|
|
|
onLoad.Close()
|
2018-09-26 22:03:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:17:15 -04:00
|
|
|
onAttrModified, err = client.DOM.AttributeModified(ctx)
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-15 17:17:15 -04:00
|
|
|
onLoad.Close()
|
|
|
|
onReload.Close()
|
2018-09-26 22:03:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:17:15 -04:00
|
|
|
onAttrRemoved, err = client.DOM.AttributeRemoved(ctx)
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-15 17:17:15 -04:00
|
|
|
onLoad.Close()
|
|
|
|
onReload.Close()
|
|
|
|
onAttrModified.Close()
|
2018-09-26 22:03:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:17:15 -04:00
|
|
|
onChildCountUpdated, err = client.DOM.ChildNodeCountUpdated(ctx)
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-15 17:17:15 -04:00
|
|
|
onLoad.Close()
|
|
|
|
onReload.Close()
|
|
|
|
onAttrModified.Close()
|
|
|
|
onAttrRemoved.Close()
|
2018-09-26 22:03:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:17:15 -04:00
|
|
|
onChildNodeInserted, err = client.DOM.ChildNodeInserted(ctx)
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-15 17:17:15 -04:00
|
|
|
onLoad.Close()
|
|
|
|
onReload.Close()
|
|
|
|
onAttrModified.Close()
|
|
|
|
onAttrRemoved.Close()
|
|
|
|
onChildCountUpdated.Close()
|
2018-09-26 22:03:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:17:15 -04:00
|
|
|
onChildNodeRemoved, err = client.DOM.ChildNodeRemoved(ctx)
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-15 17:17:15 -04:00
|
|
|
onLoad.Close()
|
|
|
|
onReload.Close()
|
|
|
|
onAttrModified.Close()
|
|
|
|
onAttrRemoved.Close()
|
|
|
|
onChildCountUpdated.Close()
|
|
|
|
onChildNodeInserted.Close()
|
2018-09-26 22:03:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:17:15 -04:00
|
|
|
broker := events.NewEventBroker(
|
|
|
|
onLoad,
|
|
|
|
onReload,
|
|
|
|
onAttrModified,
|
|
|
|
onAttrRemoved,
|
|
|
|
onChildCountUpdated,
|
|
|
|
onChildNodeInserted,
|
|
|
|
onChildNodeRemoved,
|
|
|
|
)
|
2018-09-26 22:03:06 -04:00
|
|
|
|
2018-10-15 17:17:15 -04:00
|
|
|
err = broker.Start()
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-15 17:17:15 -04:00
|
|
|
onLoad.Close()
|
|
|
|
onReload.Close()
|
|
|
|
onAttrModified.Close()
|
|
|
|
onAttrRemoved.Close()
|
|
|
|
onChildCountUpdated.Close()
|
|
|
|
onChildNodeInserted.Close()
|
|
|
|
onChildNodeRemoved.Close()
|
2018-09-26 22:03:06 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return broker, nil
|
|
|
|
}
|