mirror of
https://github.com/MontFerret/ferret.git
synced 2025-07-05 00:49:00 +02:00
Next (#214)
* Renamed DOCUMENT to PAGE * Added PageLoadParams * Added PageLoadParams * Renamed LoadPageParams -> PageLoadParams * Added support for context.Done() (#201) * Bug/#189 operators precedence (#202) * Fixed math operators precedence * Fixed logical operators precedence * Fixed array operator * Added support for parentheses to enforce a different operator evaluation order * Feature/#200 drivers (#209) * Added new interfaces * Renamed dynamic to cdp driver * Renamed drivers * Added ELEMENT_EXISTS function (#210) * Renamed back PAGE to DOCUMENT (#211) * Added Getter and Setter interfaces
This commit is contained in:
@ -2,27 +2,28 @@ package html
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/MontFerret/ferret/pkg/html"
|
||||
"github.com/MontFerret/ferret/pkg/drivers"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LoadDocumentArgs struct {
|
||||
Dynamic bool
|
||||
type DocumentLoadParams struct {
|
||||
Dynamic values.Boolean
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// Page loads a HTML document by a given url.
|
||||
// Document loads a HTML document by a given url.
|
||||
// By default, loads a document by http call - resulted document does not support any interactions.
|
||||
// If passed "true" as a second argument, headless browser is used for loading the document which support interactions.
|
||||
// @param url (String) - Target url string. If passed "about:blank" for dynamic document - it will open an empty page.
|
||||
// @param dynamicOrTimeout (Boolean|Int, optional) - If boolean value is passed, it indicates whether to use dynamic document.
|
||||
// If integer values is passed it sets a custom timeout.
|
||||
// @param timeout (Int, optional) - Sets a custom timeout.
|
||||
// @param isDynamicOrParams (Boolean|DocumentLoadParams) - Either a boolean value that indicates whether to use dynamic page
|
||||
// or an object with the following properties :
|
||||
// dynamic (Boolean) - Optional, indicates whether to use dynamic page.
|
||||
// timeout (Int) - Optional, Document load timeout.
|
||||
// @returns (HTMLDocument) - Returns loaded HTML document.
|
||||
func Document(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 1, 3)
|
||||
err := core.ValidateArgs(args, 1, 2)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
@ -36,23 +37,35 @@ func Document(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
|
||||
url := args[0].(values.String)
|
||||
|
||||
params, err := parseLoadDocumentArgs(args)
|
||||
var params DocumentLoadParams
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
if len(args) == 1 {
|
||||
params = newDefaultDocLoadParams()
|
||||
} else {
|
||||
p, err := newDocLoadParams(args[1])
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
params = p
|
||||
}
|
||||
|
||||
var drv html.Driver
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, params.Timeout)
|
||||
defer cancel()
|
||||
|
||||
if params.Dynamic == false {
|
||||
drv, err = html.FromContext(ctx, html.Static)
|
||||
} else {
|
||||
drv, err = html.FromContext(ctx, html.Dynamic)
|
||||
if params.Dynamic {
|
||||
drv, err := drivers.DynamicFrom(ctx)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
return drv.GetDocument(ctx, url)
|
||||
}
|
||||
|
||||
drv, err := drivers.StaticFrom(ctx)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
@ -60,39 +73,46 @@ func Document(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
return drv.GetDocument(ctx, url)
|
||||
}
|
||||
|
||||
func parseLoadDocumentArgs(args []core.Value) (LoadDocumentArgs, error) {
|
||||
res := LoadDocumentArgs{
|
||||
func newDefaultDocLoadParams() DocumentLoadParams {
|
||||
return DocumentLoadParams{
|
||||
Dynamic: false,
|
||||
Timeout: time.Second * 30,
|
||||
}
|
||||
}
|
||||
|
||||
if len(args) == 3 {
|
||||
err := core.ValidateType(args[1], core.BooleanType)
|
||||
func newDocLoadParams(arg core.Value) (DocumentLoadParams, error) {
|
||||
res := newDefaultDocLoadParams()
|
||||
|
||||
if err != nil {
|
||||
if err := core.ValidateType(arg, core.BooleanType, core.ObjectType); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
if arg.Type() == core.BooleanType {
|
||||
res.Dynamic = arg.(values.Boolean)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
obj := arg.(*values.Object)
|
||||
|
||||
isDynamic, exists := obj.Get(values.NewString("dynamic"))
|
||||
|
||||
if exists {
|
||||
if err := core.ValidateType(isDynamic, core.BooleanType); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.Dynamic = bool(args[1].(values.Boolean))
|
||||
res.Dynamic = isDynamic.(values.Boolean)
|
||||
}
|
||||
|
||||
err = core.ValidateType(args[2], core.IntType)
|
||||
timeout, exists := obj.Get(values.NewString("timeout"))
|
||||
|
||||
if err != nil {
|
||||
if exists {
|
||||
if err := core.ValidateType(timeout, core.IntType); err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.Timeout = time.Duration(args[2].(values.Int)) * time.Millisecond
|
||||
} else if len(args) == 2 {
|
||||
err := core.ValidateType(args[1], core.BooleanType, core.IntType)
|
||||
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
if args[1].Type() == core.BooleanType {
|
||||
res.Dynamic = bool(args[1].(values.Boolean))
|
||||
} else {
|
||||
res.Timeout = time.Duration(args[1].(values.Int)) * time.Millisecond
|
||||
}
|
||||
res.Timeout = time.Duration(timeout.(values.Int)) + time.Millisecond
|
||||
}
|
||||
|
||||
return res, nil
|
||||
|
Reference in New Issue
Block a user