React native - 以编程方式检查是否启用了远程JS调试

时间:2016-08-18 15:33:13

标签: react-native

在React-Native上,我怎么知道是否启用了"Debug JS Remotely"

我尝试查看RN docs和各种NPM套餐,但无法了解......

7 个答案:

答案 0 :(得分:12)

如何以编程方式检查是否启用了远程调试(今天在另一个SO问题上发现了这种奇特的行为)。在RN 0.43和Chrome调试器+ React Native Debugger上进行了测试:

const isDebuggingEnabled = (typeof atob !== 'undefined');

编辑:刚刚注意到这是半年前的问题:D ......好吧,我把它留给后代。

答案 1 :(得分:4)

熟悉这个答案,但对于检查atob或被限制为android感到不满意。我发现一个函数似乎是一个非常好的代理,如果调试器正在运行,这是一个名为__REMOTEDEV__的全局。

在我的情况下,我想看看应用程序在react-native-debugger中发出的请求,我的完整代码如下:

/**
 * When the debugger is connected, remove the XHR polyfill
 * so that the chrome inspector will be able to see requests
 */
if (typeof global.__REMOTEDEV__ !== 'undefined') {
  const _XHR = GLOBAL.originalXMLHttpRequest ?
      GLOBAL.originalXMLHttpRequest :
      GLOBAL.XMLHttpRequest;

  global.XMLHttpRequest = _XHR;
}

答案 2 :(得分:4)

如果启用了远程调试,则存在类DedicatedWorkerGlobalScope(在这种情况下,它是全局对象的构造函数)。因此我们可以:

const haveRemoteDev = (typeof DedicatedWorkerGlobalScope) !== 'undefined';

答案 3 :(得分:3)

通过一种简单的方法检查__REMOTEDEV__

if(global.__REMOTEDEV__) { console.log('Remote Debug'); }

答案 4 :(得分:1)

您可以检查global.nativeCallSyncHook

if (!global.nativeCallSyncHook) {
  console.log("Debug JS Remotely")
}

更多信息,请访问:https://github.com/facebook/react-native/commit/417e191a1cfd6a049d1d4b0a511f87aa7f176082

答案 5 :(得分:0)

对于Android,在共享首选项中,您可以找到远程调试状态。 当我打开我的应用程序的sharedPreferences文件时。

远程调试活动

<map>
    <boolean name="remote_js_debug" value="true" />
    <boolean name="hot_module_replacement" value="true" />
    <boolean name="reload_on_js_change" value="true" />
</map>

远程调试无效

<map>
    <boolean name="remote_js_debug" value="false" />
    <boolean name="hot_module_replacement" value="true" />
    <boolean name="reload_on_js_change" value="true" />
</map>

所以(仅限Android),您可以使用类似这样的模块:https://github.com/sriraman/react-native-shared-preferences来检查远程调试是否处于活动状态。

答案 6 :(得分:0)

因此,以下任何答案对我都无效...

我发现全局对象上的位置路径名更改为/debugger-ui

if (global.location && global.location.pathname.includes('/debugger-ui')) {
  Alert.alert('Remote debug is on');
} else {
  Alert.alert('Remote debug is off');
}

正在研究RN 0.59。