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

Kuf*_*Kuf 18 react-native

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

我试着查看RN文档和各种NPM软件包,但无法找到如何...

zvo*_*ona 12

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

const isDebuggingEnabled = (typeof atob !== 'undefined');
Run Code Online (Sandbox Code Playgroud)

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


wj2*_*061 8

你可以检查global.nativeCallSyncHook

if (!global.nativeCallSyncHook) {
  console.log("Debug JS Remotely")
}
Run Code Online (Sandbox Code Playgroud)

查看更多信息:https://github.com/facebook/react-native/commit/417e191a1cfd6a049d1d4b0a511f87aa7f176082


Jok*_*ter 7

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

const haveRemoteDev = (typeof DedicatedWorkerGlobalScope) !== 'undefined';
Run Code Online (Sandbox Code Playgroud)


Jes*_*sse 6

遇到了这个答案,但对检查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;
}
Run Code Online (Sandbox Code Playgroud)

  • `global.__REMOTEDEV__` 即使在调试模式下也是 `undefined`。 (6认同)

Ida*_*dan 6

用一个简单的方法来检查__REMOTEDEV__

if(global.__REMOTEDEV__) { console.log('Remote Debug'); }
Run Code Online (Sandbox Code Playgroud)