3 * This is the web browser implementation of `debug()`.
5 * Expose `debug()` as the module.
8 exports = module.exports = require('./debug');
10 exports.formatArgs = formatArgs;
13 exports.useColors = useColors;
16 * Use chrome.storage.local if we are in an app
21 if (typeof chrome !== 'undefined' && typeof chrome.storage !== 'undefined')
22 storage = chrome.storage.local;
24 storage = localstorage();
40 * Currently only WebKit-based Web Inspectors, Firefox >= v31,
41 * and the Firebug extension (any Firefox version) are known
42 * to support "%c" CSS customizations.
44 * TODO: add a `localStorage` variable to explicitly enable/disable colors
47 function useColors() {
48 // is webkit? http://stackoverflow.com/a/16459606/376773
49 return ('WebkitAppearance' in document.documentElement.style) ||
50 // is firebug? http://stackoverflow.com/a/398120/376773
51 (window.console && (console.firebug || (console.exception && console.table))) ||
53 // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
54 (navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31);
58 * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
61 exports.formatters.j = function(v) {
62 return JSON.stringify(v);
67 * Colorize log arguments if enabled.
72 function formatArgs() {
74 var useColors = this.useColors;
76 args[0] = (useColors ? '%c' : '')
78 + (useColors ? ' %c' : ' ')
80 + (useColors ? '%c ' : ' ')
81 + '+' + exports.humanize(this.diff);
83 if (!useColors) return args;
85 var c = 'color: ' + this.color;
86 args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1));
88 // the final "%c" is somewhat tricky, because there could be other
89 // arguments passed either before or after the %c, so we need to
90 // figure out the correct index to insert the CSS into
93 args[0].replace(/%[a-z%]/g, function(match) {
94 if ('%%' === match) return;
97 // we only are interested in the *last* %c
98 // (the user may have provided their own)
103 args.splice(lastC, 0, c);
108 * Invokes `console.log()` when available.
109 * No-op when `console.log` is not a "function".
115 // this hackery is required for IE8/9, where
116 // the `console.log` function doesn't have 'apply'
117 return 'object' === typeof console
119 && Function.prototype.apply.call(console.log, console, arguments);
125 * @param {String} namespaces
129 function save(namespaces) {
131 if (null == namespaces) {
132 storage.removeItem('debug');
134 storage.debug = namespaces;
142 * @return {String} returns the previously persisted debug modes
155 * Enable namespaces listed in `localStorage.debug` initially.
158 exports.enable(load());
161 * Localstorage attempts to return the localstorage.
163 * This is necessary because safari throws
164 * when a user disables cookies/localstorage
165 * and you attempt to access it.
167 * @return {LocalStorage}
171 function localstorage(){
173 return window.localStorage;