From 24fa80d92ac639f21c6b2fc24d4f1f6463308e53 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Thu, 9 Jan 2020 04:29:48 +0100 Subject: [PATCH] open links displayed in the embedded webview on the device browser --- src/page/file/view.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/page/file/view.js b/src/page/file/view.js index 82f52a1..d1887a7 100644 --- a/src/page/file/view.js +++ b/src/page/file/view.js @@ -64,6 +64,10 @@ class FilePage extends React.PureComponent { converter = null; + linkHandlerScript = `(function () { + window.onclick = function(evt) { evt.preventDefault(); window.ReactNativeWebView.postMessage(evt.target.href); evt.stopPropagation(); } + }());`; + constructor(props) { super(props); this.state = { @@ -109,7 +113,6 @@ class FilePage extends React.PureComponent { onComponentFocused = () => { StatusBar.setHidden(false); - console.log('fileComponent Focused...'); NativeModules.Firebase.setCurrentScreen('File').then(result => { DeviceEventEmitter.addListener('onStoragePermissionGranted', this.handleStoragePermissionGranted); DeviceEventEmitter.addListener('onStoragePermissionRefused', this.handleStoragePermissionRefused); @@ -800,6 +803,13 @@ class FilePage extends React.PureComponent { } }; + handleWebViewMessage = evt => { + const href = evt.nativeEvent.data; + if (href && href.startsWith('http')) { + Linking.openURL(href); + } + }; + buildWebViewSource = () => { const { contentType, fileInfo } = this.props; const localFileUri = this.localUriForFileInfo(fileInfo); @@ -996,6 +1006,8 @@ class FilePage extends React.PureComponent { source={this.buildWebViewSource()} style={filePageStyle.viewer} onLoad={this.handleWebViewLoad} + injectedJavaScript={this.linkHandlerScript} + onMessage={this.handleWebViewMessage} /> )} {this.state.showImageViewer && (