2 * This is the web browser implementation of `debug()`.
4 * Expose `debug()` as the module.
7 exports = module.exports = require('./debug');
9 exports.formatArgs = formatArgs;
12 exports.useColors = useColors;
13 exports.storage = 'undefined' != typeof chrome
14 && 'undefined' != typeof chrome.storage
15 ? chrome.storage.local
32 * Currently only WebKit-based Web Inspectors, Firefox >= v31,
33 * and the Firebug extension (any Firefox version) are known
34 * to support "%c" CSS customizations.
36 * TODO: add a `localStorage` variable to explicitly enable/disable colors
39 function useColors() {
40 // NB: In an Electron preload script, document will be defined but not fully
41 // initialized. Since we know we're in Chrome, we'll just detect this case
43 if (typeof window !== 'undefined' && window.process && window.process.type === 'renderer') {
47 // is webkit? http://stackoverflow.com/a/16459606/376773
48 // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
49 return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
50 // is firebug? http://stackoverflow.com/a/398120/376773
51 (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
53 // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
54 (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
55 // double check webkit in userAgent just in case we are in a worker
56 (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
60 * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
63 exports.formatters.j = function(v) {
65 return JSON.stringify(v);
67 return '[UnexpectedJSONParseError]: ' + err.message;
73 * Colorize log arguments if enabled.
78 function formatArgs(args) {
79 var useColors = this.useColors;
81 args[0] = (useColors ? '%c' : '')
83 + (useColors ? ' %c' : ' ')
85 + (useColors ? '%c ' : ' ')
86 + '+' + exports.humanize(this.diff);
88 if (!useColors) return;
90 var c = 'color: ' + this.color;
91 args.splice(1, 0, c, 'color: inherit')
93 // the final "%c" is somewhat tricky, because there could be other
94 // arguments passed either before or after the %c, so we need to
95 // figure out the correct index to insert the CSS into
98 args[0].replace(/%[a-zA-Z%]/g, function(match) {
99 if ('%%' === match) return;
101 if ('%c' === match) {
102 // we only are interested in the *last* %c
103 // (the user may have provided their own)
108 args.splice(lastC, 0, c);
112 * Invokes `console.log()` when available.
113 * No-op when `console.log` is not a "function".
119 // this hackery is required for IE8/9, where
120 // the `console.log` function doesn't have 'apply'
121 return 'object' === typeof console
123 && Function.prototype.apply.call(console.log, console, arguments);
129 * @param {String} namespaces
133 function save(namespaces) {
135 if (null == namespaces) {
136 exports.storage.removeItem('debug');
138 exports.storage.debug = namespaces;
146 * @return {String} returns the previously persisted debug modes
153 r = exports.storage.debug;
156 // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
157 if (!r && typeof process !== 'undefined' && 'env' in process) {
158 r = process.env.DEBUG;
165 * Enable namespaces listed in `localStorage.debug` initially.
168 exports.enable(load());
171 * Localstorage attempts to return the localstorage.
173 * This is necessary because safari throws
174 * when a user disables cookies/localstorage
175 * and you attempt to access it.
177 * @return {LocalStorage}
181 function localstorage() {
183 return window.localStorage;