//go:build browser package slidestest // Steps the browser tests share, on chromedp v0.21's generic API // (rastrillo v0.30 and later). They keep the shapes the drives were // written against on chromedp v0.14, so a port is a rename and not a // rewrite of what a drive reads. import ( "context" "encoding/json" "reflect" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/network" "github.com/chromedp/cdproto/runtime" "github.com/chromedp/chromedp" ) // evalInto is a step that evaluates js and decodes the result into out, // as chromedp v0.14's Evaluate(js, out) did: a script that returns null // leaves out as it was (or nil, for a slice, map or pointer), and one // that returns undefined fails unless out can be nil. v0.21's Evaluate // fails on null for a type that cannot be nil. func evalInto[T any](out *T, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { return evalValue(out, js, opts...) } // evalValue is evalInto for an out that is already an any — a pointer, // or nil to drop the result — as v0.14's Evaluate took it. func evalValue(out any, js string, opts ...chromedp.EvaluateOption) chromedp.Action[chromedp.Void] { // By value, set before the caller's options as v0.14 set it. all := append([]chromedp.EvaluateOption{func(p *runtime.EvaluateParams) { p.ReturnByValue = new(true) }}, opts...) return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { v, err := chromedp.Evaluate[*runtime.RemoteObject](js, all...)(ctx, t) if err != nil || out == nil { return err } return decodeV014(v, out) }) } // decodeV014 is chromedp v0.14's parseRemoteObject for a by-value // result. func decodeV014(v *runtime.RemoteObject, out any) error { if raw, ok := out.(*[]byte); ok { *raw = v.Value return nil } value := []byte(v.Value) if value == nil { if rv := reflect.ValueOf(out); rv.Kind() == reflect.Pointer { switch rv.Elem().Kind() { case reflect.Pointer, reflect.Map, reflect.Slice, reflect.Chan, reflect.Func, reflect.Interface: default: if v.Type == runtime.RemoteObjectTypeUndefined { return chromedp.ErrJSUndefined } // v0.14's protocol decoder kept a JSON null as the // bytes "null", which leave out as it was. } } value = []byte("null") } return json.Unmarshal(value, out) } // cdpStep is a step that sends one protocol command and drops its // result, as running a v0.14 cdproto command as an action did. func cdpStep[P, R any](c cdp.Command[P, R], params P) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { _, err := cdp.Call(ctx, t, c, params) return err }) } // attributeInto is a step that reads the attribute name of the first // node sel matches into value, and whether it exists into ok when ok is // not nil, as v0.14's AttributeValue(sel, name, value, ok) did. func attributeInto(sel chromedp.CSS, name string, value *string, ok *bool, opts ...chromedp.QueryOption) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { r, err := chromedp.AttributeValue(sel, name, opts...)(ctx, t) if err != nil { return err } *value = r.Value if ok != nil { *ok = r.Exists } return nil }) } // blockedURLsParams is Network.setBlockedURLs with the urls field the // drives were written against: wildcard patterns, matched anywhere in a // URL. cdproto v0.158 carries only urlPatterns, which take the // URLPattern syntax and must be absolute, so the old field is sent // as it was. type blockedURLsParams struct { URLs []string `json:"urls"` } var setBlockedURLs = cdp.Command[blockedURLsParams, cdp.Empty]{Method: network.CommandSetBlockedURLs} // blockURLs is a step that blocks the URLs matching patterns, or none // when it has none, as v0.14's network.SetBlockedURLs(patterns) did. func blockURLs(patterns ...string) chromedp.Action[chromedp.Void] { return cdpStep(setBlockedURLs, blockedURLsParams{URLs: append([]string{}, patterns...)}) }