-
Notifications
You must be signed in to change notification settings - Fork 270
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
window.postMessage is not ready right away because of WKUserScriptInjectionTime atDocumentEnd #166
Comments
It works, but not right away. |
Indeed, it is required to specify the target origin: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage In this library, window.webkit.messageHandlers.reactNative.postMessage(data); ... Indeed, this alias method shouldn't need a target origin; looks like you're using The alias is only set upon the As a quick guess, you could try running the same aliasing code in an earlier part of the webpage loading lifecycle – in the new window.originalPostMessage = window.postMessage;
window.postMessage = function() {
return window.webkit.messageHandlers.reactNative.postMessage.apply(window.webkit.messageHandlers.reactNative, arguments);
}; Disclaimer: there may well be a valid reason for this aliasing happening only after the page has been loaded; I haven't read the code in detail. You could consider submitting a PR that moves the aliasing code to that earlier part of the webpage loading lifecycle if you could convincingly determine that there are no issues in doing so. ... Otherwise, just use the non-aliased |
Looks like ref: https://developer.apple.com/documentation/webkit/wkuserscript |
That's the webpage loading lifecycle event that I was referring to, yes. The |
Looks like currently the |
Yes. It seems that if I'll be the first to say that I don't see why anyone would ever want to enable such behaviour (I dislike non-explicit behaviours), but I guess it's just a reproduction of what the |
Yes. This is added to be "compatible" with . I don't like it either. But it helps people migrating from WebView. |
@insraq Do you support moving this declaration... : window.originalPostMessage = window.postMessage;
window.postMessage = function() {
return window.webkit.messageHandlers.reactNative.postMessage.apply(window.webkit.messageHandlers.reactNative, arguments);
}; ... to the |
I think it's fine. Last time someone gave it a try, it did not work out as planned (either this did not work or it broke some other functions). But do feel free to submit a PR and give it a go. I have added you as a contributor so you should be able to merge it. Let me know when you think it's ready for a npm release |
@insraq Thank you for adding me as a collaborator! @henrylearn2rock Are you confident to submit a PR for this, or would you prefer to leave it with me? Looks like it might be as simple as changing the lines in - (void)setupPostMessageScript {
if (_messagingEnabled) {
NSString *source=@"window.originalPostMessage = window.postMessage; window.postMessage = function (data) { window.webkit.messageHandlers.reactNative.postMessage(data); }";
WKUserScript *script = [[WKUserScript alloc] initWithSource:source
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:_injectedJavaScriptForMainFrameOnly];
[_webView.configuration.userContentController addUserScript:script];
}
} ... replacing Don't think @henrylearn2rock First, could you try making that one-line change I've mentioned above in your own install of this project (no need to fork the repo, for this initial experiment: just go into |
Good news, it works as expected. After changing that line to Then I tried @shirakaba I haven't cloned this repo, so pls go ahead! 👍 Thanks! |
I've started a PR for this at #167 . Note that I haven't tested anything on my end yet (not sure when I'll be free to do so yet). @henrylearn2rock What props would I need to enter in order to test this? I haven't used this feature before. I see a reference to |
|
Sorry for the delay. I lost a whole day testing You know, I wonder now whether the reason that the Anyway, regardless of the reasoning, I feel that we should maintain consistency with React Native core (wherein the aliasing is effectively configured to If you want it to be effectively available window.originalPostMessage = window.postMessage;
window.postMessage = function(data) {
window.webkit.messageHandlers.reactNative.postMessage(data);
}; |
I think the reason why WebView injects this at the end of documents is that there're no other options. I don't see any problems moving this to the document start. Regarding #170, I think we should de-couple the method override from onMessage callback - make it more explicit. I propose:
|
@insraq That all sounds reasonable if we're happy to diverge a little bit from the (to be frank, problematic) implementation of the native
If I may draw your attention to PR #176, however, which would allow us to forego the addition of an Through this PR, we could furthermore move the code to |
Version: 1.21
(iOS: 11.4)
window.webkit.messageHandlers.reactNative.postMessage
works as documented, butwindow.postMessage({message: 'hello!'})
throws Not enough argumentsWhen I try
window.postMessage({message: 'hello!'}, '*')
, the message is not being sent to RN.Thanks!
The text was updated successfully, but these errors were encountered: