Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit cf054df

Browse files
committed
Add $ and $$ wildcard mappings
`$` and `$$` are mapped to `Query` and `QueryAll`, respectively. Users won't be able to use `Query` and `QueryAll`. Instead, they will use `$` and `$$` to access those querying methods.
1 parent d8c0e32 commit cf054df

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

browser/mapping.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ func mapResponse(rt *goja.Runtime, r api.Response) mapping {
110110
}
111111

112112
// mapElementHandle to the JS module.
113+
//
114+
//nolint:funlen
113115
func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
114116
maps := mapping{
115117
"asElement": func() *goja.Object {
@@ -168,6 +170,22 @@ func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
168170
return rt.ToValue(ehm).ToObject(rt)
169171
},
170172
}
173+
maps["$"] = func(selector string) *goja.Object {
174+
eh := eh.Query(selector)
175+
ehm := mapElementHandle(rt, eh)
176+
return rt.ToValue(ehm).ToObject(rt)
177+
}
178+
maps["$$"] = func(selector string) *goja.Object {
179+
var (
180+
mehs []mapping
181+
ehs = eh.QueryAll(selector)
182+
)
183+
for _, eh := range ehs {
184+
ehm := mapElementHandle(rt, eh)
185+
mehs = append(mehs, ehm)
186+
}
187+
return rt.ToValue(mehs).ToObject(rt)
188+
}
171189

172190
return maps
173191
}
@@ -247,6 +265,22 @@ func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
247265
},
248266
"waitForTimeout": f.WaitForTimeout,
249267
}
268+
maps["$"] = func(selector string) *goja.Object {
269+
eh := f.Query(selector)
270+
ehm := mapElementHandle(rt, eh)
271+
return rt.ToValue(ehm).ToObject(rt)
272+
}
273+
maps["$$"] = func(selector string) *goja.Object {
274+
var (
275+
mehs []mapping
276+
ehs = f.QueryAll(selector)
277+
)
278+
for _, eh := range ehs {
279+
ehm := mapElementHandle(rt, eh)
280+
mehs = append(mehs, ehm)
281+
}
282+
return rt.ToValue(mehs).ToObject(rt)
283+
}
250284

251285
return maps
252286
}
@@ -343,6 +377,22 @@ func mapPage(rt *goja.Runtime, p api.Page) mapping {
343377
"waitForTimeout": p.WaitForTimeout,
344378
"workers": p.Workers,
345379
}
380+
maps["$"] = func(selector string) *goja.Object {
381+
eh := p.Query(selector)
382+
ehm := mapElementHandle(rt, eh)
383+
return rt.ToValue(ehm).ToObject(rt)
384+
}
385+
maps["$$"] = func(selector string) *goja.Object {
386+
var (
387+
mehs []mapping
388+
ehs = p.QueryAll(selector)
389+
)
390+
for _, eh := range ehs {
391+
ehm := mapElementHandle(rt, eh)
392+
mehs = append(mehs, ehm)
393+
}
394+
return rt.ToValue(mehs).ToObject(rt)
395+
}
346396

347397
return maps
348398
}

0 commit comments

Comments
 (0)