2019-02-20 01:10:18 +02:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
|
|
|
)
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func GetInPage(ctx context.Context, page drivers.HTMLPage, path []core.Value) (core.Value, error) {
|
2019-05-03 23:10:34 +02:00
|
|
|
if len(path) == 0 {
|
2019-06-19 23:58:56 +02:00
|
|
|
return page, nil
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
segment := path[0]
|
|
|
|
|
|
|
|
if segment.Type() == types.String {
|
|
|
|
segment := segment.(values.String)
|
|
|
|
|
|
|
|
switch segment {
|
2019-06-19 23:58:56 +02:00
|
|
|
case "mainFrame", "document":
|
|
|
|
return GetInDocument(ctx, page.GetMainFrame(), path[1:])
|
|
|
|
case "frames":
|
|
|
|
if len(path) == 1 {
|
|
|
|
return page.GetFrames(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := path[1]
|
|
|
|
|
|
|
|
if !values.IsNumber(idx) {
|
|
|
|
return values.None, core.TypeError(idx.Type(), types.Int, types.Float)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := page.GetFrame(ctx, values.ToInt(idx))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(path) == 2 {
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
frame, err := drivers.ToDocument(value)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetInDocument(ctx, frame, path[2:])
|
2019-02-20 01:10:18 +02:00
|
|
|
case "url", "URL":
|
2019-06-19 23:58:56 +02:00
|
|
|
return page.GetMainFrame().GetURL(), nil
|
2019-03-16 01:59:05 +02:00
|
|
|
case "cookies":
|
|
|
|
if len(path) == 1 {
|
2019-06-19 23:58:56 +02:00
|
|
|
return page.GetCookies(ctx)
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch idx := path[1].(type) {
|
|
|
|
case values.Int:
|
2019-06-19 23:58:56 +02:00
|
|
|
cookies, err := page.GetCookies(ctx)
|
2019-03-16 01:59:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cookies.Get(idx), nil
|
|
|
|
default:
|
|
|
|
return values.None, core.TypeError(idx.Type(), types.Int)
|
|
|
|
}
|
2019-06-19 23:58:56 +02:00
|
|
|
case "isClosed":
|
|
|
|
return page.IsClosed(), nil
|
|
|
|
case "title":
|
|
|
|
return page.GetMainFrame().GetTitle(), nil
|
|
|
|
default:
|
|
|
|
return GetInDocument(ctx, page.GetMainFrame(), path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetInDocument(ctx, page.GetMainFrame(), path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetInDocument(ctx context.Context, doc drivers.HTMLDocument, path []core.Value) (core.Value, error) {
|
|
|
|
if len(path) == 0 {
|
|
|
|
return doc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
segment := path[0]
|
|
|
|
|
|
|
|
if segment.Type() == types.String {
|
|
|
|
segment := segment.(values.String)
|
|
|
|
|
|
|
|
switch segment {
|
|
|
|
case "url", "URL":
|
|
|
|
return doc.GetURL(), nil
|
|
|
|
case "title":
|
|
|
|
return doc.GetTitle(), nil
|
|
|
|
case "parent":
|
|
|
|
parent := doc.GetParentDocument()
|
|
|
|
|
|
|
|
if parent == nil {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(path) == 1 {
|
|
|
|
return parent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetInDocument(ctx, parent, path[1:])
|
|
|
|
case "body", "head":
|
|
|
|
out := doc.QuerySelector(ctx, segment)
|
|
|
|
|
|
|
|
if out == values.None {
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(path) == 1 {
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
el, err := drivers.ToElement(out)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetInElement(ctx, el, path[1:])
|
2019-02-20 01:10:18 +02:00
|
|
|
default:
|
2019-06-19 23:58:56 +02:00
|
|
|
return GetInNode(ctx, doc.GetElement(), path)
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return GetInNode(ctx, doc.GetElement(), path)
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetInElement(ctx context.Context, el drivers.HTMLElement, path []core.Value) (core.Value, error) {
|
2019-05-03 23:10:34 +02:00
|
|
|
if len(path) == 0 {
|
2019-06-19 23:58:56 +02:00
|
|
|
return el, nil
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
segment := path[0]
|
|
|
|
|
|
|
|
if segment.Type() == types.String {
|
|
|
|
segment := segment.(values.String)
|
|
|
|
|
|
|
|
switch segment {
|
|
|
|
case "innerText":
|
2019-06-19 23:58:56 +02:00
|
|
|
return el.GetInnerText(ctx), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "innerHTML":
|
2019-06-19 23:58:56 +02:00
|
|
|
return el.GetInnerHTML(ctx), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "value":
|
2019-02-21 04:24:05 +02:00
|
|
|
return el.GetValue(ctx), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "attributes":
|
2019-02-26 04:46:39 +02:00
|
|
|
attrs := el.GetAttributes(ctx)
|
|
|
|
|
|
|
|
if len(path) == 1 {
|
|
|
|
return attrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.GetIn(ctx, attrs, path[1:])
|
2019-03-13 20:51:30 +02:00
|
|
|
case "style":
|
|
|
|
styles, err := el.GetStyles(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(path) == 1 {
|
|
|
|
return styles, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.GetIn(ctx, styles, path[1:])
|
2019-02-20 01:10:18 +02:00
|
|
|
default:
|
|
|
|
return GetInNode(ctx, el, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetInNode(ctx, el, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetInNode(ctx context.Context, node drivers.HTMLNode, path []core.Value) (core.Value, error) {
|
2019-05-03 23:10:34 +02:00
|
|
|
if len(path) == 0 {
|
2019-06-19 23:58:56 +02:00
|
|
|
return node, nil
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nt := node.Type()
|
|
|
|
segment := path[0]
|
|
|
|
st := segment.Type()
|
|
|
|
|
2019-02-26 04:46:39 +02:00
|
|
|
switch st {
|
|
|
|
case types.Int:
|
2019-02-20 01:10:18 +02:00
|
|
|
if nt == drivers.HTMLElementType || nt == drivers.HTMLDocumentType {
|
|
|
|
re := node.(drivers.HTMLNode)
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
return re.GetChildNode(ctx, segment.(values.Int)), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 04:46:39 +02:00
|
|
|
return values.GetIn(ctx, node, path[1:])
|
|
|
|
case types.String:
|
2019-02-20 01:10:18 +02:00
|
|
|
segment := segment.(values.String)
|
|
|
|
|
|
|
|
switch segment {
|
2019-06-19 23:58:56 +02:00
|
|
|
case "isDetached":
|
|
|
|
return node.IsDetached(), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "nodeType":
|
2019-06-19 23:58:56 +02:00
|
|
|
return node.GetNodeType(), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "nodeName":
|
2019-06-19 23:58:56 +02:00
|
|
|
return node.GetNodeName(), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "children":
|
2019-02-26 04:46:39 +02:00
|
|
|
children := node.GetChildNodes(ctx)
|
|
|
|
|
|
|
|
if len(path) == 1 {
|
|
|
|
return children, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.GetIn(ctx, children, path[1:])
|
2019-02-20 01:10:18 +02:00
|
|
|
case "length":
|
|
|
|
return node.Length(), nil
|
|
|
|
default:
|
|
|
|
return values.None, nil
|
|
|
|
}
|
2019-02-26 04:46:39 +02:00
|
|
|
default:
|
|
|
|
return values.None, core.TypeError(st, types.Int, types.String)
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
}
|