shripalsoni04 / nativescript-webview-interface

Plugin for bi-directional communication between webView and android/ios
MIT License
89 stars 35 forks source link

Quote string messages in URL for Android #6

Closed saulshanabrook closed 8 years ago

saulshanabrook commented 8 years ago

Previously, if you sent a string message with " in it, it would not be quoted and the javascript would fail.

shripalsoni04 commented 8 years ago

Thanks for the PR.

In demo app of this plugin, I tried to send string with " using callJSFunction as shown below.

// Case 1: sending object having string with " in it
export function getSelectedLanguage(){
   oLangWebViewInterface.callJSFunction('getSelectedLanguage', {name: 'hello "user"'}, (oSelectedLang) => {
        alert(`Selected Language is ${oSelectedLang.text}`);
    });
}
// Case 2: sending string with " in it
export function getSelectedLanguage(){
   oLangWebViewInterface.callJSFunction('getSelectedLanguage', 'hello "user"', (oSelectedLang) => {
        alert(`Selected Language is ${oSelectedLang.text}`);
    });
}

And in JS file in web-view, I added console statements to see the passed value as shown below:

window.getSelectedLanguage = function(arg){
            console.log('argument is ');
            console.log(JSON.stringify(arg)); // prints {"name":"hello \"user\""} for first case
            console.log(arg); // prints hello "user" for second case
           ...
}

As shown above, In both the cases I am getting string with " in it on console in web-view.

It will be great, if you can share with me what data you are sending, so I can check where and why it is breaking or you can just test that in the demo app as mentioned above.

Thanks.

saulshanabrook commented 8 years ago

Sure. I modified the example app to send the initial languages as a string, by using JSON.stringify in the app and JSON.parse in the webview.

I also changed it to emit that string every time you clicked "Add" so that you can manually trigger the error. Then I opened up the chrome remote device inspector while the app was open and looked at the error that occurred when I clicked "Add":

screen shot 2016-09-06 at 11 39 12 am

You can see it is executing some javascript without properly quoting the ". This occurs on both Android and iOS.

shripalsoni04 commented 8 years ago

Thanks for applying changes to the demo app. It really helped me to identify from where the issue was coming.

I have done changes to resolve this issue and now you should be able to pass " in message.

Calling encodeURIComponent may solve this issue, but the problem with that is, it will increase the payload size. So if someone is sending large data to webview (like image), in android the max limit can be reached and the functionality may not work.

I have resolved this by always converting data to string using JSON.stringify() before sending it to web-view. So if data contains ", stringify function will escape that character.

New version of this plugin with the updated code is published at 1.3.2 version.

Thanks again for pointing this issue and the PR.

saulshanabrook commented 8 years ago

@shripalsoni04 Thank you for addressing this so quickly!