Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useColors() method causes ReferenceError: 'navigator' is not defined #374

Closed
sstone1 opened this issue Dec 15, 2016 · 3 comments
Closed

Comments

@sstone1
Copy link

sstone1 commented Dec 15, 2016

The following code when used in a JavaScript interpreter for Go (Otto) throws an error:
ReferenceError: 'navigator' is not defined.
This worked fine in previous versions of the module (I've tested 2.3.3).
Could a check be added to see if navigator exists?

function useColors() {
  // NB: In an Electron preload script, document will be defined but not fully
  // initialized. Since we know we're in Chrome, we'll just detect this case
  // explicitly
  if (typeof window !== 'undefined' && 'process' in window && window.process.type === 'renderer') {
    return true;
  }

  // is webkit? http://stackoverflow.com/a/16459606/376773
  // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
  return (typeof document !== 'undefined' && 'WebkitAppearance' in document.documentElement.style) ||
    // is firebug? http://stackoverflow.com/a/398120/376773
    (typeof window !== 'undefined' && window.console && (console.firebug || (console.exception && console.table))) ||
    // is firefox >= v31?
    // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
    (navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
    // double check webkit in userAgent just in case we are in a worker
    (navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
}
@thebigredgeek
Copy link
Contributor

@sstone1 this was fixed yesterday in 2.4.3

@sstone1
Copy link
Author

sstone1 commented Dec 15, 2016

@thebigredgeek the problem is occurring with debug 2.4.4 (I've just retested to confirm).
The problem is occurring because navigator is undefined in this JavaScript interpreter.
I think it needs a typeof navigator !== 'undefined' test before the navigator.userAgent test.

@sstone1
Copy link
Author

sstone1 commented Dec 16, 2016

Looks like @jochenberger has got a fix for this in PR #376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants