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
|
|
|
)
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
type PageLoadParams struct {
|
2019-07-17 19:29:16 +02:00
|
|
|
drivers.Params
|
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
|
|
|
}
|
|
|
|
|
2019-09-07 20:03:17 +02:00
|
|
|
// DOCUMENT opens an HTML page by a given url.
|
2019-07-17 19:29:16 +02:00
|
|
|
// By default, loads a page by http call - resulted page does not support any interactions.
|
|
|
|
// @param params (Object) - Optional, An object containing the following properties :
|
|
|
|
// driver (String) - Optional, driver name.
|
|
|
|
// timeout (Int) - Optional, timeout.
|
|
|
|
// userAgent (String) - Optional, user agent.
|
|
|
|
// keepCookies (Boolean) - Optional, boolean value indicating whether to use cookies from previous sessions.
|
|
|
|
// i.e. not to open a page in the Incognito mode.
|
2019-09-05 17:49:21 +02:00
|
|
|
// cookies (HTTPCookies) - Optional, set of HTTP cookies.
|
2019-08-04 23:25:47 +02:00
|
|
|
// headers (HTTPHeaders) - Optional, HTTP headers.
|
2019-07-17 19:29:16 +02:00
|
|
|
// viewport (Viewport) - Optional, viewport params.
|
|
|
|
// @returns (HTMLPage) - Returns loaded HTML page.
|
2019-06-19 23:58:56 +02:00
|
|
|
func Open(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
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
var params PageLoadParams
|
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-06-19 23:58:56 +02:00
|
|
|
p, err := newPageLoadParams(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-07-17 19:29:16 +02:00
|
|
|
return drv.Open(ctx, params.Params)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2018-11-22 03:38:27 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func newDefaultDocLoadParams(url values.String) PageLoadParams {
|
|
|
|
return PageLoadParams{
|
2019-07-17 19:29:16 +02:00
|
|
|
Params: drivers.Params{
|
2019-03-16 01:59:05 +02:00
|
|
|
URL: url.String(),
|
|
|
|
},
|
2019-09-05 18:17:22 +02:00
|
|
|
Timeout: drivers.DefaultPageLoadTimeout * time.Millisecond,
|
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-06-19 23:58:56 +02:00
|
|
|
func newPageLoadParams(url values.String, arg core.Value) (PageLoadParams, error) {
|
2019-03-16 01:59:05 +02:00
|
|
|
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-06-25 21:33:42 +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 {
|
2019-09-05 17:49:21 +02:00
|
|
|
if err := core.ValidateType(cookies, types.Array, types.Object); err != nil {
|
2019-03-16 01:59:05 +02:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:49:21 +02:00
|
|
|
switch c := cookies.(type) {
|
|
|
|
case *values.Array:
|
|
|
|
cookies, err := parseCookieArray(c)
|
2019-03-16 01:59:05 +02:00
|
|
|
|
2019-09-05 17:49:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2019-03-16 01:59:05 +02:00
|
|
|
|
2019-09-05 17:49:21 +02:00
|
|
|
res.Cookies = cookies
|
|
|
|
case *values.Object:
|
|
|
|
cookies, err := parseCookieObject(c)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Cookies = cookies
|
|
|
|
default:
|
|
|
|
res.Cookies = make(drivers.HTTPCookies)
|
|
|
|
}
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 23:25:47 +02:00
|
|
|
headers, exists := obj.Get(values.NewString("headers"))
|
2019-03-16 01:59:05 +02:00
|
|
|
|
|
|
|
if exists {
|
2019-08-04 23:25:47 +02:00
|
|
|
if err := core.ValidateType(headers, types.Object); err != nil {
|
2019-03-16 01:59:05 +02:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2019-08-04 23:25:47 +02:00
|
|
|
header := parseHeader(headers.(*values.Object))
|
|
|
|
res.Headers = header
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 19:29:16 +02:00
|
|
|
viewport, exists := obj.Get(values.NewString("viewport"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
viewport, err := parseViewport(viewport)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Viewport = viewport
|
|
|
|
}
|
2019-02-20 01:10:18 +02:00
|
|
|
case types.String:
|
|
|
|
res.Driver = arg.(values.String).String()
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2019-03-16 01:59:05 +02:00
|
|
|
|
2019-09-05 17:49:21 +02:00
|
|
|
func parseCookieObject(obj *values.Object) (drivers.HTTPCookies, error) {
|
|
|
|
var err error
|
|
|
|
res := make(drivers.HTTPCookies)
|
|
|
|
|
|
|
|
obj.ForEach(func(value core.Value, _ string) bool {
|
|
|
|
cookie, e := parseCookie(value)
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res[cookie.Name] = cookie
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCookieArray(arr *values.Array) (drivers.HTTPCookies, error) {
|
2019-03-16 01:59:05 +02:00
|
|
|
var err error
|
2019-09-05 17:49:21 +02:00
|
|
|
res := make(drivers.HTTPCookies)
|
2019-03-16 01:59:05 +02:00
|
|
|
|
2019-09-05 17:49:21 +02:00
|
|
|
arr.ForEach(func(value core.Value, _ int) bool {
|
2019-03-16 01:59:05 +02:00
|
|
|
cookie, e := parseCookie(value)
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:49:21 +02:00
|
|
|
res[cookie.Name] = cookie
|
2019-03-16 01:59:05 +02:00
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCookie(value core.Value) (drivers.HTTPCookie, error) {
|
2019-05-03 23:10:34 +02:00
|
|
|
err := core.ValidateType(value, types.Object, drivers.HTTPCookieType)
|
2019-03-29 16:48:51 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-03-16 01:59:05 +02:00
|
|
|
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
|
|
|
|
case "strict":
|
|
|
|
cookie.SameSite = drivers.SameSiteStrictMode
|
|
|
|
default:
|
|
|
|
cookie.SameSite = drivers.SameSiteDefaultMode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-04 23:25:47 +02:00
|
|
|
func parseHeader(headers *values.Object) drivers.HTTPHeaders {
|
|
|
|
res := make(drivers.HTTPHeaders)
|
2019-03-16 01:59:05 +02:00
|
|
|
|
2019-08-04 23:25:47 +02:00
|
|
|
headers.ForEach(func(value core.Value, key string) bool {
|
2019-03-16 01:59:05 +02:00
|
|
|
res.Set(key, value.String())
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2019-03-29 16:48:51 +02:00
|
|
|
return res
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
2019-07-17 19:29:16 +02:00
|
|
|
|
|
|
|
func parseViewport(value core.Value) (*drivers.Viewport, error) {
|
|
|
|
if err := core.ValidateType(value, types.Object); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res := &drivers.Viewport{}
|
|
|
|
|
|
|
|
viewport := value.(*values.Object)
|
|
|
|
|
|
|
|
width, exists := viewport.Get(values.NewString("width"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err := core.ValidateType(width, types.Int); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Width = int(values.ToInt(width))
|
|
|
|
}
|
|
|
|
|
|
|
|
height, exists := viewport.Get(values.NewString("height"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
if err := core.ValidateType(height, types.Int); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Height = int(values.ToInt(height))
|
|
|
|
}
|
|
|
|
|
|
|
|
mobile, exists := viewport.Get(values.NewString("mobile"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
res.Mobile = bool(values.ToBoolean(mobile))
|
|
|
|
}
|
|
|
|
|
|
|
|
landscape, exists := viewport.Get(values.NewString("landscape"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
res.Landscape = bool(values.ToBoolean(landscape))
|
|
|
|
}
|
|
|
|
|
|
|
|
scaleFactor, exists := viewport.Get(values.NewString("scaleFactor"))
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
res.ScaleFactor = float64(values.ToFloat(scaleFactor))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|