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;
14 exports.storage = 'undefined' != typeof chrome
15 && 'undefined' != typeof chrome.storage
16 ? chrome.storage.local
33 * Currently only WebKit-based Web Inspectors, Firefox >= v31,
34 * and the Firebug extension (any Firefox version) are known
35 * to support "%c" CSS customizations.
37 * TODO: add a `localStorage` variable to explicitly enable/disable colors
40 function useColors() {
41 // is webkit? http://stackoverflow.com/a/16459606/376773
42 return ('WebkitAppearance' in document.documentElement.style) ||
43 // is firebug? http://stackoverflow.com/a/398120/376773
44 (window.console && (console.firebug || (console.exception && console.table))) ||
46 // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
47 (navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31);
51 * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
54 exports.formatters.j = function(v) {
55 return JSON.stringify(v);
60 * Colorize log arguments if enabled.
65 function formatArgs() {
67 var useColors = this.useColors;
69 args[0] = (useColors ? '%c' : '')
71 + (useColors ? ' %c' : ' ')
73 + (useColors ? '%c ' : ' ')
74 + '+' + exports.humanize(this.diff);
76 if (!useColors) return args;
78 var c = 'color: ' + this.color;
79 args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1));
81 // the final "%c" is somewhat tricky, because there could be other
82 // arguments passed either before or after the %c, so we need to
83 // figure out the correct index to insert the CSS into
86 args[0].replace(/%[a-z%]/g, function(match) {
87 if ('%%' === match) return;
90 // we only are interested in the *last* %c
91 // (the user may have provided their own)
96 args.splice(lastC, 0, c);
101 * Invokes `console.log()` when available.
102 * No-op when `console.log` is not a "function".
108 // this hackery is required for IE8/9, where
109 // the `console.log` function doesn't have 'apply'
110 return 'object' === typeof console
112 && Function.prototype.apply.call(console.log, console, arguments);
118 * @param {String} namespaces
122 function save(namespaces) {
124 if (null == namespaces) {
125 exports.storage.removeItem('debug');
127 exports.storage.debug = namespaces;
135 * @return {String} returns the previously persisted debug modes
142 r = exports.storage.debug;
148 * Enable namespaces listed in `localStorage.debug` initially.
151 exports.enable(load());
154 * Localstorage attempts to return the localstorage.
156 * This is necessary because safari throws
157 * when a user disables cookies/localstorage
158 * and you attempt to access it.
160 * @return {LocalStorage}
164 function localstorage(){
166 return window.localStorage;