You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[WebView] onMessage failing when there are JS warnings and errors on the page #10865
I load the app with this example and I have it pointed to a website (rather not say which one. Sorry.) And the website is emitting 2 errors into the Chrome console when I go there.
(index):994 A Parser-blocking, cross-origin script, http://example.com/thing.js, is invoked via document.write. This may be blocked by the browser if the device has poor network connectivity.
widgets.js:8 Uncaught TypeError: Cannot set property '[object Array]' of undefined(…)
Other websites like google.com and github.com are just fine. If you want to replicate it, change the uri to yahoo.com
Additional Information
React Native version: 0.37
Platform: iOS 10.1, iPhone 6 emulator
Operating System: Mac OSX Sierra 10.12.1
Reacted by Mark Allen, Flyover, Renato, Joe Van Leeuwen, Dominik Skalník, Andrea Cresta, Faraz Sherwani, Cristiano Santos, Abe Clark, Sajin Shrestha and 68 more
Reacted by amit-saini, James Maidment, khanhtdbse, Emilio Martinez, Michael Ford and Vasilii
I should also add that I attempted to replicate the error on Android 7.0 and API 24 using a Nexus_5x emulator, and I got NO error. So this seems to be a platform specific error.
I'm getting the same error. Seems like this exception is thrown when window.postMessage has been overwritten. But since webViewDidFinishLoad is called multiple times, it tries to overwrite window.postMessage over and over. Not sure though. Can we have you expertise @jacobp100?
It’s because the page you’re visiting is overriding postMessage, which may be due to a shim. Showing an error was chosen over invoking the shim’s value of postMessage when you call the value of postMessage that we set.
I thought I’d made it easier to workaround this problem, but it looks like that won’t work. The best you can do at the moment, which is an awful hack, is,
Well, when I try String(window.postMessage) === String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage') on the page I visit, it returns true, so I am guessing postMessage has not been overridden.
Oh I see. The JS is injected after postMessage is overwritten. Your best bet is to fork it then, and I’ll see what I can do about getting this fixed! Sorry. :(
Reacted by Sylvain Reucherand, watzak, Darren Camp and Karl Tarvas
and in my ../lib/html/somefile.html' I have javascript code doing both window.postMessage() and document.addEventListener("message",.......
Everything worked fine when I had the HTML in a variable "someHtml", like:
source={{html: someHtml}}
So difference seem to be when changing source to HTML-file instead of HTML-string.
Still for me I had other reasons, not related to this, why I really need to read HTML from file instead of string so it would be great to get this issue fixed.
Reacted by Pawan Sharma, Kevin, Muhammad Arsal Abdullah and Jesse Vander Does
Reacted by Renato, Jeduan Cornejo, hyochan, Jack Clark, PatrickBenz09, Hen Greville, Peter Pistorius, Vyacheslav Ivantsov, Soul Theory, Dan Homola and 8 more
Reacted by Simon Bolduc, Mark Allen, PAMPANG, Mang-Git Ng, Zino Hofmann, Guilherme da Silva Mello, Joe Van Leeuwen, Dean Wake, Kirill, Eldad Bercovici and 150 more
Reacted by Hamsin, DV, Conor McGrath, maxperry, pallyoung, Tobe, Maurício R Duarte, osiellima, Lei Xiao, Muhammad Husnain Tahir and 12 more
Reacted by yaoyin, Alex, Maxim Kolesnikov, Hamsin, Casey Garland, Anis Jonischkeit, DV, Esa-Matti Suuronen, Kurtis Kemple, Pete and 35 more
Reacted by Saeid reza, Donato Azevedo, Tobe, osiellima, Karl Castillo, Max, Lei Xiao, Serdar, Milan Kamilya, TORIKI and 18 more
This issue will not occur is you use "useWebKit" prop of webview, that will use WK webview instead of UI webview in case of IOS.
But in case if you don't want to use WK webview, then below is the sample JS code that you can inject in you Webview with "injectedJavaScript" prop.
This issue will not occur is you use "useWebKit" prop of webview, that will use WK webview instead of UI webview in case of IOS.
But in case if you don't want to use WK webview, then below is the sample JS code that you can inject in you Webview with "injectedJavaScript" prop.
The above code did nothing just patched window.postMessage function, the above issue will not appear.
The patched script makes my onMessage to be always called twice in iOS when i do a click event...
The useWebKit prop was the only way, at least for now...
This will prevent error from above but also this will prevent in some cases to use window.postMessage from your injected JS code. To go around this just wrap window.postMessage(msg) in timeout like this: setTimeout(() => { window.postMessage(JSON.stringify(postMessage)); }, 100);
Is there an updated version of this answer which will work with the new version of react-native-webview ? I am unable to receive messages in onMessage() on both iOS and Android. :(
Is there an updated version of this answer which will work with the new version of react-native-webview ? I am unable to receive messages in onMessage() on both iOS and Android. :(
Answering my own question here for everyone else's reference -
Is there an updated version of this answer which will work with the new version of react-native-webview ? I am unable to receive messages in onMessage() on both iOS and Android. :(
Answering my own question here for everyone else's reference -
This did solve the issue with the red screen, but when I tried to post data back from my frontend code calling window.postMessage('some stuff'. '*') I got error TypeError: undefined is not an object (evaluating 'window.ReactNativeWebView.postMessage') Any ideas?
Is there an updated version of this answer which will work with the new version of react-native-webview ? I am unable to receive messages in onMessage() on both iOS and Android. :(
This did solve the issue with the red screen, but when I tried to post data back from my frontend code calling window.postMessage('some stuff'. '*') I got error TypeError: undefined is not an object (evaluating 'window.ReactNativeWebView.postMessage') Any ideas?
As a result, our company is now moving away from React Native for Android. Sadly, there are too many breaking changes and too many security vulnerabilities in third party libraries.
Activity
raiderrobert commentedon Nov 10, 2016
I should also add that I attempted to replicate the error on Android 7.0 and API 24 using a Nexus_5x emulator, and I got NO error. So this seems to be a platform specific error.
sreucherand commentedon Nov 11, 2016
I'm getting the same error. Seems like this exception is thrown when
window.postMessagehas been overwritten. But sincewebViewDidFinishLoadis called multiple times, it tries to overwrite window.postMessage over and over. Not sure though. Can we have you expertise @jacobp100?jacobp100 commentedon Nov 11, 2016
It’s because the page you’re visiting is overriding
postMessage, which may be due to a shim. Showing an error was chosen over invoking the shim’s value ofpostMessagewhen you call the value ofpostMessagethat we set.I thought I’d made it easier to workaround this problem, but it looks like that won’t work. The best you can do at the moment, which is an awful hack, is,
Or just fork the project and remove the code you pointed to. Whatever’s easiest for you!
Ping @satya164 for input on how to resolve.
sreucherand commentedon Nov 11, 2016
Well, when I try
String(window.postMessage) === String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage')on the page I visit, it returnstrue, so I am guessingpostMessagehas not been overridden.jacobp100 commentedon Nov 11, 2016
Oh I see. The JS is injected after postMessage is overwritten. Your best bet is to fork it then, and I’ll see what I can do about getting this fixed! Sorry. :(
persyl commentedon Nov 20, 2016
I have same error, without injecting any javascript to WebView - just setting WebView-properties:
source={require('../lib/html/somefile.html')}
onMessage={() => console.log('Hello World')}
and in my ../lib/html/somefile.html' I have javascript code doing both window.postMessage() and document.addEventListener("message",.......
Everything worked fine when I had the HTML in a variable "someHtml", like:
source={{html: someHtml}}
So difference seem to be when changing source to HTML-file instead of HTML-string.
Still for me I had other reasons, not related to this, why I really need to read HTML from file instead of string so it would be great to get this issue fixed.
cbcye commentedon Nov 20, 2016
+1, still has this problem
jacobp100 commentedon Nov 20, 2016
There is an ongoing discussion in #10941 for the best way to go forward with this.
It won't be in the next release or probably the release after. If you need this fixed now, fork the project and use the fix in the PR.
lealife commentedon Dec 19, 2016
+1, I'm getting the same error. When there is a
iframeon web, it occurs the same errorvarhp commentedon Dec 21, 2016
+1,encounter this problem
jacobp100 commentedon Dec 21, 2016
Again, there is a pr for this. We're aware this problem is affecting a lot of people.
edencakir commentedon Dec 26, 2016
+1 getting the same error.
kyle-ilantzis commentedon Dec 31, 2016
injectedJavascript should instead be the following
And then it works! 😄
And if any one is interested, in my code instead of writing a giant string I did the following
mschipperheyn commentedon Jan 21, 2017
@kyle-ilantzis tried this with RN 0.38.1, but still get the Red Screen. What version of RN are you on?
82 remaining items
AdamZaczek commentedon Jan 23, 2019
You man are my hero! This is so simple and eloquent. It works for Expo 31 even when using iframes
olegongit commentedon Feb 6, 2019
For me it helped to use 'useWebKit' prop on a WebView.
iOS specific workaround - react/react-native#10865 (comment)
princeTPG commentedon Feb 12, 2019
This issue will not occur is you use "useWebKit" prop of webview, that will use WK webview instead of UI webview in case of IOS.
But in case if you don't want to use WK webview, then below is the sample JS code that you can inject in you Webview with "injectedJavaScript" prop.
The above code did nothing just patched window.postMessage function, the above issue will not appear.
Angelk90 commentedon Apr 1, 2019
@AdamZaczek , @princeTPG :
I can't get it to work properly, you can give an example that can work on https://snack.expo.io?
I did something like that, but it seems that it doesn't work anymore.
Code:
onMessage seems to receive nothing more, even if it worked before.
TfADrama commentedon May 22, 2019
The patched script makes my onMessage to be always called twice in iOS when i do a click event...
The useWebKit prop was the only way, at least for now...
kenobi91 commentedon Jun 27, 2019
If you still have this issue you can use code from above, to prevent this error like this:
`const patchPostMessageFunction = function() {
var originalPostMessage = window.postMessage;
var patchedPostMessage = function(message, targetOrigin, transfer) {
originalPostMessage(message, targetOrigin, transfer);
};
patchedPostMessage.toString = function() {
return String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage');
};
window.postMessage = patchedPostMessage;
};`
Then you can append your own JS injection code like this:
<WebView injectedJavaScript={'(' + String(patchPostMessageFunction) + ')();' + injectedJavaScript}/>This will prevent error from above but also this will prevent in some cases to use window.postMessage from your injected JS code. To go around this just wrap window.postMessage(msg) in timeout like this:
setTimeout(() => { window.postMessage(JSON.stringify(postMessage)); }, 100);Rafaell416 commentedon Jul 29, 2019
Awesome man, if you come to Colombia I'll invite u some Empanadas.
kckunal2612 commentedon Sep 30, 2019
Is there an updated version of this answer which will work with the new version of react-native-webview ? I am unable to receive messages in onMessage() on both iOS and Android. :(
kckunal2612 commentedon Sep 30, 2019
Answering my own question here for everyone else's reference -
(See - https://github.com/react-native-community/react-native-webview/releases/tag/v5.0.0)
Make sure you call windowPostMessageFix function first and then your second function -
I hope it helps ! Thanks !
DevArenaCN commentedon Oct 7, 2019
This did solve the issue with the red screen, but when I tried to post data back from my frontend code calling
window.postMessage('some stuff'. '*')I got errorTypeError: undefined is not an object (evaluating 'window.ReactNativeWebView.postMessage')Any ideas?kckunal2612 commentedon Oct 8, 2019
I tested my app on Lollipop and it crashes due to a WebView issue listed here -
react-native-webview/react-native-webview#858
As a result, our company is now moving away from React Native for Android. Sadly, there are too many breaking changes and too many security vulnerabilities in third party libraries.