-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Implement a postMessage function and an onMessage event for webviews … #9762
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
426b2f8
Implement a postMessage function and an onMessage event for webviews …
dec7c42
Merge messagingEnabled prop with onMessage
jacobp100 2aded21
WIP android branch
jacobp100 efce5f1
WIP web view messaging
62edd77
Implement webview communication on Android
d0f0341
Move native-only props to requireNativeComponent
dfd4db2
Remove incorrect ios annotation from documentation
a508f46
Don't implicitly serialise data passed to onmessage/postMessage: only…
jacobp100 4e4b27c
Call window.postMessage with an event-like object rather than the dat…
jacobp100 762b708
Change onmessage handler to the event-based equivalent
jacobp100 3df06ba
Show warning when overriding non-native value of window.postMessage, …
jacobp100 3c81714
Include logging for Android WebView in BUCK build
jacobp100 ff94739
Fix NSString @-prefix
jacobp100 1d67a78
Merge branch 'master' into ios-webview-communication
satya164 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,6 +217,53 @@ class ScaledWebView extends React.Component { | |
| } | ||
| } | ||
|
|
||
| class MessagingTest extends React.Component { | ||
| webview = null | ||
|
|
||
| state = { | ||
| messagesReceivedFromWebView: 0, | ||
| message: '', | ||
| } | ||
|
|
||
| onMessage = e => this.setState({ | ||
| messagesReceivedFromWebView: this.state.messagesReceivedFromWebView + 1, | ||
| message: e.nativeEvent.data, | ||
| }) | ||
|
|
||
| postMessage = () => { | ||
| if (this.webview) { | ||
| this.webview.postMessage('"Hello" from React Native!'); | ||
| } | ||
| } | ||
|
|
||
| render(): ReactElement<any> { | ||
| const {messagesReceivedFromWebView, message} = this.state; | ||
|
|
||
| return ( | ||
| <View style={[styles.container, { height: 200 }]}> | ||
| <View style={styles.container}> | ||
| <Text>Messages received from web view: {messagesReceivedFromWebView}</Text> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. react/no-string-refs: Using this.refs is deprecated. |
||
| <Text>{message || '(No message)'}</Text> | ||
| <View style={styles.buttons}> | ||
| <Button text="Send Message to Web View" enabled onPress={this.postMessage} /> | ||
| </View> | ||
| </View> | ||
| <View style={styles.container}> | ||
| <WebView | ||
| ref={webview => { this.webview = webview; }} | ||
| style={{ | ||
| backgroundColor: BGWASH, | ||
| height: 100, | ||
| }} | ||
| source={require('./messagingtest.html')} | ||
| onMessage={this.onMessage} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. property |
||
| /> | ||
| </View> | ||
| </View> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| var styles = StyleSheet.create({ | ||
| container: { | ||
| flex: 1, | ||
|
|
@@ -391,5 +438,9 @@ exports.examples = [ | |
| /> | ||
| ); | ||
| } | ||
| }, | ||
| { | ||
| title: 'Mesaging Test', | ||
| render(): ReactElement<any> { return <MessagingTest />; } | ||
| } | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Messaging Test</title> | ||
| <meta http-equiv="content-type" content="text/html; charset=utf-8"> | ||
| <meta name="viewport" content="width=320, user-scalable=no"> | ||
| </head> | ||
| <body> | ||
| <p>Messages recieved from React Native: 0</p> | ||
| <p>(No messages)</p> | ||
| <button type="button"> | ||
| Send message to React Native | ||
| </button> | ||
| </body> | ||
| <script> | ||
| var messagesReceivedFromReactNative = 0; | ||
| document.addEventListener('message', function(e) { | ||
| messagesReceivedFromReactNative += 1; | ||
| document.getElementsByTagName('p')[0].innerHTML = | ||
| 'Messages recieved from React Native: ' + messagesReceivedFromReactNative; | ||
| document.getElementsByTagName('p')[1].innerHTML = e.data; | ||
| }); | ||
|
|
||
| document.getElementsByTagName('button')[0].addEventListener('click', function() { | ||
| window.postMessage('"Hello" from the web view'); | ||
| }); | ||
| </script> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curly: Expected { after 'if' condition.