Advertisement
Guest User

injectedJavascript

a guest
Apr 9th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // fix https://github.com/facebook/react-native/issues/10865
  2. // MrLoh
  3. const patchPostMessageJsCode = `(${String(function() {
  4.     throw new Error;  // Doesn't get triggered
  5.     var originalPostMessage = window.postMessage
  6.     var patchedPostMessage = function(message, targetOrigin, transfer) {
  7.         originalPostMessage(message, targetOrigin, transfer)
  8.     }
  9.     patchedPostMessage.toString = function() {
  10.         return String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage')
  11.     }
  12.     window.postMessage = patchedPostMessage
  13.     setTimeout(function () {window.postMessage( "my message", "*" );}, 2000);
  14.     throw new Error
  15. })})();`
  16.  
  17.  
  18. class JourneyVid extends Component {
  19.     state = {debug: null}
  20.  
  21.     postMessage = (action) => {
  22.         // throw new Error  // Works fine too
  23.         this.webView.postMessage("my other message", "*");
  24.     }
  25.  
  26.     onMessage = (evnt) => {
  27.         throw new Error;  // Doesn't get triggered
  28.         var dataStr = JSON.parse( evnt.nativeEvent.data );
  29.         this.setState({ debug: dataStr });
  30.     }
  31.  
  32.     render () {
  33.         // throw new Error  // works just fine
  34.         count++
  35.  
  36.         var style           = {...styles.vid, backgroundColor: color, flex: 1, display: 'flex'},
  37.             color           = this.props.color || 'red',
  38.             debugContent    = null;
  39.  
  40.         if (this.state.debug !== null) {
  41.           debugContent = <Text>{ this.state.debug }</Text>;  // Doesn't ever seem to show anything
  42.           style.backgroundColor = 'blue';
  43.         }
  44.  
  45.         return (
  46.             <View style={style}>
  47.                 <WebView style={{flex: 1}}
  48.                     ref={thsiRef => {this.webView = thsiRef}}
  49.                     automaticallyAdjustContentInsets={true}
  50.                     source={{uri: 'https://myURL.com'}}
  51.                     javaScriptEnabled={true}
  52.                     javaScriptEnabledAndroid={true}
  53.                     domStorageEnabled={true}
  54.                     decelerationRate='normal'
  55.                     startInLoadingState={true}
  56.                     allowsInlineMediaPlayback={false}
  57.                     onMessage={this.onMessage}
  58.                     injectedJavascript={patchPostMessageJsCode} />
  59.                 <TouchableOpacity onPress={this.postMessage}>
  60.                     <Text>Send message from react native</Text>
  61.                 </TouchableOpacity>
  62.                 { debugContent }
  63.             </View>
  64.         );
  65.     }
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement