mirror of
				https://github.com/MontFerret/ferret.git
				synced 2025-10-30 23:37:40 +02:00 
			
		
		
		
	Feature/#221 mouse events (#237)
* Initial work * Added MoveMouseByXY and ScrollByXY * Fixed liniting issues
This commit is contained in:
		| @@ -27,22 +27,46 @@ func Hover(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	if len(args) == 2 { | ||||
| 	selector := values.EmptyString | ||||
|  | ||||
| 	if len(args) > 1 { | ||||
| 		err = core.ValidateType(args[1], types.String) | ||||
|  | ||||
| 		if err != nil { | ||||
| 			return values.None, err | ||||
| 		} | ||||
|  | ||||
| 		// Document with a selector | ||||
| 		doc := args[0].(drivers.HTMLDocument) | ||||
| 		selector := args[1].(values.String) | ||||
|  | ||||
| 		return values.None, doc.HoverBySelector(ctx, selector) | ||||
| 		selector = args[1].(values.String) | ||||
| 	} | ||||
|  | ||||
| 	// Element | ||||
| 	el := args[0].(drivers.HTMLElement) | ||||
| 	switch n := args[0].(type) { | ||||
| 	case drivers.HTMLDocument: | ||||
| 		if selector == values.EmptyString { | ||||
| 			return values.None, core.Error(core.ErrMissedArgument, "selector") | ||||
| 		} | ||||
|  | ||||
| 	return values.None, el.Hover(ctx) | ||||
| 		return values.None, n.MoveMouseBySelector(ctx, selector) | ||||
| 	case drivers.HTMLElement: | ||||
| 		if selector == values.EmptyString { | ||||
| 			return values.None, n.Hover(ctx) | ||||
| 		} | ||||
|  | ||||
| 		found := n.QuerySelector(ctx, selector) | ||||
|  | ||||
| 		if found == values.None { | ||||
| 			return values.None, core.Errorf(core.ErrNotFound, "element by selector %s", selector) | ||||
| 		} | ||||
|  | ||||
| 		el, ok := found.(drivers.HTMLElement) | ||||
|  | ||||
| 		if !ok { | ||||
| 			return values.None, core.Errorf(core.ErrNotFound, "element by selector %s", selector) | ||||
| 		} | ||||
|  | ||||
| 		defer el.Close() | ||||
|  | ||||
| 		return values.None, el.Hover(ctx) | ||||
| 	default: | ||||
| 		return values.None, core.TypeError(n.Type(), drivers.HTMLDocumentType, drivers.HTMLElementType) | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -2,11 +2,12 @@ package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/drivers" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values/types" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| const defaultTimeout = 5000 | ||||
| @@ -27,12 +28,14 @@ func NewLib() map[string]core.Function { | ||||
| 		"INNER_TEXT":       InnerText, | ||||
| 		"INNER_TEXT_ALL":   InnerTextAll, | ||||
| 		"INPUT":            Input, | ||||
| 		"MOUSE":            MouseMoveXY, | ||||
| 		"NAVIGATE":         Navigate, | ||||
| 		"NAVIGATE_BACK":    NavigateBack, | ||||
| 		"NAVIGATE_FORWARD": NavigateForward, | ||||
| 		"PAGINATION":       Pagination, | ||||
| 		"PDF":              PDF, | ||||
| 		"SCREENSHOT":       Screenshot, | ||||
| 		"SCROLL":           ScrollXY, | ||||
| 		"SCROLL_BOTTOM":    ScrollBottom, | ||||
| 		"SCROLL_ELEMENT":   ScrollInto, | ||||
| 		"SCROLL_TOP":       ScrollTop, | ||||
|   | ||||
							
								
								
									
										56
									
								
								pkg/stdlib/html/mouse_xy.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								pkg/stdlib/html/mouse_xy.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/drivers" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values/types" | ||||
| ) | ||||
|  | ||||
| // MouseMoveXY moves mouse by given coordinates. | ||||
| // @param doc (HTMLDocument) - HTML document. | ||||
| // @param x (Int|Float) - X coordinate. | ||||
| // @param y (Int|Float) - Y coordinate. | ||||
| func MouseMoveXY(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 3, 3) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[0], drivers.HTMLDocumentType) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[1], types.Int, types.Float) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[2], types.Int, types.Float) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	x, err := values.ToFloat(args[0]) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	y, err := values.ToFloat(args[1]) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	doc := args[0].(drivers.HTMLDocument) | ||||
|  | ||||
| 	return values.None, doc.MoveMouseByXY(ctx, x, y) | ||||
| } | ||||
							
								
								
									
										56
									
								
								pkg/stdlib/html/scroll_xy.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								pkg/stdlib/html/scroll_xy.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"github.com/MontFerret/ferret/pkg/drivers" | ||||
|  | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values/types" | ||||
| ) | ||||
|  | ||||
| // ScrollXY scrolls by given coordinates. | ||||
| // @param doc (HTMLDocument) - HTML document. | ||||
| // @param x (Int|Float) - X coordinate. | ||||
| // @param y (Int|Float) - Y coordinate. | ||||
| func ScrollXY(ctx context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 3, 3) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[0], drivers.HTMLDocumentType) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[1], types.Int, types.Float) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[2], types.Int, types.Float) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	x, err := values.ToFloat(args[0]) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	y, err := values.ToFloat(args[1]) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	doc := args[0].(drivers.HTMLDocument) | ||||
|  | ||||
| 	return values.None, doc.ScrollByXY(ctx, x, y) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user