8000 Check for undefined on browser globals. · debug-js/debug@850749f · GitHub
[go: up one dir, main page]

Skip to content

Commit 850749f

Browse files
author
Marc MacLeod
authored
Check for undefined on browser globals.
Not all environments include these globals. For example, web workers do not have global window objects.
1 parent 6bb07f7 commit 850749f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/browser.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ function useColors() {
4040
// NB: In an Electron preload script, document will be defined but not fully
4141
// initialized. Since we know we're in Chrome, we'll just detect this case
4242
// explicitly
43-
if (window && window.process && window.process.type === 'renderer') {
43+
if (typeof window !== 'undefined' && window && window.process && window.process.type === 'renderer') {
4444
return true;
4545
}
4646

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

5959
/**

0 commit comments

Comments
 (0)
0