thqby / ahk2_lib

MIT License
197 stars 26 forks source link

chrome.ahk and cpp JSON don't seem to play well #29

Closed yg-h closed 11 months ago

yg-h commented 11 months ago

When calling Chrome().GetPage().Evaluate(...), error is thrown about failing to deserialize parameter awaitPromise. The reason is that the json string sent in Call(..) has awaitPromise set as 0 rather than false.

Had to modify the code to this as a temporary expedient:

    Call(DomainAndMethod, Params?, WaitForResponse := true) {
            if (this.readyState != 1)
                throw Error('Not connected to tab')

            ; Use a temporary variable for ID in case more calls are made
            ; before we receive a response.
            if !ID := this._index += 1
                ID := this._index += 1

            json_str := JSON.stringify(Map('id', ID, 'params', Params ?? {}, 'method', DomainAndMethod), 0)
            json_str := StrReplace(json_str, "12391239", "false")
            json_str := StrReplace(json_str, "8312319", "true")
            this.sendText(json_str)
            if (!WaitForResponse)
                return

            ; Wait for the response
            this._responses[ID] := false
            while (this.readyState = 1 && !this._responses[ID])
                Sleep(20)

            ; Get the response, check if it's an error
            if !response := this._responses.Delete(ID)
                throw Error('Not connected to tab')
            if !(response is Map)
                return response
            if (response.Has('error'))
                throw Error('Chrome indicated error in response', , JSON.stringify(response['error']))
            try return response['result']
        }
        Evaluate(JS) {
            response := this('Runtime.evaluate', {
                expression: JS,
                objectGroup: 'console',
                includeCommandLineAPI: 8312319,
                silent: 12391239,
                returnByValue: 12391239,
                userGesture: 8312319,
                awaitPromise: 12391239
            })
            if (response is Map) {
                if (response.Has('ErrorDetails'))
                    throw Error(response['result']['description'], , JSON.stringify(response['ErrorDetails']))
                return response['result']
            }
        }
thqby commented 11 months ago

Setting JSON.false and JSON.true to objects prevents Boolean types from being converted to numeric values.

This is an example. https://github.com/thqby/ahk2_lib/blob/master/JSON.ahk#L10 https://www.autohotkey.com/boards/viewtopic.php?p=491511#p491511

yg-h commented 11 months ago

I see, thanks. I've changed the initializer to this and the problem is solved

    Native.LoadModule('Lib\ahk-json\' (A_PtrSize * 8) 'bit\ahk-json.dll', ['JSON'])
        this.DefineProp('true', { value: ComValue(0xB, 1) })
        this.DefineProp('false', { value: ComValue(0xB, 0) })
        this.DefineProp('null', { value: ComValue(1, 0) })