React Native WebView onMessage and postMessage to get all web page












2















I don't really clear how to implement onMessage and postMessage, can I get whole web page but only from react native side.



I mean, I will inject this code using injectedJavaScript



var markup = document.documentElement.innerHTML
window.postMessage(markup)


and I will receive the result using onMessage. Is it posible cause so far I can't do that










share|improve this question



























    2















    I don't really clear how to implement onMessage and postMessage, can I get whole web page but only from react native side.



    I mean, I will inject this code using injectedJavaScript



    var markup = document.documentElement.innerHTML
    window.postMessage(markup)


    and I will receive the result using onMessage. Is it posible cause so far I can't do that










    share|improve this question

























      2












      2








      2








      I don't really clear how to implement onMessage and postMessage, can I get whole web page but only from react native side.



      I mean, I will inject this code using injectedJavaScript



      var markup = document.documentElement.innerHTML
      window.postMessage(markup)


      and I will receive the result using onMessage. Is it posible cause so far I can't do that










      share|improve this question














      I don't really clear how to implement onMessage and postMessage, can I get whole web page but only from react native side.



      I mean, I will inject this code using injectedJavaScript



      var markup = document.documentElement.innerHTML
      window.postMessage(markup)


      and I will receive the result using onMessage. Is it posible cause so far I can't do that







      react-native webview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 31 '17 at 17:00









      2r832r83

      236316




      236316
























          2 Answers
          2






          active

          oldest

          votes


















          2














          yes you can do this all you to have to do is use window.postMessage("message") from your web-page that is going to load in WebView and you can see that message in onMessage prop.



          Example:



          class Test extends React.Component{
          constructor(props){
          super(props);
          this.state:{
          html:''
          }
          }
          componentWillMount(){
          this.setState({
          html : `<html>
          <head>
          <script>
          window.postMessage("Messga from webView")
          </script>
          </head>
          <body><h1>Hello from webView</h1></body>
          </html>`
          })
          }

          render(){
          return (
          <View style={{flex: 1}}>
          <WebView
          ref={(reff) => {
          this.webView = reff;
          }}
          source={{html: this.state.html}}
          style={[styles.flex1, styles.padding5]}
          onMessage={(event)=>{
          let message = event.nativeEvent.data;
          /* event.nativeEvent.data must be string, i.e. window.postMessage
          should send only string.
          * */
          }}
          onNavigationStateChange={(evt)=>{}}
          onError={(e) => {
          console.warn('error occured', e)
          }}/>
          </View>
          )
          }
          }


          I just added a sample html and rendered it in WebView, you can do the same in your page that you are going to load in WebView.



          Or another solution is:



          You can use injectedJavaScript or injectJavaScript props of WebView as described here.






          share|improve this answer

































            0














            Actually
            i was looking at the documentation since i use the injectJavascript function of the react native´s webview. And in here https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage , it says that for extensions we need to use a "*" as a property .
            So it wouldn´t be like this "window.postMessage("Messga from webView")"



            Instead it will need to be window.postMessage("Messga from webView","*") to work.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47040733%2freact-native-webview-onmessage-and-postmessage-to-get-all-web-page%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              yes you can do this all you to have to do is use window.postMessage("message") from your web-page that is going to load in WebView and you can see that message in onMessage prop.



              Example:



              class Test extends React.Component{
              constructor(props){
              super(props);
              this.state:{
              html:''
              }
              }
              componentWillMount(){
              this.setState({
              html : `<html>
              <head>
              <script>
              window.postMessage("Messga from webView")
              </script>
              </head>
              <body><h1>Hello from webView</h1></body>
              </html>`
              })
              }

              render(){
              return (
              <View style={{flex: 1}}>
              <WebView
              ref={(reff) => {
              this.webView = reff;
              }}
              source={{html: this.state.html}}
              style={[styles.flex1, styles.padding5]}
              onMessage={(event)=>{
              let message = event.nativeEvent.data;
              /* event.nativeEvent.data must be string, i.e. window.postMessage
              should send only string.
              * */
              }}
              onNavigationStateChange={(evt)=>{}}
              onError={(e) => {
              console.warn('error occured', e)
              }}/>
              </View>
              )
              }
              }


              I just added a sample html and rendered it in WebView, you can do the same in your page that you are going to load in WebView.



              Or another solution is:



              You can use injectedJavaScript or injectJavaScript props of WebView as described here.






              share|improve this answer






























                2














                yes you can do this all you to have to do is use window.postMessage("message") from your web-page that is going to load in WebView and you can see that message in onMessage prop.



                Example:



                class Test extends React.Component{
                constructor(props){
                super(props);
                this.state:{
                html:''
                }
                }
                componentWillMount(){
                this.setState({
                html : `<html>
                <head>
                <script>
                window.postMessage("Messga from webView")
                </script>
                </head>
                <body><h1>Hello from webView</h1></body>
                </html>`
                })
                }

                render(){
                return (
                <View style={{flex: 1}}>
                <WebView
                ref={(reff) => {
                this.webView = reff;
                }}
                source={{html: this.state.html}}
                style={[styles.flex1, styles.padding5]}
                onMessage={(event)=>{
                let message = event.nativeEvent.data;
                /* event.nativeEvent.data must be string, i.e. window.postMessage
                should send only string.
                * */
                }}
                onNavigationStateChange={(evt)=>{}}
                onError={(e) => {
                console.warn('error occured', e)
                }}/>
                </View>
                )
                }
                }


                I just added a sample html and rendered it in WebView, you can do the same in your page that you are going to load in WebView.



                Or another solution is:



                You can use injectedJavaScript or injectJavaScript props of WebView as described here.






                share|improve this answer




























                  2












                  2








                  2







                  yes you can do this all you to have to do is use window.postMessage("message") from your web-page that is going to load in WebView and you can see that message in onMessage prop.



                  Example:



                  class Test extends React.Component{
                  constructor(props){
                  super(props);
                  this.state:{
                  html:''
                  }
                  }
                  componentWillMount(){
                  this.setState({
                  html : `<html>
                  <head>
                  <script>
                  window.postMessage("Messga from webView")
                  </script>
                  </head>
                  <body><h1>Hello from webView</h1></body>
                  </html>`
                  })
                  }

                  render(){
                  return (
                  <View style={{flex: 1}}>
                  <WebView
                  ref={(reff) => {
                  this.webView = reff;
                  }}
                  source={{html: this.state.html}}
                  style={[styles.flex1, styles.padding5]}
                  onMessage={(event)=>{
                  let message = event.nativeEvent.data;
                  /* event.nativeEvent.data must be string, i.e. window.postMessage
                  should send only string.
                  * */
                  }}
                  onNavigationStateChange={(evt)=>{}}
                  onError={(e) => {
                  console.warn('error occured', e)
                  }}/>
                  </View>
                  )
                  }
                  }


                  I just added a sample html and rendered it in WebView, you can do the same in your page that you are going to load in WebView.



                  Or another solution is:



                  You can use injectedJavaScript or injectJavaScript props of WebView as described here.






                  share|improve this answer















                  yes you can do this all you to have to do is use window.postMessage("message") from your web-page that is going to load in WebView and you can see that message in onMessage prop.



                  Example:



                  class Test extends React.Component{
                  constructor(props){
                  super(props);
                  this.state:{
                  html:''
                  }
                  }
                  componentWillMount(){
                  this.setState({
                  html : `<html>
                  <head>
                  <script>
                  window.postMessage("Messga from webView")
                  </script>
                  </head>
                  <body><h1>Hello from webView</h1></body>
                  </html>`
                  })
                  }

                  render(){
                  return (
                  <View style={{flex: 1}}>
                  <WebView
                  ref={(reff) => {
                  this.webView = reff;
                  }}
                  source={{html: this.state.html}}
                  style={[styles.flex1, styles.padding5]}
                  onMessage={(event)=>{
                  let message = event.nativeEvent.data;
                  /* event.nativeEvent.data must be string, i.e. window.postMessage
                  should send only string.
                  * */
                  }}
                  onNavigationStateChange={(evt)=>{}}
                  onError={(e) => {
                  console.warn('error occured', e)
                  }}/>
                  </View>
                  )
                  }
                  }


                  I just added a sample html and rendered it in WebView, you can do the same in your page that you are going to load in WebView.



                  Or another solution is:



                  You can use injectedJavaScript or injectJavaScript props of WebView as described here.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 '18 at 14:25









                  Sunny Khan

                  18011




                  18011










                  answered Nov 2 '17 at 13:12









                  PrincePrince

                  47218




                  47218

























                      0














                      Actually
                      i was looking at the documentation since i use the injectJavascript function of the react native´s webview. And in here https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage , it says that for extensions we need to use a "*" as a property .
                      So it wouldn´t be like this "window.postMessage("Messga from webView")"



                      Instead it will need to be window.postMessage("Messga from webView","*") to work.






                      share|improve this answer




























                        0














                        Actually
                        i was looking at the documentation since i use the injectJavascript function of the react native´s webview. And in here https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage , it says that for extensions we need to use a "*" as a property .
                        So it wouldn´t be like this "window.postMessage("Messga from webView")"



                        Instead it will need to be window.postMessage("Messga from webView","*") to work.






                        share|improve this answer


























                          0












                          0








                          0







                          Actually
                          i was looking at the documentation since i use the injectJavascript function of the react native´s webview. And in here https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage , it says that for extensions we need to use a "*" as a property .
                          So it wouldn´t be like this "window.postMessage("Messga from webView")"



                          Instead it will need to be window.postMessage("Messga from webView","*") to work.






                          share|improve this answer













                          Actually
                          i was looking at the documentation since i use the injectJavascript function of the react native´s webview. And in here https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage , it says that for extensions we need to use a "*" as a property .
                          So it wouldn´t be like this "window.postMessage("Messga from webView")"



                          Instead it will need to be window.postMessage("Messga from webView","*") to work.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 29 '18 at 21:59









                          ValdaXDValdaXD

                          1,102217




                          1,102217






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47040733%2freact-native-webview-onmessage-and-postmessage-to-get-all-web-page%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              How to pass form data using jquery Ajax to insert data in database?

                              National Museum of Racing and Hall of Fame

                              Guess what letter conforming each word