Skip to content
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

Setting onMessage on a WebView overrides existing values of window.postMessage #2

Open
PaddyLock opened this issue Jan 4, 2018 · 9 comments

Comments

@PaddyLock
Copy link

PaddyLock commented Jan 4, 2018

When I load the webview I get this error message

Setting onMessage on a WebView overrides existing values of window.postMessage

I'm not setting onMessage anywhere else.

The content of my render is

<View
                style={{
                    paddingVertical: 40,
                    backgroundColor: `#ffffff`,
                    flex: 1
                }}
            >
                <Text>Testing application for react-native-webview-braintree npm package</Text>

                <ActivityIndicator
                    animating={true}
                    style={[styles.centering, {height: 180}]}
                    size="large"
                />

                {renderIf(this.state.clientToken !== '')(
                    <WebViewBraintree
                        clientToken={this.state.clientToken}
                        nonceObtainedCallback={this.handlePaymentMethod}
                        navigationBackCallback={this.navigationBackCallback}
                        paymentAPIResponse={this.state.paymentAPIResponse}
                    />
                )}

            </View>
@reggie3
Copy link
Owner

reggie3 commented Jan 5, 2018

Can you take a snapshot of your device's screen and post a picture of the error you are getting.

@PaddyLock
Copy link
Author

Hi, thanks for taking a look at this.

First I see this briefly (the console warning is just my client token)

screen shot 2018-01-05 at 09 35 56

Then I get the error

screen shot 2018-01-05 at 14 21 44

I also get this error until I have removed react-native from react-native-webview-braintree/node_modules

screen shot 2018-01-05 at 14 20 34

@reggie3
Copy link
Owner

reggie3 commented Jan 5, 2018

This problem seems to be related to this react-native issue here: facebook/react-native#10865

It's platform specific to iOS. It looks like it is a problem associated with the react-native-webview-messaging npm package. There is an issue associated with it here: lesnitsky/react-native-webview-messaging#16. I've contributed to that package before, and the writer is a good guy. I'm going to move the conversation over there.

@PaddyLock
Copy link
Author

I have loaded up the same app in the Genymotion android simulator and get a different error for the same view.

screen shot 2018-01-08 at 09 34 45

@hobbesdaboba
Copy link

hobbesdaboba commented Feb 27, 2018

Referencing commit 7790af6

I modified the node_modules/react-native-webview-braintree/node_modules/react-native-webview-messaging/WebView.js render function as follows:

  render() {
    const patchPostMessageFunction = () => {
      var originalPostMessage = window.postMessage;
      var patchedPostMessage = function(message, targetOrigin, transfer) {
        originalPostMessage(message, targetOrigin, transfer);
      };

      patchedPostMessage.toString = () => {
        return String(Object.hasOwnProperty).replace(
          'hasOwnProperty',
          'postMessage'
        );
      };
      window.postMessage = patchedPostMessage;
    };

    const patchPostMessageJsCode =
      '(' + String(patchPostMessageFunction) + ')();';

    return (
      <NativeWebView
        {...this.props}
        injectedJavaScript={patchPostMessageJsCode}
        onMessage={this.onMessage}
        ref={this._refWebView}
      />
    );
  }

No more red screen... not sure how this affects the rest of the package, if at all...

@reggie3
Copy link
Owner

reggie3 commented Mar 3, 2018

I had a couple of other projects that used the same messaging technique. I replaced it in those apps, and it seems to be working okay. I'll have to do the same here.

@PaddyLock
Copy link
Author

Hi @reggie3 I've been away for a while, but just just picking up my project again and ready to try implementing this again. So will you be updating with the solution from @hobbesdaboba Also will you be removing react-native from react-native-webview-braintree/node_modules so there is no ambiguous resolution? Thanks

@reggie3
Copy link
Owner

reggie3 commented Mar 7, 2018

I won't be a able to work on this project for a few days, but I plan on making this project more like my other react-native-webview projects so that it incorporates both the library and an example application. It'll take me a couple of weeks to get around to it though. Doing this will solve this particular issue.

@sumanlamsal
Copy link

override node_modules/react-native-webview-messaging/WebView.js whith the following code

`render() {
    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;
};

const patchPostMessageJsCode = '(' + String(patchPostMessageFunction) + ')();';

    return `(`
      <NativeWebView
        {...this.props}
        scalesPageToFit={false}
        injectedJavaScript={patchPostMessageJsCode}
        onMessage={this.onMessage}
        ref={this._refWebView}
      />
    );
  }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants