1 /* eslint-env browser */
4 * This is the web browser implementation of `debug()`.
7 exports.formatArgs = formatArgs;
10 exports.useColors = useColors;
11 exports.storage = localstorage();
12 exports.destroy = (() => {
18 console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
107 * Currently only WebKit-based Web Inspectors, Firefox >= v31,
108 * and the Firebug extension (any Firefox version) are known
109 * to support "%c" CSS customizations.
111 * TODO: add a `localStorage` variable to explicitly enable/disable colors
114 // eslint-disable-next-line complexity
115 function useColors() {
116 // NB: In an Electron preload script, document will be defined but not fully
117 // initialized. Since we know we're in Chrome, we'll just detect this case
119 if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
123 // Internet Explorer and Edge do not support colors.
124 if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
128 // Is webkit? http://stackoverflow.com/a/16459606/376773
129 // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
130 return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
131 // Is firebug? http://stackoverflow.com/a/398120/376773
132 (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
133 // Is firefox >= v31?
134 // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
135 (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
136 // Double check webkit in userAgent just in case we are in a worker
137 (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
141 * Colorize log arguments if enabled.
146 function formatArgs(args) {
147 args[0] = (this.useColors ? '%c' : '') +
149 (this.useColors ? ' %c' : ' ') +
151 (this.useColors ? '%c ' : ' ') +
152 '+' + module.exports.humanize(this.diff);
154 if (!this.useColors) {
158 const c = 'color: ' + this.color;
159 args.splice(1, 0, c, 'color: inherit');
161 // The final "%c" is somewhat tricky, because there could be other
162 // arguments passed either before or after the %c, so we need to
163 // figure out the correct index to insert the CSS into
166 args[0].replace(/%[a-zA-Z%]/g, match => {
167 if (match === '%%') {
171 if (match === '%c') {
172 // We only are interested in the *last* %c
173 // (the user may have provided their own)
178 args.splice(lastC, 0, c);
182 * Invokes `console.debug()` when available.
183 * No-op when `console.debug` is not a "function".
184 * If `console.debug` is not available, falls back
189 exports.log = console.debug || console.log || (() => {});
194 * @param {String} namespaces
197 function save(namespaces) {
200 exports.storage.setItem('debug', namespaces);
202 exports.storage.removeItem('debug');
206 // XXX (@Qix-) should we be logging these?
213 * @return {String} returns the previously persisted debug modes
219 r = exports.storage.getItem('debug');
222 // XXX (@Qix-) should we be logging these?
225 // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
226 if (!r && typeof process !== 'undefined' && 'env' in process) {
227 r = process.env.DEBUG;
234 * Localstorage attempts to return the localstorage.
236 * This is necessary because safari throws
237 * when a user disables cookies/localstorage
238 * and you attempt to access it.
240 * @return {LocalStorage}
244 function localstorage() {
246 // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
247 // The Browser also has localStorage in the global context.
251 // XXX (@Qix-) should we be logging these?
255 module.exports = require('./common')(exports);
257 const {formatters} = module.exports;
260 * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
263 formatters.j = function (v) {
265 return JSON.stringify(v);
267 return '[UnexpectedJSONParseError]: ' + error.message;