Comments (11)
I figured it out. Braintree was emitting an event that was being picked up in the react-native-webview-messaging/WebView component. The data from the event.nativeEvent object was a string that began with "/event/{..." the beginning portion is what was screwing it up.
What do you think about wrapping the JSON.parse(data) call in a try/catch in attempt to avoid errors like this? I tried it on a local version and it seemed to work.
from react-native-webview-messaging.
@reggie3 sorry for long reply, I've been on vacation. Could you please provide exact object which causes this error?
from react-native-webview-messaging.
I hope your vacation was nice.
The problem is caused when a webview contains code that emits a message itself. For example, the following code in the Braintree JavaScript SDK can be put into a WebView (via the source prop) in order to display a payment form:
<head>
<meta charset="utf-8">
<script src="https://js.braintreegateway.com/web/dropin/1.7.0/js/dropin.min.js"></script>
</head>
<body>
<div id="dropin-container"></div>
<button id="submit-button">Request payment method</button>
<script>
var button = document.querySelector('#submit-button');
braintree.dropin.create({
authorization: 'CLIENT_TOKEN_FROM_SERVER',
container: '#dropin-container'
}, function (createErr, instance) {
button.addEventListener('click', function () {
instance.requestPaymentMethod(function (err, payload) {
// Submit payload.nonce to your server
});
});
});
</script>
</body>
The form that is displayed includes a submit button that executes the code in the event listener. The code that is activated on click emits an event on its own as part of the Braintree SDK's code. The user is unable to control the format of that event. I don't have the full message available to me right now, but it begins with "/event/{..." which does not match react-native-webview-messaging's expected format for an event. This results in the attempt to parse the event on line 29 of your library's WebView.js file failing with the above error.
However, wrapping that JSON.parse(data)
call along with the switch statement that follows in a try/catch statement keeps the error from happening.
from react-native-webview-messaging.
I'd better add unique hash to messages posted using send
/sendJson
/emit
and check this hash in RN WebView to avoid confusion in case someone will post actual json with type
field using window.postMessage
. This approach seems more accurate as it fixes one more potential issue + try/catch could affect performance
from react-native-webview-messaging.
could you also give me short overview of how are you going to combine this module with Braintree SDK? This seems quite tricky to me and I'd expect more difficulties 😞
from react-native-webview-messaging.
Now that you mention it, only using the try/catch might result in unexpected messages being received. However, how would you package a hash in such a way that it can be decoded without potentially trying to JSON.parse something that isn't JSON?
I was working on a picture for a blog post to describe the process, so hopefully that will do a better job of explaining how I plan to use this module with Braintree. The bolded & underlined actions are what I plan to use your module for.
from react-native-webview-messaging.
how would you package a hash in such a way that it can be decoded without potentially trying to JSON.parse something that isn't JSON?
I'd implement smth like this
Stringify:
const dataToEmit = { a: 1};
const stringified = JSON.stringify(dataToEmit);
const withHash = SOME_MAGIC_VALUE + stringified;
window.postMessage(withHash);
Parse:
if (data.indexOf(SOME_MAGIC_VALUE) !== 0) {
return; // that's not something that was received from rn messages channel
}
const jsonString = data.replace(SOME_MAGIC_VALUE, '');
const parsed = JSON.parse(jsonString);
Thanks for illustration, now it's more clear 👍
from react-native-webview-messaging.
That's a pretty cool solution!
from react-native-webview-messaging.
SOME_MAGIC_VALUE doesn't need to change, it can be hardwired into your library, correct? It just has to be something unique that isn't likely to be created by some other library. Would a GUID suffice?
from react-native-webview-messaging.
Yes, you're right. Sorry, had no spare time to address this issue yet 😞
from react-native-webview-messaging.
I'll try to implement your technique above using a GUID and submit a pull request.
from react-native-webview-messaging.
Related Issues (20)
- Android 4.4.4 below, react native cannot call the web side method HOT 2
- Failed to execute 'postMessage' on 'Window': 2 arguments required, but only 1 present HOT 2
- Setting onMessage on a WebView overrides existing values of window.postMessage HOT 9
- Event call on Android but not on iOS HOT 3
- await connectToRemote() not returning a connection or error HOT 4
- (V1) Cannot run on Android HOT 2
- Android Javascript injection not working. HOT 16
- Is there an example to send data to RN in pure html/js and not generated? HOT 1
- cant debug. missing setWebContentsDebuggingEnabled(true) method HOT 1
- webview messagesChannel not working on event listener callback
- file miss HOT 1
- Migrate to latest react native version
- The input type file does not work in webview. from android
- Doesn't seem to work on iOS 9.3 HOT 3
- react-native bundling fails when `require`-ing a html file HOT 6
- onPermissionRequest HOT 2
- Can you provide a working HTML? HOT 1
- "Module JSTimersExecution is not a registered callable module (calling callTimers)" Error HOT 3
- Migrate expo example to v20 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-webview-messaging.