mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-12 11:15:14 +02:00
Added new methods
This commit is contained in:
parent
a8813f8b03
commit
533f868b85
@ -57,6 +57,37 @@ func Click(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
return doc.ClickBySelector(values.NewString(selector))
|
||||
}
|
||||
|
||||
/*
|
||||
* Dispatches click event on all matched element
|
||||
* @param source (Document) - Document.
|
||||
* @param selector (String) - Selector.
|
||||
* @returns (Boolean) - Returns true if matched at least one element.
|
||||
*/
|
||||
func ClickAll(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 2, 2)
|
||||
|
||||
if err != nil {
|
||||
return values.False, err
|
||||
}
|
||||
|
||||
arg1 := args[0]
|
||||
selector := args[1].String()
|
||||
|
||||
err = core.ValidateType(arg1, core.HtmlDocumentType)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
doc, ok := arg1.(*dynamic.HtmlDocument)
|
||||
|
||||
if !ok {
|
||||
return values.False, core.Error(core.ErrInvalidType, "expected dynamic document")
|
||||
}
|
||||
|
||||
return doc.ClickBySelectorAll(values.NewString(selector))
|
||||
}
|
||||
|
||||
/*
|
||||
* Navigates a document to a new resource.
|
||||
* The operation blocks the execution until the page gets loaded.
|
||||
|
132
pkg/stdlib/html/data.go
Normal file
132
pkg/stdlib/html/data.go
Normal file
@ -0,0 +1,132 @@
|
||||
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/dynamic"
|
||||
)
|
||||
|
||||
/*
|
||||
* Returns inner html of a matched element
|
||||
* @param doc (Document) - Document
|
||||
* @param selector (String) - Selector
|
||||
* @returns str (String) - String value of inner html.
|
||||
*/
|
||||
func InnerHtml(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 2, 2)
|
||||
|
||||
if err != nil {
|
||||
return values.EmptyString, err
|
||||
}
|
||||
|
||||
arg1 := args[0]
|
||||
selector := args[1].String()
|
||||
|
||||
err = core.ValidateType(arg1, core.HtmlDocumentType)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
doc, ok := arg1.(*dynamic.HtmlDocument)
|
||||
|
||||
if !ok {
|
||||
return values.EmptyString, core.Error(core.ErrInvalidType, "expected dynamic document")
|
||||
}
|
||||
|
||||
return doc.InnerHtmlBySelector(values.NewString(selector))
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns inner html of all matched elements.
|
||||
* @param doc (Document) - Document
|
||||
* @param selector (String) - Selector
|
||||
* @returns array (Array) - Array of string values.
|
||||
*/
|
||||
func InnerHtmlAll(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 2, 2)
|
||||
|
||||
if err != nil {
|
||||
return values.EmptyString, err
|
||||
}
|
||||
|
||||
arg1 := args[0]
|
||||
selector := args[1].String()
|
||||
|
||||
err = core.ValidateType(arg1, core.HtmlDocumentType)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
doc, ok := arg1.(*dynamic.HtmlDocument)
|
||||
|
||||
if !ok {
|
||||
return values.EmptyString, core.Error(core.ErrInvalidType, "expected dynamic document")
|
||||
}
|
||||
|
||||
return doc.InnerHtmlBySelectorAll(values.NewString(selector))
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns inner text of a matched element
|
||||
* @param doc (Document) - Document
|
||||
* @param selector (String) - Selector
|
||||
* @returns str (String) - String value of inner text.
|
||||
*/
|
||||
func InnerText(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 2, 2)
|
||||
|
||||
if err != nil {
|
||||
return values.EmptyString, err
|
||||
}
|
||||
|
||||
arg1 := args[0]
|
||||
selector := args[1].String()
|
||||
|
||||
err = core.ValidateType(arg1, core.HtmlDocumentType)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
doc, ok := arg1.(*dynamic.HtmlDocument)
|
||||
|
||||
if !ok {
|
||||
return values.EmptyString, core.Error(core.ErrInvalidType, "expected dynamic document")
|
||||
}
|
||||
|
||||
return doc.InnerHtmlBySelector(values.NewString(selector))
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns inner text of all matched elements.
|
||||
* @param doc (Document) - Document
|
||||
* @param selector (String) - Selector
|
||||
* @returns array (Array) - Array of string values.
|
||||
*/
|
||||
func InnerTextAll(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 2, 2)
|
||||
|
||||
if err != nil {
|
||||
return values.EmptyString, err
|
||||
}
|
||||
|
||||
arg1 := args[0]
|
||||
selector := args[1].String()
|
||||
|
||||
err = core.ValidateType(arg1, core.HtmlDocumentType)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
doc, ok := arg1.(*dynamic.HtmlDocument)
|
||||
|
||||
if !ok {
|
||||
return values.EmptyString, core.Error(core.ErrInvalidType, "expected dynamic document")
|
||||
}
|
||||
|
||||
return doc.InnerHtmlBySelectorAll(values.NewString(selector))
|
||||
}
|
@ -323,6 +323,114 @@ func (doc *HtmlDocument) Url() core.Value {
|
||||
return doc.url
|
||||
}
|
||||
|
||||
func (doc *HtmlDocument) InnerHtmlBySelector(selector values.String) (values.String, error) {
|
||||
res, err := eval.Eval(
|
||||
doc.client,
|
||||
fmt.Sprintf(`
|
||||
var el = document.querySelector(%s);
|
||||
|
||||
if (el == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return el.innerHtml;
|
||||
`, eval.ParamString(selector.String())),
|
||||
true,
|
||||
false,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return values.EmptyString, err
|
||||
}
|
||||
|
||||
if res.Type() == core.StringType {
|
||||
return res.(values.String), nil
|
||||
}
|
||||
|
||||
return values.EmptyString, nil
|
||||
}
|
||||
|
||||
func (doc *HtmlDocument) InnerHtmlBySelectorAll(selector values.String) (*values.Array, error) {
|
||||
res, err := eval.Eval(
|
||||
doc.client,
|
||||
fmt.Sprintf(`
|
||||
var elements = document.querySelectorAll(%s);
|
||||
|
||||
if (elements == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return elements.map(i => i.innerHtml);
|
||||
`, eval.ParamString(selector.String())),
|
||||
true,
|
||||
false,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return values.NewArray(0), err
|
||||
}
|
||||
|
||||
if res.Type() == core.ArrayType {
|
||||
return res.(*values.Array), nil
|
||||
}
|
||||
|
||||
return values.NewArray(0), nil
|
||||
}
|
||||
|
||||
func (doc *HtmlDocument) InnerTextBySelector(selector values.String) (values.String, error) {
|
||||
res, err := eval.Eval(
|
||||
doc.client,
|
||||
fmt.Sprintf(`
|
||||
var el = document.querySelector(%s);
|
||||
|
||||
if (el == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return el.innerText;
|
||||
`, eval.ParamString(selector.String())),
|
||||
true,
|
||||
false,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return values.EmptyString, err
|
||||
}
|
||||
|
||||
if res.Type() == core.StringType {
|
||||
return res.(values.String), nil
|
||||
}
|
||||
|
||||
return values.EmptyString, nil
|
||||
}
|
||||
|
||||
func (doc *HtmlDocument) InnerTextBySelectorAll(selector values.String) (*values.Array, error) {
|
||||
res, err := eval.Eval(
|
||||
doc.client,
|
||||
fmt.Sprintf(`
|
||||
var elements = document.querySelectorAll(%s);
|
||||
|
||||
if (elements == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return elements.map(i => i.innerText);
|
||||
`, eval.ParamString(selector.String())),
|
||||
true,
|
||||
false,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return values.NewArray(0), err
|
||||
}
|
||||
|
||||
if res.Type() == core.ArrayType {
|
||||
return res.(*values.Array), nil
|
||||
}
|
||||
|
||||
return values.NewArray(0), nil
|
||||
}
|
||||
|
||||
func (doc *HtmlDocument) ClickBySelector(selector values.String) (values.Boolean, error) {
|
||||
res, err := eval.Eval(
|
||||
doc.client,
|
||||
@ -353,6 +461,38 @@ func (doc *HtmlDocument) ClickBySelector(selector values.String) (values.Boolean
|
||||
return values.False, nil
|
||||
}
|
||||
|
||||
func (doc *HtmlDocument) ClickBySelectorAll(selector values.String) (values.Boolean, error) {
|
||||
res, err := eval.Eval(
|
||||
doc.client,
|
||||
fmt.Sprintf(`
|
||||
var elements = document.querySelectorAll(%s);
|
||||
|
||||
if (elements == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
elements.forEach((el) => {
|
||||
var evt = new window.MouseEvent('click', { bubbles: true });
|
||||
el.dispatchEvent(evt);
|
||||
});
|
||||
|
||||
return true;
|
||||
`, eval.ParamString(selector.String())),
|
||||
true,
|
||||
false,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return values.False, err
|
||||
}
|
||||
|
||||
if res.Type() == core.BooleanType {
|
||||
return res.(values.Boolean), nil
|
||||
}
|
||||
|
||||
return values.False, nil
|
||||
}
|
||||
|
||||
func (doc *HtmlDocument) InputBySelector(selector values.String, value core.Value) (values.Boolean, error) {
|
||||
res, err := eval.Eval(
|
||||
doc.client,
|
||||
|
@ -11,7 +11,12 @@ func NewLib() map[string]core.Function {
|
||||
"WAIT_ELEMENT": WaitElement,
|
||||
"WAIT_NAVIGATION": WaitNavigation,
|
||||
"CLICK": Click,
|
||||
"CLICK_ALL": ClickAll,
|
||||
"NAVIGATE": Navigate,
|
||||
"INPUT": Input,
|
||||
"INNER_HTML": InnerHtml,
|
||||
"INNER_HTML_ALL": InnerHtmlAll,
|
||||
"INNER_TEXT": InnerText,
|
||||
"INNER_TEXT_ALL": InnerTextAll,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user