Skip to content

Commit 2482e08

Browse files
Marc MacLeodTooTallNate
Marc MacLeod
authored andcommitted
Check for undefined on browser globals (#462)
* Check for undefined on browser globals. Not all environments include these globals. For example, web workers do not have global window objects. * remove redundant global checks
1 parent 6bb07f7 commit 2482e08

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/browser.js

+5-5
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.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.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.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.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.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
5757
}
5858

5959
/**

0 commit comments

Comments
 (0)