Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HTML content of webpage with webview

Tags:

react-native

I want to get html code with webview in react native my current code only can call alert function after its injected. i want to get a specific div html content.

  const jsCode = "window.postMessage(alert(document.getElementById('result')))"

<WebView 
            style={{width:Dimensions.get('window').width,height:Dimensions.get('window').height}}
            javaScriptEnabled ={true}
             injectedJavaScript={jsCode} 
            source={{uri: this.state.uri}}
      />
like image 314
Berkin Tatlisu Avatar asked Dec 13 '25 10:12

Berkin Tatlisu


1 Answers

window.postMessage causes the WebView.onMessage-handler to get called with the data.

So this should work:

const jsCode = "window.postMessage(document.getElementById('gb-main').innerHTML)"

<WebView
  javaScriptEnabled={true}
  injectedJavaScript={jsCode}
  source={{uri: 'http://www.google.com/'}}
  onMessage={event => console.log('Received: ', event.nativeEvent.data)}
/>
like image 148
mtkopone Avatar answered Dec 16 '25 02:12

mtkopone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!