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 {
|
|
|
|
drivers.OpenPageParams
|
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-06-19 23:58:56 +02:00
|
|
|
// Open opens an HTML page 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.
|
2019-06-19 23:58:56 +02:00
|
|
|
// @param isDynamicOrParams (Boolean|PageLoadParams) - Either a boolean value that indicates whether to use dynamic page
|
2018-12-22 06:14:41 +02:00
|
|
|
// or an object with the following properties :
|
|
|
|
// dynamic (Boolean) - Optional, indicates whether to use dynamic page.
|
2019-06-19 23:58:56 +02:00
|
|
|
// timeout (Int) - Optional, Open load timeout.
|
2018-10-14 19:06:27 +02:00
|
|
|
// @returns (HTMLDocument) - Returns loaded HTML document.
|
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-06-19 23:58:56 +02:00
|
|
|
return drv.Open(ctx, params.OpenPageParams)
|
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{
|
|
|
|
OpenPageParams: drivers.OpenPageParams{
|
2019-03-16 01:59:05 +02:00
|
|
|
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-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-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
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:48:51 +02:00
|
|
|
header := parseHeader(header.(*values.Object))
|
2019-03-16 01:59:05 +02:00
|
|
|
res.Header = header
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
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
|
|
|
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
|
|
|
|
|
|
|
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) {
|
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-03-29 16:48:51 +02:00
|
|
|
func parseHeader(header *values.Object) drivers.HTTPHeader {
|
2019-03-16 01:59:05 +02:00
|
|
|
res := make(drivers.HTTPHeader)
|
|
|
|
|
|
|
|
header.ForEach(func(value core.Value, key string) bool {
|
|
|
|
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
|
|
|
}
|