React native - Programmatically check if in remote JS debugging is enabled

How to programmatically check if remote debugging is enabled (just found this peculiar behaviour today on another SO question). Tested on RN 0.43 and with Chrome debugger + React Native Debugger :

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

Edit: just noticed this was asked over half a year ago :D... well, I leave it here for future generations.


Check __REMOTEDEV__ in a simple way:

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

A class DedicatedWorkerGlobalScope exists iff remote debugging is enabled (it is the constructor of global object in that case). Thus we can:

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

Ran across this answer, but wasn't happy with checking for atob or being constrained to android. I found a function that appears to be a pretty good proxy for if the debugger is running, which is a global called __REMOTEDEV__.

In my case, I wanted to see requests made by the app in react-native-debugger, my full code is as follows:

/**
 * 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;
}

Tags:

React Native