@@ -138,7 +138,7 @@ func wrapCustomCommand(method string, params map[string]any, sessionID string) m
138138 m , _ := json .Marshal (method )
139139 p , _ := json .Marshal (params )
140140 sid , _ := json .Marshal (sessionID )
141- return callFunctionParams (fmt .Sprintf (`async function() { return await globalThis.ModCDP.handleCommand(%s, %s, %s); }` , string (m ), string (p ), string (sid )))
141+ return callFunctionParams (fmt .Sprintf (`async function() { return JSON.stringify( await globalThis.ModCDP.handleCommand(%s, %s, %s) ); }` , string (m ), string (p ), string (sid )))
142142}
143143
144144func wrapServiceWorkerCommand (method string , params map [string ]any , sessionID string , targetSessionID string ) []rawStep {
@@ -179,7 +179,11 @@ func wrapServiceWorkerCommand(method string, params map[string]any, sessionID st
179179 }
180180 runtimeParams = wrapCustomCommand (method , params , cdpSessionID )
181181 }
182- return []rawStep {{Method : "Runtime.callFunctionOn" , Params : runtimeParams , Unwrap : "runtime" }}
182+ unwrap := "runtime_json"
183+ if strings .HasPrefix (method , "Mod." ) {
184+ unwrap = "runtime"
185+ }
186+ return []rawStep {{Method : "Runtime.callFunctionOn" , Params : runtimeParams , Unwrap : unwrap }}
183187}
184188
185189func WrapCommandIfNeeded (method string , params map [string ]any , routes map [string ]string , sessionID string , targetSessionID ... string ) (rawCommand , error ) {
@@ -198,7 +202,7 @@ func WrapCommandIfNeeded(method string, params map[string]any, routes map[string
198202}
199203
200204func UnwrapResponseIfNeeded (result map [string ]any , unwrap string ) (any , error ) {
201- if unwrap != "runtime" {
205+ if unwrap != "runtime" && unwrap != "runtime_json" {
202206 return result , nil
203207 }
204208 if ex , ok := result ["exceptionDetails" ].(map [string ]any ); ok {
@@ -219,7 +223,17 @@ func UnwrapResponseIfNeeded(result map[string]any, unwrap string) (any, error) {
219223 return nil , fmt .Errorf ("%s" , msg )
220224 }
221225 inner , _ := result ["result" ].(map [string ]any )
222- return inner ["value" ], nil
226+ value := inner ["value" ]
227+ if unwrap == "runtime_json" {
228+ if raw , ok := value .(string ); ok {
229+ var decoded any
230+ if err := json .Unmarshal ([]byte (raw ), & decoded ); err != nil {
231+ return nil , err
232+ }
233+ return decoded , nil
234+ }
235+ }
236+ return value , nil
223237}
224238
225239func UnwrapEventIfNeeded (method string , params map [string ]any , sessionID string , ourSessionID string ) (string , any , bool ) {
0 commit comments