forked from metafates/mangal-lua-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
133 lines (108 loc) · 2.63 KB
/
page.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package headless
import (
"github.com/go-rod/rod"
lua "github.com/yuin/gopher-lua"
)
var pageMethods = map[string]lua.LGFunction{
"waitLoad": pageWaitLoad,
"element": pageElement,
"elementR": pageElementR,
"elements": pageElements,
"elementByJS": pageElementByJS,
"waitElementsMoreThan": pageWaitElementsMoreThan,
"navigate": pageNavigate,
"has": pageHas,
"eval": pageEval,
"html": pageHTML,
}
func registerPageType(L *lua.LState) {
mt := L.NewTypeMetatable("browserPage")
L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), pageMethods))
}
func checkPage(L *lua.LState) *rod.Page {
ud := L.CheckUserData(1)
if v, ok := ud.Value.(*rod.Page); ok {
return v
}
L.ArgError(1, "browserPage expected")
return nil
}
func pageWaitLoad(L *lua.LState) int {
page := checkPage(L)
page.MustWaitLoad()
return 0
}
func pageElement(L *lua.LState) int {
p := checkPage(L)
selector := L.CheckString(2)
ud := L.NewUserData()
ud.Value = p.MustElement(selector)
L.SetMetatable(ud, L.GetTypeMetatable("pageElement"))
L.Push(ud)
return 1
}
func pageNavigate(L *lua.LState) int {
p := checkPage(L)
url := L.CheckString(2)
p.MustNavigate(url)
return 0
}
func pageElementByJS(L *lua.LState) int {
p := checkPage(L)
js := L.CheckString(2)
ud := L.NewUserData()
ud.Value = p.MustElementByJS(js)
L.SetMetatable(ud, L.GetTypeMetatable("pageElement"))
L.Push(ud)
return 1
}
func pageElements(L *lua.LState) int {
p := checkPage(L)
selector := L.CheckString(2)
els := p.MustElements(selector)
table := L.NewTable()
for i, el := range els {
ud := L.NewUserData()
ud.Value = el
L.SetMetatable(ud, L.GetTypeMetatable("pageElement"))
table.RawSetInt(i+1, ud)
}
L.Push(table)
return 1
}
func pageHas(L *lua.LState) int {
p := checkPage(L)
selector := L.CheckString(2)
L.Push(lua.LBool(p.MustHas(selector)))
return 1
}
func pageEval(L *lua.LState) int {
p := checkPage(L)
js := L.CheckString(2)
result := p.MustEval(js).Str()
L.Push(lua.LString(result))
return 1
}
func pageHTML(L *lua.LState) int {
p := checkPage(L)
html := p.MustHTML()
L.Push(lua.LString(html))
return 1
}
func pageElementR(L *lua.LState) int {
p := checkPage(L)
selector := L.CheckString(2)
re := L.CheckString(3)
ud := L.NewUserData()
ud.Value = p.MustElementR(selector, re)
L.SetMetatable(ud, L.GetTypeMetatable("pageElement"))
L.Push(ud)
return 1
}
func pageWaitElementsMoreThan(L *lua.LState) int {
p := checkPage(L)
selector := L.CheckString(2)
count := L.CheckInt(3)
p.MustWaitElementsMoreThan(selector, count)
return 0
}