ptmt / react-native-macos

[deprecated in favor of https://microsoft.github.io/react-native-windows/] React Native for macOS is an experimental fork for writing desktop apps using Cocoa
MIT License
11.25k stars 429 forks source link

WebView's injectedJavaScript prop doesn't produce any effect #196

Closed shirakaba closed 6 years ago

shirakaba commented 6 years ago

Description

Related to #195 . The injectedJavaScript prop should allow one to inject JS into a webpage (on iOS, one gets the choice of injecting before or after page load, but React Native doesn't seem to give that amount of granularity). I'd expect to see the page transition from saying "Test before" to "Test after". However, it doesn't produce any change in the web-page at all.

I have tried this both with a static HTML page and dynamic pages loaded over HTTP and HTTPS. I have furthermore tried both with and without NSAllowsArbitraryLoadsInWebContent set to YES.

Reproduction Steps and Sample Code

The UIExplorer WebView examples can be used to reproduce this, or the following minimal example can be copied:

'use strict';

var React = require('React');
var ReactNative = require('react-native');
var {
  StyleSheet,
  Button,
  View,
  WebView,
} = ReactNative;

class InjectJS extends React.Component {
  webview = null;
  render() {
    return (
      <View>
        <WebView
          ref={webview => {
            this.webview = webview;
          }}
          injectedJavaScript={"document.write(\"<h1>Test after</h1>\");"}
          style={{
            backgroundColor: 'rgba(255,255,255,0.8)',
            height: 300,
          }}
          source={{ html: "<h1>Test before</h1>" }}
          scalesPageToFit={true}
        />
      </View>
    );
  }
}

var styles = StyleSheet.create({
  buttons: {
    flexDirection: 'row',
    height: 30,
    backgroundColor: 'black',
    alignItems: 'center',
    justifyContent: 'space-between',
  }
})

Additional Information

Solution

Will need to revisit this, but I'm beginning to suspect that the native code is outdated. I'm seeing a few problems:

RCTWebView.m passes on the injectedJavaScript prop to a call tostringByEvaluatingJavaScriptFromString() during the decidePolicyForNavigationAction() block.

Is this on the roadmap for the next update, or is help needed in this area?

shirakaba commented 6 years ago

Looks like my build/cache must have been screwing up, because I found that the injected JS did begin to work today.

In any case, having been alarmed by the fact that the RN core is still using the hideously outdated UIWebView, I decided to make a macOS fork of the community-favourite CRAlpha/react-native-wkwebview. Until RN do decide to merge that into core, it'll remain as a third-party plugin. But thanks to this fork, making a macOS port will be trivial to get up-to-date if their repo ever does get merged.

My port does not yet expose an API for WKUserContentController.addUserScript(), but it's on my hitlist.

ptmt commented 6 years ago

Sorry, for my silence! I have a little vacation. I’ll answer as soon as I get enough time to process this. Anyway, WKWebView totally makes sense. On Mon, 14 May 2018 at 01:56, Jamie Birch notifications@github.com wrote:

Closed #196 https://github.com/ptmt/react-native-macos/issues/196.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ptmt/react-native-macos/issues/196#event-1623733847, or mute the thread https://github.com/notifications/unsubscribe-auth/AA9SUwXAGHdfMiOXQHSiv6qm_NinrMRIks5tyLo1gaJpZM4T5Kqn .