2018-09-18 22:42:38 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-16 01:59:05 +02:00
|
|
|
"strings"
|
2019-02-13 19:31:18 +02:00
|
|
|
"time"
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2019-02-20 01:10:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp"
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
type DocumentLoadParams struct {
|
2019-03-16 01:59:05 +02:00
|
|
|
drivers.LoadDocumentParams
|
2019-02-20 01:10:18 +02:00
|
|
|
Driver string
|
2018-11-22 06:11:01 +02:00
|
|
|
Timeout time.Duration
|
2018-11-22 03:38:27 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
// Document loads a HTML document by a given url.
|
2018-10-14 19:06:27 +02:00
|
|
|
// 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.
|
2018-12-22 06:14:41 +02:00
|
|
|
// @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.
|
2018-10-14 19:06:27 +02:00
|
|
|
// @returns (HTMLDocument) - Returns loaded HTML document.
|
2018-10-07 04:33:39 +02:00
|
|
|
func Document(ctx context.Context, args ...core.Value) (core.Value, error) {
|
2018-12-22 06:14:41 +02:00
|
|
|
err := core.ValidateArgs(args, 1, 2)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
err = core.ValidateType(args[0], types.String)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-11-22 03:38:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
url := args[0].(values.String)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
var params DocumentLoadParams
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
if len(args) == 1 {
|
2019-03-16 01:59:05 +02:00
|
|
|
params = newDefaultDocLoadParams(url)
|
2018-12-22 06:14:41 +02:00
|
|
|
} else {
|
2019-03-16 01:59:05 +02:00
|
|
|
p, err := newDocLoadParams(url, args[1])
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
params = p
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-11-22 06:11:01 +02:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, params.Timeout)
|
2018-11-22 03:38:27 +02:00
|
|
|
defer cancel()
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
drv, err := drivers.FromContext(ctx, params.Driver)
|
2018-12-22 06:14:41 +02:00
|
|
|
|
2018-09-23 10:33:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-03-16 01:59:05 +02:00
|
|
|
return drv.LoadDocument(ctx, params.LoadDocumentParams)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2018-11-22 03:38:27 +02:00
|
|
|
|
2019-03-16 01:59:05 +02:00
|
|
|
func newDefaultDocLoadParams(url values.String) DocumentLoadParams {
|
2018-12-22 06:14:41 +02:00
|
|
|
return DocumentLoadParams{
|
2019-03-16 01:59:05 +02:00
|
|
|
LoadDocumentParams: drivers.LoadDocumentParams{
|
|
|
|
URL: url.String(),
|
|
|
|
},
|
2018-11-22 06:11:01 +02:00
|
|
|
Timeout: time.Second * 30,
|
2018-11-22 03:38:27 +02:00
|
|
|
}
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
2018-11-22 03:38:27 +02:00
|
|
|
|
2019-03-16 01:59:05 +02:00
|
|
|
func newDocLoadParams(url values.String, arg core.Value) (DocumentLoadParams, error) {
|
|
|
|
res := newDefaultDocLoadParams(url)
|
2018-11-22 03:38:27 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
if err := core.ValidateType(arg, types.Boolean, types.String, types.Object); err != nil {
|
2018-12-22 06:14:41 +02:00
|
|
|
return res, err
|
|
|
|
}
|
2018-11-22 03:38:27 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
switch arg.Type() {
|
|
|
|
case types.Object:
|
|
|
|
obj := arg.(*values.Object)
|
2018-11-22 03:38:27 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
driver, exists := obj.Get(values.NewString("driver"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err := core.ValidateType(driver, types.String); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2018-11-22 03:38:27 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
res.Driver = driver.(values.String).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout, exists := obj.Get(values.NewString("timeout"))
|
2018-12-22 06:14:41 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
if exists {
|
|
|
|
if err := core.ValidateType(timeout, types.Int); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2018-12-22 06:14:41 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
res.Timeout = time.Duration(timeout.(values.Int)) + time.Millisecond
|
2018-11-22 03:38:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-16 01:59:05 +02:00
|
|
|
userAgent, exists := obj.Get(values.NewString("userAgent"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err := core.ValidateType(userAgent, types.String); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.UserAgent = userAgent.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
keepCookies, exists := obj.Get(values.NewString("keepCookies"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err := core.ValidateType(keepCookies, types.Boolean); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.KeepCookies = bool(keepCookies.(values.Boolean))
|
|
|
|
}
|
|
|
|
|
|
|
|
cookies, exists := obj.Get(values.NewString("cookies"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err := core.ValidateType(cookies, types.Array); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cookies, err := parseCookies(cookies.(*values.Array))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Cookies = cookies
|
|
|
|
}
|
|
|
|
|
|
|
|
header, exists := obj.Get(values.NewString("header"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err := core.ValidateType(header, types.Object); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
header, err := parseHeader(header.(*values.Object))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Header = header
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
break
|
|
|
|
case types.String:
|
|
|
|
res.Driver = arg.(values.String).String()
|
2018-12-22 06:14:41 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
break
|
|
|
|
case types.Boolean:
|
|
|
|
b := arg.(values.Boolean)
|
2018-11-22 03:38:27 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
// fallback
|
|
|
|
if b {
|
|
|
|
res.Driver = cdp.DriverName
|
2018-11-22 03:38:27 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
break
|
2018-11-22 03:38:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2019-03-16 01:59:05 +02:00
|
|
|
|
|
|
|
func parseCookies(arr *values.Array) ([]drivers.HTTPCookie, error) {
|
|
|
|
var err error
|
|
|
|
res := make([]drivers.HTTPCookie, 0, arr.Length())
|
|
|
|
|
|
|
|
arr.ForEach(func(value core.Value, idx int) bool {
|
|
|
|
cookie, e := parseCookie(value)
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, cookie)
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCookie(value core.Value) (drivers.HTTPCookie, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = core.ValidateType(value, types.Object, drivers.HTTPCookieType); err != nil {
|
|
|
|
return drivers.HTTPCookie{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if value.Type() == drivers.HTTPCookieType {
|
|
|
|
return value.(drivers.HTTPCookie), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
co := value.(*values.Object)
|
|
|
|
|
|
|
|
cookie := drivers.HTTPCookie{
|
|
|
|
Name: co.MustGet("name").String(),
|
|
|
|
Value: co.MustGet("value").String(),
|
|
|
|
Path: co.MustGet("path").String(),
|
|
|
|
Domain: co.MustGet("domain").String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
maxAge, exists := co.Get("maxAge")
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err = core.ValidateType(maxAge, types.Int); err != nil {
|
|
|
|
return drivers.HTTPCookie{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cookie.MaxAge = int(maxAge.(values.Int))
|
|
|
|
}
|
|
|
|
|
|
|
|
expires, exists := co.Get("expires")
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err = core.ValidateType(maxAge, types.DateTime, types.String); err != nil {
|
|
|
|
return drivers.HTTPCookie{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if expires.Type() == types.DateTime {
|
|
|
|
cookie.Expires = expires.(values.DateTime).Unwrap().(time.Time)
|
|
|
|
} else {
|
|
|
|
t, err := time.Parse(expires.String(), values.DefaultTimeLayout)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return drivers.HTTPCookie{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cookie.Expires = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sameSite, exists := co.Get("sameSite")
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
sameSite := strings.ToLower(sameSite.String())
|
|
|
|
|
|
|
|
switch sameSite {
|
|
|
|
case "lax":
|
|
|
|
cookie.SameSite = drivers.SameSiteLaxMode
|
|
|
|
break
|
|
|
|
case "strict":
|
|
|
|
cookie.SameSite = drivers.SameSiteStrictMode
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
cookie.SameSite = drivers.SameSiteDefaultMode
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
httpOnly, exists := co.Get("httpOnly")
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err = core.ValidateType(httpOnly, types.Boolean); err != nil {
|
|
|
|
return drivers.HTTPCookie{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cookie.HTTPOnly = bool(httpOnly.(values.Boolean))
|
|
|
|
}
|
|
|
|
|
|
|
|
secure, exists := co.Get("secure")
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err = core.ValidateType(secure, types.Boolean); err != nil {
|
|
|
|
return drivers.HTTPCookie{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cookie.Secure = bool(secure.(values.Boolean))
|
|
|
|
}
|
|
|
|
|
|
|
|
return cookie, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseHeader(header *values.Object) (drivers.HTTPHeader, error) {
|
|
|
|
res := make(drivers.HTTPHeader)
|
|
|
|
|
|
|
|
header.ForEach(func(value core.Value, key string) bool {
|
|
|
|
res.Set(key, value.String())
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|