/**1* This is the web browser implementation of `debug()`.2*3* Expose `debug()` as the module.4*/56exports = module.exports = require('./debug');7exports.log = log;8exports.formatArgs = formatArgs;9exports.save = save;10exports.load = load;11exports.useColors = useColors;12exports.storage = 'undefined' != typeof chrome13&& 'undefined' != typeof chrome.storage14? chrome.storage.local15: localstorage();1617/**18* Colors.19*/2021exports.colors = [22'lightseagreen',23'forestgreen',24'goldenrod',25'dodgerblue',26'darkorchid',27'crimson'28];2930/**31* Currently only WebKit-based Web Inspectors, Firefox >= v31,32* and the Firebug extension (any Firefox version) are known33* to support "%c" CSS customizations.34*35* TODO: add a `localStorage` variable to explicitly enable/disable colors36*/3738function useColors() {39// NB: In an Electron preload script, document will be defined but not fully40// initialized. Since we know we're in Chrome, we'll just detect this case41// explicitly42if (window && window.process && window.process.type === 'renderer') {43return true;44}4546// is webkit? http://stackoverflow.com/a/16459606/37677347// document is undefined in react-native: https://github.com/facebook/react-native/pull/163248return (document && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||49// is firebug? http://stackoverflow.com/a/398120/37677350(window && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||51// is firefox >= v31?52// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages53(navigator && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||54// double check webkit in userAgent just in case we are in a worker55(navigator && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));56}5758/**59* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.60*/6162exports.formatters.j = function(v) {63try {64return JSON.stringify(v);65} catch (err) {66return '[UnexpectedJSONParseError]: ' + err.message;67}68};697071/**72* Colorize log arguments if enabled.73*74* @api public75*/7677function formatArgs(args) {78var useColors = this.useColors;7980args[0] = (useColors ? '%c' : '')81+ this.namespace82+ (useColors ? ' %c' : ' ')83+ args[0]84+ (useColors ? '%c ' : ' ')85+ '+' + exports.humanize(this.diff);8687if (!useColors) return;8889var c = 'color: ' + this.color;90args.splice(1, 0, c, 'color: inherit')9192// the final "%c" is somewhat tricky, because there could be other93// arguments passed either before or after the %c, so we need to94// figure out the correct index to insert the CSS into95var index = 0;96var lastC = 0;97args[0].replace(/%[a-zA-Z%]/g, function(match) {98if ('%%' === match) return;99index++;100if ('%c' === match) {101// we only are interested in the *last* %c102// (the user may have provided their own)103lastC = index;104}105});106107args.splice(lastC, 0, c);108}109110/**111* Invokes `console.log()` when available.112* No-op when `console.log` is not a "function".113*114* @api public115*/116117function log() {118// this hackery is required for IE8/9, where119// the `console.log` function doesn't have 'apply'120return 'object' === typeof console121&& console.log122&& Function.prototype.apply.call(console.log, console, arguments);123}124125/**126* Save `namespaces`.127*128* @param {String} namespaces129* @api private130*/131132function save(namespaces) {133try {134if (null == namespaces) {135exports.storage.removeItem('debug');136} else {137exports.storage.debug = namespaces;138}139} catch(e) {}140}141142/**143* Load `namespaces`.144*145* @return {String} returns the previously persisted debug modes146* @api private147*/148149function load() {150var r;151try {152r = exports.storage.debug;153} catch(e) {}154155// If debug isn't set in LS, and we're in Electron, try to load $DEBUG156if (!r && typeof process !== 'undefined' && 'env' in process) {157r = process.env.DEBUG;158}159160return r;161}162163/**164* Enable namespaces listed in `localStorage.debug` initially.165*/166167exports.enable(load());168169/**170* Localstorage attempts to return the localstorage.171*172* This is necessary because safari throws173* when a user disables cookies/localstorage174* and you attempt to access it.175*176* @return {LocalStorage}177* @api private178*/179180function localstorage() {181try {182return window.localStorage;183} catch (e) {}184}185186187