Skip to content

How to set local variable to post message whenever a new one is listened #305

@erdemyerebasmaz

Description

@erdemyerebasmaz

Hello,

I can listen to post messages on browser like this but I didn't quite understand how I can set a local variable to event.data.

String script1 = 'window.addEventListener("message", receiveMessage, false);' +
                'function receiveMessage(event) {console.log(event.data);}';
widgetWebview.evalJavascript(script);

I tried

String script2 = 'var test = "Hello, World!";'+
                 'function getMessage(val) {return val;}' +
                 'getMessage(test);';
var testLocal = await widgetWebview.evalJavascript(script2);

and you can set testLocal to test but I couldn't apply it to post message.

Also, I tried setting value of test inside script1 and trigger onUrlChanged inside receiveMessage function and access it from script2. The variables in script1 were not accessible by script2.

Can we use a callback function to communicate between app & webview?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions