forked from vadv/gopher-lua-libs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
element.go
83 lines (62 loc) · 1.42 KB
/
element.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package headless
import (
"github.com/go-rod/rod"
lua "github.com/yuin/gopher-lua"
)
var elementMethods = map[string]lua.LGFunction{
"input": elementInput,
"click": elementClick,
"text": elementText,
"attribute": elementAttribute,
"html": elementHtml,
"property": elementProperty,
}
func registerElementType(L *lua.LState) {
mt := L.NewTypeMetatable("pageElement")
L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), elementMethods))
}
func checkElement(L *lua.LState) *rod.Element {
ud := L.CheckUserData(1)
if v, ok := ud.Value.(*rod.Element); ok {
return v
}
L.ArgError(1, "element expected")
return nil
}
func elementInput(L *lua.LState) int {
el := checkElement(L)
value := L.ToString(2)
el.MustInput(value)
return 0
}
func elementClick(L *lua.LState) int {
el := checkElement(L)
el.MustClick()
return 0
}
func elementText(L *lua.LState) int {
el := checkElement(L)
text := el.MustText()
L.Push(lua.LString(text))
return 1
}
func elementAttribute(L *lua.LState) int {
el := checkElement(L)
name := L.ToString(2)
value := el.MustAttribute(name)
L.Push(lua.LString(*value))
return 1
}
func elementHtml(L *lua.LState) int {
el := checkElement(L)
html := el.MustHTML()
L.Push(lua.LString(html))
return 1
}
func elementProperty(L *lua.LState) int {
el := checkElement(L)
name := L.ToString(2)
value := el.MustProperty(name)
L.Push(lua.LString(value.Str()))
return 1
}