/**
 * Console History v1.4.0
 * console-history.js
 *
 * Licensed under the MIT License.
 *
 * Written by Sander Laarhoven <sander@laarhoven.org>
 * For Doorbell.io <3
 * https://git.io/console
 * https://sander.tech - https://doorbell.io
 */

/* Allow only one instance of console-history.js */
if (typeof console.history !== 'undefined') {
  throw new Error('Only one instance of console-history.js can run at a time.')
}

/* Alert all watching developers that we are intercepting the
   original console log functions. */
if (typeof ConsoleHistoryHideMessage === 'undefined') {
  console.log('%cAll console logging functions on this page are intercepted and ' +
  'locally stored at \'console.history\'.', 'font-weight: bold; color: red;')
  console.log('Call the original, un-modified, log ' +
  'functions with \'console._log()\', \'console._warn()\' and so on.')
  console.log('%cFor more information, see the console.history project on GitHub: ' +
  'https://git.io/console', 'font-style: italic;')
}

/* Store the original log functions. */
console._log = console.log
console._info = console.info
console._warn = console.warn
console._error = console.error
console._debug = console.debug

/* Declare our console history variable. */
console.history = []

/* Redirect all calls to the collector. */
console.log = function () { return console._intercept('log', arguments) }
console.info = function () { return console._intercept('info', arguments) }
console.warn = function () { return console._intercept('warn', arguments) }
console.error = function () { return console._intercept('error', arguments) }
console.debug = function () { return console._intercept('debug', arguments) }

/* Give the developer the ability to intercept the message before letting
   console-history access it. */
console._intercept = function (type, args) {
  // Your own code can go here, but the preferred method is to override this
  // function in your own script, and add the line below to the end or
  // begin of your own 'console._intercept' function.
  console._collect(type, args)
}

/* Define the main log catcher. */
console._collect = function (type, args) {
  // WARNING: When debugging this function, DO NOT call a modified console.log
  // function, all hell will break loose.
  // Instead use the original console._log functions.

  // All the arguments passed to any function, even when not defined
  // inside the function declaration, are stored and locally available in
  // the variable called 'arguments'.
  //
  // The arguments of the original console.log functions are collected above,
  // and passed to this function as a variable 'args', since 'arguments' is
  // reserved for the current function.
  args; arguments

  // Collect the timestamp of the console log.
  var time = new Date().toUTCString()

  // Make sure the 'type' parameter is set. If no type is set, we fall
  // back to the default log type.
  if (!type) { var type = 'log' }

  // To ensure we behave like the original console log functions, we do not
  // output anything if no arguments are provided.
  if (!args || args.length === 0) { return }

  // Act normal, and just pass all original arguments to
  // the origial console function :)
  console['_' + type].apply(console, args)

  // Get stack trace information. By throwing an error, we get access to
  // a stack trace. We then go up in the trace tree and filter out
  // irrelevant information.
  var stack = false
  try { throw Error('') } catch (error) {
    // The lines containing 'console-history.js' are not relevant to us.
    var stackParts = error.stack.split('\n')
    var stack = []
    for (var i = 0; i < stackParts.length; i++) {
      if (stackParts[i].indexOf('console-history.js') > -1 ||
      stackParts[i].indexOf('console-history.min.js') > -1) {
        continue
      }
      stack.push(stackParts[i].trim())
    }
  }

  // Add the log to our history.
  console.history.push({type: type, timestamp: time, arguments: args, stack: stack})
}

@@ -2930,20 +2930,16 @@
 !type) %7B
- var
  type = 
@@ -3607,28 +3607,24 @@
 t('%5Cn')%0A    
-var 
 stack = %5B%5D%0A 

Fix already defined values.