View difference between Paste ID: Epy9xSSn and tv1n76wH
SHOW: | | - or go back to the newest paste.
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( "my message" );
12+
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-
        this.WebView.postMessage(JSON.stringify(action));
20+
21
    postMessage = (action) => {
22
        // throw new Error  // Works fine too
23
        this.webView.postMessage("my other message", "*");
24-
      var dataStr = JSON.parse(evnt.nativeEvent.data);
24+
25-
      this.setState({ debug: dataStr })
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-
        var style = {...styles.vid, backgroundColor: color, flex: 1, display: 'flex'}
31+
32
    render () {
33-
        var color   = this.props.color || 'red',
33+
        // throw new Error  // works just fine
34-
            debugContent = null;
34+
35
36
        var style           = {...styles.vid, backgroundColor: color, flex: 1, display: 'flex'},
37-
          debugContent = <View style={{position: 'absolute', top: 0, left: 0}}><Text>this.state.debug</Text></View>;
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-
                    ref={thsiRef => {this.WebView = thsiRef}}
44+
45
        return (
46-
                    // source={{uri: 'https://www.youtube.com/embed/sd1KFa0pPvM?playsinline=1&autoplay=1&enablejsapi=1'}}
46+
47-
                    // source={{uri: 'https://www.youtube.com/watch?v=sd1KFa0pPvM?playsinline=0&enablejsapi=1&autoplay=0'}}
47+
48-
                    // source={{uri: 'https://www.youtube.com/watch?v=sd1KFa0pPvM?playsinline=0&autoplay=0'}}
48+
                    ref={thsiRef => {this.webView = thsiRef}}
49-
                    source={{uri: 'https://codeforboston.github.io/cliff-effects/#/'}}
49+
50-
                    // source={{uri: 'https://www.youtube.com/watch?v=sd1KFa0pPvM'}}
50+
                    source={{uri: 'https://myURL.com'}}
51-
                    // source={{html: '<html><body><iframe width="1347" height="503" src="https://www.youtube.com/embed/sd1KFa0pPvM" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></body></html>'}}
51+
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
};