playwright-go/binding_call.go
Can Stand d2aa790e89
Some checks failed
Go / Lint (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, oldstable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, oldstable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, oldstable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, stable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, stable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (chromium, stable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, oldstable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, oldstable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, oldstable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, stable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, stable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (firefox, stable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, oldstable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, oldstable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, oldstable, windows-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, stable, macos-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, stable, ubuntu-latest) (push) Has been cancelled
Go / ${{ matrix.browser }} on ${{ matrix.os }}, go ${{ matrix.go }} (webkit, stable, windows-latest) (push) Has been cancelled
Go / test-examples (push) Has been cancelled
Docs / Deploy docs (push) Has been cancelled
Verify Types / verify (push) Has been cancelled
Go / finish (push) Has been cancelled
chore: maintain StorageState type (#583)
2026-02-23 09:13:11 -08:00

88 lines
2.2 KiB
Go

package playwright
import (
"fmt"
"strings"
"github.com/go-stack/stack"
)
type BindingCall interface {
Call(f BindingCallFunction)
}
type bindingCallImpl struct {
channelOwner
}
// BindingSource is the value passed to a binding call execution
type BindingSource struct {
Context BrowserContext
Page Page
Frame Frame
}
// ExposedFunction represents the func signature of an exposed function
type ExposedFunction = func(args ...any) any
// BindingCallFunction represents the func signature of an exposed binding call func
type BindingCallFunction func(source *BindingSource, args ...any) any
func (b *bindingCallImpl) Call(f BindingCallFunction) {
defer func() {
if r := recover(); r != nil {
if _, err := b.channel.Send("reject", map[string]any{
"error": serializeError(r.(error)),
}); err != nil {
logger.Error("could not reject BindingCall", "error", err)
}
}
}()
frame := fromChannel(b.initializer["frame"]).(*frameImpl)
source := &BindingSource{
Context: frame.Page().Context(),
Page: frame.Page(),
Frame: frame,
}
var result any
if handle, ok := b.initializer["handle"]; ok {
result = f(source, fromChannel(handle))
} else {
initializerArgs := b.initializer["args"].([]any)
funcArgs := []any{}
for i := range initializerArgs {
funcArgs = append(funcArgs, parseResult(initializerArgs[i]))
}
result = f(source, funcArgs...)
}
_, err := b.channel.Send("resolve", map[string]any{
"result": serializeArgument(result),
})
if err != nil {
logger.Error("could not resolve BindingCall", "error", err)
}
}
func serializeError(err error) map[string]any {
st := stack.Trace().TrimRuntime()
if len(st) == 0 { // https://github.com/go-stack/stack/issues/27
st = stack.Trace()
}
return map[string]any{
"error": &Error{
Name: "Playwright for Go Error",
Message: err.Error(),
Stack: strings.ReplaceAll(strings.TrimFunc(fmt.Sprintf("%+v", st), func(r rune) bool {
return r == '[' || r == ']'
}), " ", "\n"),
},
}
}
func newBindingCall(parent *channelOwner, objectType string, guid string, initializer map[string]any) *bindingCallImpl {
bt := &bindingCallImpl{}
bt.createChannelOwner(bt, parent, objectType, guid, initializer)
return bt
}