Skip to content

Commit

Permalink
rename Array to JSArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 14, 2020
1 parent cbba06c commit 18dd2d9
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 45 deletions.
4 changes: 2 additions & 2 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Browser struct {
quiet bool // see defaults.Quiet
trace bool // see defaults.Trace
traceLogAct func(string)
traceLogJS func(string, Array)
traceLogJS func(string, JSArgs)
traceLogErr func(error)

defaultViewport *proto.EmulationSetDeviceMetricsOverride
Expand Down Expand Up @@ -114,7 +114,7 @@ func (b *Browser) Trace(enable bool) *Browser {
}

// TraceLog overrides the default log functions for trace
func (b *Browser) TraceLog(msg func(string), js func(string, Array), err func(error)) *Browser {
func (b *Browser) TraceLog(msg func(string), js func(string, JSArgs), err func(error)) *Browser {
if msg == nil {
b.traceLogAct = defaultTraceLogAct
} else {
Expand Down
2 changes: 1 addition & 1 deletion browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (s *S) TestTrace() {
func(s string) {
msg = s
},
func(string, rod.Array) {},
func(string, rod.JSArgs) {},
func(e error) {
errs = append(errs, e)
},
Expand Down
16 changes: 8 additions & 8 deletions dev_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (p *Page) Overlay(left, top, width, height float64, msg string) (remove fun
root := p.Root()
id := utils.RandString(8)

_, err := root.EvalWithOptions(jsHelper(js.Overlay, Array{
_, err := root.EvalWithOptions(jsHelper(js.Overlay, JSArgs{
id,
left,
top,
Expand All @@ -89,7 +89,7 @@ func (p *Page) Overlay(left, top, width, height float64, msg string) (remove fun
}

remove = func() {
_, _ = root.EvalWithOptions(jsHelper(js.RemoveOverlay, Array{id}))
_, _ = root.EvalWithOptions(jsHelper(js.RemoveOverlay, JSArgs{id}))
}

return
Expand All @@ -106,7 +106,7 @@ func (p *Page) ExposeJSHelper() *Page {
func (el *Element) Trace(msg string) (removeOverlay func()) {
id := utils.RandString(8)

_, err := el.EvalWithOptions(jsHelper(js.ElementOverlay, Array{
_, err := el.EvalWithOptions(jsHelper(js.ElementOverlay, JSArgs{
id,
msg,
}))
Expand All @@ -115,7 +115,7 @@ func (el *Element) Trace(msg string) (removeOverlay func()) {
}

removeOverlay = func() {
_, _ = el.EvalWithOptions(jsHelper(js.RemoveOverlay, Array{id}))
_, _ = el.EvalWithOptions(jsHelper(js.RemoveOverlay, JSArgs{id}))
}

return
Expand Down Expand Up @@ -144,7 +144,7 @@ func (el *Element) tryTrace(msg string) func() {

var regHelperJS = regexp.MustCompile(`\A\(rod, \.\.\.args\) => (rod\..+)\.apply\(this, `)

func (p *Page) tryTraceFn(js string, params Array) func() {
func (p *Page) tryTraceFn(js string, params JSArgs) func() {
if !p.browser.trace {
return func() {}
}
Expand All @@ -168,7 +168,7 @@ func defaultTraceLogAct(msg string) {
log.Println("act", msg)
}

func defaultTraceLogJS(js string, params Array) {
func defaultTraceLogJS(js string, params JSArgs) {
paramsStr := ""
if len(params) > 0 {
paramsStr = strings.Trim(mustToJSONForDev(params), "[]\r\n")
Expand All @@ -184,11 +184,11 @@ func defaultTraceLogErr(err error) {
}

func (m *Mouse) initMouseTracer() {
_, _ = m.page.EvalWithOptions(jsHelper(js.InitMouseTracer, Array{m.id, assets.MousePointer}))
_, _ = m.page.EvalWithOptions(jsHelper(js.InitMouseTracer, JSArgs{m.id, assets.MousePointer}))
}

func (m *Mouse) updateMouseTracer() bool {
res, err := m.page.EvalWithOptions(jsHelper(js.UpdateMouseTracer, Array{m.id, m.x, m.y}))
res, err := m.page.EvalWithOptions(jsHelper(js.UpdateMouseTracer, JSArgs{m.id, m.x, m.y}))
if err != nil {
return true
}
Expand Down
6 changes: 3 additions & 3 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (el *Element) SelectText(regex string) error {
defer el.tryTrace("select text: " + regex)()
el.page.browser.trySlowmotion()

_, err = el.EvalWithOptions(jsHelper(js.SelectText, Array{regex}))
_, err = el.EvalWithOptions(jsHelper(js.SelectText, JSArgs{regex}))
return err
}

Expand Down Expand Up @@ -221,7 +221,7 @@ func (el *Element) Select(selectors []string) error {
strings.Join(selectors, "; ")))()
el.page.browser.trySlowmotion()

_, err = el.EvalWithOptions(jsHelper(js.Select, Array{selectors}))
_, err = el.EvalWithOptions(jsHelper(js.Select, JSArgs{selectors}))
return err
}

Expand Down Expand Up @@ -333,7 +333,7 @@ func (el *Element) Frame() (*Page, error) {

// ContainsElement check if the target is equal or inside the element.
func (el *Element) ContainsElement(target *Element) (bool, error) {
res, err := el.EvalWithOptions(jsHelper(js.ContainsElement, Array{target.ObjectID}))
res, err := el.EvalWithOptions(jsHelper(js.ContainsElement, JSArgs{target.ObjectID}))
if err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *S) TestIframes() {
s.Panics(func() {
s.stub(1, proto.RuntimeGetProperties{}, func(send func() ([]byte, error)) ([]byte, error) {
d, _ := send()
return sjson.SetBytes(d, "result", rod.Array{})
return sjson.SetBytes(d, "result", rod.JSArgs{})
})
p.MustElementFromNode(id).MustText()
})
Expand Down
2 changes: 1 addition & 1 deletion hijack.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func (p *Page) GetDownloadFile(pattern string, resourceType proto.NetworkResourc

u := downloading.URL
if strings.HasPrefix(u, "blob:") {
res, e := p.EvalWithOptions(jsHelper(js.FetchAsDataURL, Array{u}))
res, e := p.EvalWithOptions(jsHelper(js.FetchAsDataURL, JSArgs{u}))
if e != nil {
err = e
wg.Done()
Expand Down
8 changes: 4 additions & 4 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string) fun

// WaitIdle doc is similar to the method MustWaitIdle
func (p *Page) WaitIdle(timeout time.Duration) (err error) {
_, err = p.EvalWithOptions(jsHelper(js.WaitIdle, Array{timeout.Seconds()}))
_, err = p.EvalWithOptions(jsHelper(js.WaitIdle, JSArgs{timeout.Seconds()}))
return err
}

Expand All @@ -407,15 +407,15 @@ func (p *Page) WaitLoad() error {
func (p *Page) AddScriptTag(url, content string) error {
hash := md5.Sum([]byte(url + content))
id := hex.EncodeToString(hash[:])
_, err := p.EvalWithOptions(jsHelper(js.AddScriptTag, Array{id, url, content}))
_, err := p.EvalWithOptions(jsHelper(js.AddScriptTag, JSArgs{id, url, content}))
return err
}

// AddStyleTag to page. If url is empty, content will be used.
func (p *Page) AddStyleTag(url, content string) error {
hash := md5.Sum([]byte(url + content))
id := hex.EncodeToString(hash[:])
_, err := p.EvalWithOptions(jsHelper(js.AddStyleTag, Array{id, url, content}))
_, err := p.EvalWithOptions(jsHelper(js.AddStyleTag, JSArgs{id, url, content}))
return err
}

Expand Down Expand Up @@ -527,7 +527,7 @@ func (p *Page) EvalWithOptions(opts *EvalOptions) (*proto.RuntimeRemoteObject, e
}

// Wait js function until it returns true
func (p *Page) Wait(thisID proto.RuntimeRemoteObjectID, js string, params Array) error {
func (p *Page) Wait(thisID proto.RuntimeRemoteObjectID, js string, params JSArgs) error {
removeTrace := func() {}
defer removeTrace()

Expand Down
22 changes: 11 additions & 11 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ func (p *Page) HasMatches(pairs ...string) (bool, error) {

// Element finds element by css selector
func (p *Page) Element(selectors ...string) (*Element, error) {
return p.ElementByJS(jsHelper(js.Element, ArrayFromList(selectors)))
return p.ElementByJS(jsHelper(js.Element, JSArgsFromString(selectors)))
}

// ElementMatches doc is similar to the method MustElementMatches
func (p *Page) ElementMatches(pairs ...string) (*Element, error) {
return p.ElementByJS(jsHelper(js.ElementMatches, ArrayFromList(pairs)))
return p.ElementByJS(jsHelper(js.ElementMatches, JSArgsFromString(pairs)))
}

// ElementX finds elements by XPath
func (p *Page) ElementX(xPaths ...string) (*Element, error) {
return p.ElementByJS(jsHelper(js.ElementX, ArrayFromList(xPaths)))
return p.ElementByJS(jsHelper(js.ElementX, JSArgsFromString(xPaths)))
}

// ElementByJS returns the element from the return value of the js function.
Expand Down Expand Up @@ -179,12 +179,12 @@ func (p *Page) ElementByJS(opts *EvalOptions) (*Element, error) {

// Elements doc is similar to the method MustElements
func (p *Page) Elements(selector string) (Elements, error) {
return p.ElementsByJS(jsHelper(js.Elements, Array{selector}))
return p.ElementsByJS(jsHelper(js.Elements, JSArgs{selector}))
}

// ElementsX doc is similar to the method MustElementsX
func (p *Page) ElementsX(xpath string) (Elements, error) {
return p.ElementsByJS(jsHelper(js.ElementsX, Array{xpath}))
return p.ElementsByJS(jsHelper(js.ElementsX, JSArgs{xpath}))
}

// ElementsByJS is different from ElementByJSE, it doesn't do retry
Expand Down Expand Up @@ -332,12 +332,12 @@ func (el *Element) HasMatches(selector, regex string) (bool, error) {

// Element doc is similar to the method MustElement
func (el *Element) Element(selectors ...string) (*Element, error) {
return el.ElementByJS(jsHelper(js.Element, ArrayFromList(selectors)))
return el.ElementByJS(jsHelper(js.Element, JSArgsFromString(selectors)))
}

// ElementX doc is similar to the method MustElementX
func (el *Element) ElementX(xPaths ...string) (*Element, error) {
return el.ElementByJS(jsHelper(js.ElementX, ArrayFromList(xPaths)))
return el.ElementByJS(jsHelper(js.ElementX, JSArgsFromString(xPaths)))
}

// ElementByJS doc is similar to the method MustElementByJS
Expand All @@ -352,7 +352,7 @@ func (el *Element) Parent() (*Element, error) {

// Parents that match the selector
func (el *Element) Parents(selector string) (Elements, error) {
return el.ElementsByJS(jsHelper(js.Parents, Array{selector}))
return el.ElementsByJS(jsHelper(js.Parents, JSArgs{selector}))
}

// Next doc is similar to the method MustNext
Expand All @@ -367,17 +367,17 @@ func (el *Element) Previous() (*Element, error) {

// ElementMatches doc is similar to the method MustElementMatches
func (el *Element) ElementMatches(pairs ...string) (*Element, error) {
return el.ElementByJS(jsHelper(js.ElementMatches, ArrayFromList(pairs)))
return el.ElementByJS(jsHelper(js.ElementMatches, JSArgsFromString(pairs)))
}

// Elements doc is similar to the method MustElements
func (el *Element) Elements(selector string) (Elements, error) {
return el.ElementsByJS(jsHelper(js.Elements, Array{selector}))
return el.ElementsByJS(jsHelper(js.Elements, JSArgs{selector}))
}

// ElementsX doc is similar to the method MustElementsX
func (el *Element) ElementsX(xpath string) (Elements, error) {
return el.ElementsByJS(jsHelper(js.ElementsX, Array{xpath}))
return el.ElementsByJS(jsHelper(js.ElementsX, JSArgs{xpath}))
}

// ElementsByJS doc is similar to the method MustElementsByJS
Expand Down
26 changes: 12 additions & 14 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"net/url"
"path/filepath"
"reflect"
"regexp"
"time"

Expand Down Expand Up @@ -39,15 +38,14 @@ func ensureSleeper(gen func() utils.Sleeper) func() utils.Sleeper {
return gen
}

// Array of any type
type Array []interface{}
// JSArgs for eval
type JSArgs []interface{}

// ArrayFromList converts a random list into Array type
func ArrayFromList(list interface{}) Array {
arr := Array{}
val := reflect.ValueOf(list)
for i := 0; i < val.Len(); i++ {
arr = append(arr, val.Index(i).Interface())
// JSArgsFromString converts a string list into Array type
func JSArgsFromString(list []string) JSArgs {
arr := JSArgs{}
for _, s := range list {
arr = append(arr, s)
}
return arr
}
Expand All @@ -65,7 +63,7 @@ type EvalOptions struct {
JS string

// JSArgs of the js function
JSArgs Array
JSArgs JSArgs
}

// This set the ThisID
Expand All @@ -81,16 +79,16 @@ func (e *EvalOptions) ByObject() *EvalOptions {
}

// NewEvalOptions creates a new EvalPayload
func NewEvalOptions(js string, jsArgs Array) *EvalOptions {
return &EvalOptions{true, "", js, jsArgs}
func NewEvalOptions(js string, args JSArgs) *EvalOptions {
return &EvalOptions{true, "", js, args}
}

const jsHelperID = proto.RuntimeRemoteObjectID("rodJSHelper")

// Convert name and jsArgs to Page.Eval, the name is method name in the "lib/assets/helper.js".
func jsHelper(name js.Name, args Array) *EvalOptions {
func jsHelper(name js.Name, args JSArgs) *EvalOptions {
return &EvalOptions{
JSArgs: append(Array{jsHelperID}, args...),
JSArgs: append(JSArgs{jsHelperID}, args...),
JS: fmt.Sprintf(`(rod, ...args) => rod.%s.apply(this, args)`, name),
}
}
Expand Down

0 comments on commit 18dd2d9

Please sign in to comment.