1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-06-19 00:18:00 +02:00
Files
assets
cli
examples
pkg
compiler
parser
runtime
stdlib
arrays
collections
html
driver
blob.go
click.go
click_all.go
document.go
document_parse.go
element.go
elements.go
inner_html.go
inner_html_all.go
inner_text.go
inner_text_all.go
input.go
lib.go
navigate.go
wait_class.go
wait_class_all.go
wait_element.go
wait_navigation.go
objects
strings
types
utils
lib.go
.editorconfig
.env
.gitignore
.travis.yml
Gopkg.lock
Gopkg.toml
LICENSE
Makefile
README.md
README.md.orig
main.go
revive.toml
ferret/pkg/stdlib/html/document.go

54 lines
1.4 KiB
Go
Raw Normal View History

2018-09-18 16:42:38 -04:00
package html
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/stdlib/html/driver"
)
/*
* Loads a 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 dynamic (Boolean) - Optional boolean value indicating whether to use dynamic document.
* @returns (HTMLDocument) - Returns loaded HTML document.
*/
func Document(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
2018-09-18 16:42:38 -04:00
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], core.StringType)
2018-09-18 16:42:38 -04:00
url := args[0].(values.String)
dynamic := values.False
2018-09-18 16:42:38 -04:00
if len(args) == 2 {
err = core.ValidateType(args[1], core.BooleanType)
2018-09-18 16:42:38 -04:00
if err != nil {
return values.None, err
}
2018-09-18 16:42:38 -04:00
dynamic = args[1].(values.Boolean)
2018-09-18 16:42:38 -04:00
}
var drv driver.Driver
2018-09-18 16:42:38 -04:00
if !dynamic {
drv, err = driver.FromContext(ctx, driver.Static)
} else {
drv, err = driver.FromContext(ctx, driver.Dynamic)
2018-09-18 16:42:38 -04:00
}
2018-09-23 04:33:20 -04:00
if err != nil {
return values.None, err
2018-09-18 16:42:38 -04:00
}
return drv.GetDocument(ctx, url)
2018-09-18 16:42:38 -04:00
}