Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebMessageListener onPostMessage message is empty #2479

Open
2 tasks done
imoyakin opened this issue Dec 27, 2024 · 1 comment
Open
2 tasks done

WebMessageListener onPostMessage message is empty #2479

imoyakin opened this issue Dec 27, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@imoyakin
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

On macos, WebMessageListener onPostMessage executes postmessage in userscript. In callback, message is always empty, but sourceOrigin has data.

typedef void OnPostMessageCallback(WebMessage? message, WebUri? sourceOrigin,
bool isMainFrame, PlatformJavaScriptReplyProxy replyProxy);

Expected Behavior

message has date

Steps with code example to reproduce

I'm use this userscript to catch the web game request:

const OriginalXMLHttpRequest = XMLHttpRequest

XMLHttpRequest = class HackedXMLHttpRequest extends OriginalXMLHttpRequest {
    constructor() {
        super()

        this.method = 'GET'
        this.requestURL = ''
        this.request = ''
        this.responseHack = undefined
        this.responseTextHack = undefined
        this.responseXMLHack = undefined

        this.addEventListener('load', () => {
            SendToFlutter(this, 'load')
        })
        this.addEventListener('loadend', () => {
            if (!this.responseType || ['json', 'text'].includes(this.responseType)) {
                SendToFlutter(this, 'loadend')
            }
        })
        this.addEventListener('error', () => {
            SendToFlutter(this, 'error')
        })
    }

    open(method, requestURL, ...props) {
        this.method = method
        this.requestURL = new URL(requestURL, location.href).href
        super.open(method, requestURL, ...props)
    }

    send(body) {
        this.request = body
        super.send(body)
    }

    get response() {
        return this.responseHack || super.response
    }

    set response(response) {
        this.responseHack = response
    }

    get responseText() {
        return this.responseTextHack || super.responseText
    }

    set responseText(responseText) {
        this.responseTextHack = responseText
    }

    get responseXML() {
        return this.responseXMLHack || super.responseXML
    }

    set responseXML(responseXML) {
        this.responseXMLHack = responseXML
    }
}

function SendToFlutter(xhr, type) {
    let messageData;
    if (!xhr.responseURL.includes('/kcsapi/')) {
        return;
    }

    if (type === 'load') {
        messageData = {
            type: type,
            method: xhr.method,
            response_url: xhr.responseURL,
            request_url: xhr.requestURL,
            response: xhr.response,
            request: xhr.request,
            status: xhr.status,
            response_type: xhr.responseType
        };
    } else if (type === 'loadend') {
        messageData = {
            type: type,
            method: xhr.method,
            response_url: xhr.responseURL,
            request_url: xhr.requestURL,
            response: xhr.response,
            request: xhr.request,
            status: xhr.status,
            response_type: xhr.responseType
        };
    } else if (type === 'error') {
        messageData = {
            type: type,
            response_url: xhr.responseURL,
            request_url: xhr.requestURL,
            status: xhr.status
        };
    }

    const jsonString = JSON.stringify(messageData);
    kcMessage.postMessage(jsonString);
}

and i make sure the postMessage have date on it.
but when i received in flutter

                WebMessageListener kcl = WebMessageListener(
                  jsObjectName: "kcMessage",
                  onPostMessage:
                      (message, sourceOrigin, isMainFrame, replyProxy) {
                    var json = jsonDecode(message?.data);
                    print(json);
                  },
                );

the message always is empty

Stacktrace/Logs

Stacktrace/Logs When I located the problem through flutter, I found that in the code `/flutter/packages/flutter/lib/src/services/platform_channel.dart:569`, the information sent from the userscript can only be parsed to ``` onPostMessage sourceOrigin http://w02k.kancolle-server.com message ``` That might means when the date returned by method_channel, the specific content of the message is not attached. i will try to debug the swift

Flutter version

3.27.1

Operating System, Device-specific and/or Tool

macOS 15.1.1, xcode 16

Plugin version

master branch,lastest release

Additional information

No response

Self grab

  • I'm ready to work on this issue!
@imoyakin imoyakin added the bug Something isn't working label Dec 27, 2024
@imoyakin
Copy link
Author

by safari devtools,i make sure the postMessage is exist,such as

{"type":"load","method":"POST","response_url":"http://w02k.kancolle-server.com/kcsapi/api_req_member/get_incentive","request_url":"http://w02k.kancolle-server.com/kcsapi/api_req_member/get_incentive","response":"svdata={\"api_result\":1,\"api_result_msg\":\"\\u6210\\u529f\",\"api_data\":{\"api_count\":0}}","request":"api_token=************************************&api_verno=1","status":200,"response_type":""}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant