mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-16 11:37:36 +02:00
83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
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"
|
|
"github.com/MontFerret/ferret/pkg/stdlib/html/driver/http"
|
|
)
|
|
|
|
func Document(ctx context.Context, inputs ...core.Value) (core.Value, error) {
|
|
url, js, err := documentArgs(inputs)
|
|
|
|
if err != nil {
|
|
return values.None, err
|
|
}
|
|
|
|
var drv driver.Driver
|
|
|
|
if !js {
|
|
drv = driver.FromContext(ctx, driver.Http)
|
|
} else {
|
|
drv = driver.FromContext(ctx, driver.Cdp)
|
|
}
|
|
|
|
if drv == nil {
|
|
return values.None, core.Error(core.ErrNotFound, "dom driver")
|
|
}
|
|
|
|
return drv.GetDocument(ctx, url.String())
|
|
}
|
|
|
|
func DocumentParse(ctx context.Context, inputs ...core.Value) (core.Value, error) {
|
|
arg1 := values.EmptyString
|
|
|
|
if len(inputs) == 0 {
|
|
return arg1, core.Error(core.ErrMissedArgument, "document string")
|
|
}
|
|
|
|
a1 := inputs[0]
|
|
|
|
if a1.Type() != core.StringType {
|
|
return arg1, core.Error(core.TypeError(a1.Type(), core.StringType), "arg 1")
|
|
}
|
|
|
|
drv := driver.FromContext(ctx, driver.Http)
|
|
|
|
if drv == nil {
|
|
return values.None, core.Error(core.ErrNotFound, "dom driver")
|
|
}
|
|
|
|
return drv.(*http.HttpDriver).ParseDocument(ctx, arg1.String())
|
|
}
|
|
|
|
func documentArgs(inputs []core.Value) (values.String, values.Boolean, error) {
|
|
arg1 := values.EmptyString
|
|
arg2 := values.False
|
|
|
|
if len(inputs) == 0 {
|
|
return arg1, arg2, core.Error(core.ErrMissedArgument, "element and useJs")
|
|
}
|
|
|
|
a1 := inputs[0]
|
|
|
|
if a1.Type() != core.StringType {
|
|
return arg1, arg2, core.Error(core.TypeError(a1.Type(), core.StringType), "arg 1")
|
|
}
|
|
|
|
arg1 = a1.(values.String)
|
|
|
|
if len(inputs) == 2 {
|
|
a2 := inputs[1]
|
|
|
|
if a2.Type() != core.BooleanType {
|
|
return arg1, arg2, core.Error(core.TypeError(a2.Type(), core.BooleanType), "arg 2")
|
|
}
|
|
|
|
arg2 = a2.(values.Boolean)
|
|
}
|
|
|
|
return arg1, arg2, nil
|
|
}
|