我的React原生应用程序中有一个WebView,我需要显示在React原生应用程序的WebView中执行的Javascript日志(log).

我认为我需要利用WebView的nativeConfigprops 来实现这一点,但我不知道如何让它发挥作用.

以下是我的最新try :

 <WebView
    nativeConfig={{
      props: {
        webContentsDebuggingEnabled: true,
        console: new MyLogger()
      }
    }}
   ...

class MyLogger {
  log = message => {
    console.log(message); // Print in RN logs for now...
  };   
} 

任何帮助都将不胜感激.

推荐答案

好吧,我终于明白了.我只需要向WebView注入一些代码,让它使用我自己版本的控制台.

const debugging = `
     // Debug
     console = new Object();
     console.log = function(log) {
       window.webViewBridge.send("console", log);
     };
     console.debug = console.log;
     console.info = console.log;
     console.warn = console.log;
     console.error = console.log;
     `;


<WebView
  injectedJavaScript={debugging}
  onMessage={this.onMessage}

然后在onMessage函数中获取控制台日志(log).

React-native相关问答推荐

替换数组中的一个元素就是替换数组中的所有元素?

为什么我的expoav视频不会自动播放?

React Native:如何以编程方式对图像进行黑白过滤?

在 react native 平面列表中动态定位弹出窗口

react-native react-native-vector-icons:如何使用字体真棒图标

PanResponder 在第二次拖动时将 Animated.View 捕捉回原始位置

可能的未处理promise 拒绝(id:0):错误: "getLoginData" is read-only

如何在react-native中淡化视图的边缘?

redux-saga:如何以编程方式为yields 创建多个calls/side-effects?

react-native android中出现此问题Cannot convert argument of type class org.json.JSONArray的原因是什么?

为 React Native TextInput 创建 ref 时出现 TypeScript 问题

React-Native:Facebook 和 Google 登录

React Native localhost 另一个调试器已经连接

如何判断 goBack() 函数在react-navigation 中是否可行?

我可以使用 react native 开发 pwa

React-native:图像未显示在 android 设备中,但在模拟器中完美显示

在 FlatList 上使用 React Native 搜索过滤器

React-Native Invariant Violation:元素类型无效

React-Native 最低 Android API 级别

如何允许 react-native 启用对 JSX(扩展)文件的支持