在页面呈现之前将JavaScript或CSS注入React Native WebView

在页面呈现之前将JavaScript或CSS注入React Native WebView,webview,react-native,Webview,React Native,我想隐藏嵌入到React本机WebView中的特定页面的标题。目前,我使用类似这样的方法来动态删除标题: <WebView ... some other props ... injectedJavaScript={'function hideHead(){document.getElementById("head").style.display="none";};hideHead();'} /> 有时您仍然可以看到标题闪烁,因此我猜在WebView中加载页面后,会

我想隐藏嵌入到React本机WebView中的特定页面的标题。目前,我使用类似这样的方法来动态删除标题:

<WebView 
  ... some other props ...
  injectedJavaScript={'function hideHead(){document.getElementById("head").style.display="none";};hideHead();'}
  />

有时您仍然可以看到标题闪烁,因此我猜在WebView中加载页面后,会对该JavaScript进行评估


是否有可能在页面呈现之前添加/注入JavaScript或CSS以移除闪烁?

我找不到在页面加载之前注入JavaScript或CSS的方法


为了解决闪烁问题,我将另一个
视图
放在
WebView
的顶部,而它仍处于
加载
状态。可以使用
onNavigationStateChanged
回调查找页面是否已加载。我放在上面的
视图只显示了一个
活动指示符号
我面对这个问题,我的解决方案就是这样

render() {
    const remove_header = 'header = document.querySelector("header"); header.parentNode.removeChild(header);';
    uri = this.props.url
    return (
      <View style={{flex:1 }}>
        {(!this.state.showWebView) &&  <ActivityIndicator size="large" /> }
        <WebView
          style={this.state.showWebView ?  {flex:1} : {flex: 0} }
          source={{uri: uri}}
          javaScriptEnabled
          injectedJavaScript={'function injectRules() {'remove_header'};injectRules();'}
          onNavigationStateChange={(event) => {
            if (event.url !== uri) {
              this.webview.stopLoading()
              Linking.openURL(event.url)
            }
          }}
          onLoadStart={() => {
            this.setState({ showWebView: false })
          }}

          onLoadEnd={() => {
            if (!this.state.showWebView ) {
              this.setState({ showWebView: true })
            }
          }}
        />
      </View>
    )
  }
render(){
const remove_header='header=document.querySelector(“header”);header.parentNode.removeChild(header);';
uri=this.props.url
返回(
{(!this.state.showWebView)&&}
{
如果(event.url!==uri){
this.webview.stopLoading()
Linking.openURL(event.url)
}
}}
onLoadStart={()=>{
this.setState({showWebView:false})
}}
onLoadEnd={()=>{
如果(!this.state.showWebView){
this.setState({showWebView:true})
}
}}
/>
)
}

我的快速解决方案是在WebView中添加一个负marginTop以隐藏标题。通过执行此操作,标头将在渲染之前隐藏

 webView: {
    ...
    marginTop: -80,
  },

也许您可以在隐藏的react原生视图中呈现webview,然后显示webview。我希望用户能够跟踪webview中的链接。如果它总是换成一个新的WebView,那就不太好了,不是吗?现在它在WebView中加载的每个页面中都注入了JS,但有时仍然会看到闪烁的标题。如果你颠倒逻辑,首先隐藏标题,你会得到更好的结果吗,然后在不在网络视图中显示它?@ChrisGeirman我不想更改网页本身,因为人们可以在线看到它。这也会在网页上引起另一个问题:如何在用户没有注意到的情况下将标题放回去。是的,它是-您可以使用injectJavascript属性。你能提供一个你如何实现它的示例代码吗?谢谢