diff --git a/browser/mapping.go b/browser/mapping.go index a53beb76b..e5239eeb9 100644 --- a/browser/mapping.go +++ b/browser/mapping.go @@ -3,6 +3,7 @@ package browser import ( "context" "fmt" + "reflect" "time" "github.com/dop251/goja" @@ -811,8 +812,34 @@ func mapWorker(vu moduleVU, w *common.Worker) mapping { func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolint:funlen rt := vu.Runtime() return mapping{ - "addCookies": bc.AddCookies, - "addInitScript": bc.AddInitScript, + "addCookies": bc.AddCookies, + "addInitScript": func(script goja.Value) error { + if !gojaValueExists(script) { + return nil + } + + source := "" + switch script.ExportType() { + case reflect.TypeOf(string("")): + source = script.String() + case reflect.TypeOf(goja.Object{}): + opts := script.ToObject(rt) + for _, k := range opts.Keys() { + if k == "content" { + source = opts.Get(k).String() + } + } + default: + _, isCallable := goja.AssertFunction(script) + if !isCallable { + source = fmt.Sprintf("(%s);", script.ToString().String()) + } else { + source = fmt.Sprintf("(%s)(...args);", script.ToString().String()) + } + } + + return bc.AddInitScript(source) //nolint:wrapcheck + }, "browser": bc.Browser, "clearCookies": bc.ClearCookies, "clearPermissions": bc.ClearPermissions, diff --git a/common/browser_context.go b/common/browser_context.go index dc8ae216a..f736df525 100644 --- a/common/browser_context.go +++ b/common/browser_context.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/url" - "reflect" "strings" "time" @@ -109,18 +108,13 @@ func NewBrowserContext( } } - rt := b.vu.Runtime() - k6Obj := rt.ToValue(js.K6ObjectScript) - wv := rt.ToValue(js.WebVitalIIFEScript) - wvi := rt.ToValue(js.WebVitalInitScript) - - if err := b.AddInitScript(k6Obj, nil); err != nil { + if err := b.AddInitScript(js.K6ObjectScript); err != nil { return nil, fmt.Errorf("adding k6 object to new browser context: %w", err) } - if err := b.AddInitScript(wv, nil); err != nil { + if err := b.AddInitScript(js.WebVitalIIFEScript); err != nil { return nil, fmt.Errorf("adding web vital script to new browser context: %w", err) } - if err := b.AddInitScript(wvi, nil); err != nil { + if err := b.AddInitScript(js.WebVitalInitScript); err != nil { return nil, fmt.Errorf("adding web vital init script to new browser context: %w", err) } @@ -128,38 +122,13 @@ func NewBrowserContext( } // AddInitScript adds a script that will be initialized on all new pages. -func (b *BrowserContext) AddInitScript(script goja.Value, arg goja.Value) error { +func (b *BrowserContext) AddInitScript(script string) error { b.logger.Debugf("BrowserContext:AddInitScript", "bctxid:%v", b.id) - rt := b.vu.Runtime() - - source := "" - if gojaValueExists(script) { - switch script.ExportType() { - case reflect.TypeOf(string("")): - source = script.String() - case reflect.TypeOf(goja.Object{}): - opts := script.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "content": - source = opts.Get(k).String() - } - } - default: - _, isCallable := goja.AssertFunction(script) - if !isCallable { - source = fmt.Sprintf("(%s);", script.ToString().String()) - } else { - source = fmt.Sprintf("(%s)(...args);", script.ToString().String()) - } - } - } - - b.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, source) + b.evaluateOnNewDocumentSources = append(b.evaluateOnNewDocumentSources, script) for _, p := range b.browser.getPages() { - if err := p.evaluateOnNewDocument(source); err != nil { + if err := p.evaluateOnNewDocument(script); err != nil { return fmt.Errorf("adding init script to browser context: %w", err) } }