Last active
January 2, 2018 19:23
-
-
Save jhyland87/5d4398f36ae21b618d4b6922e96e8e95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @desc An attempt at persistent browser console log history | |
* @note How To Use: | |
* 1) Copy/paste all below JS into the browsers JS console | |
* 2) Use any of the console.log/debug/warn/error functions | |
* 3) Refresh the page | |
* 4) Repeat steps #1 & #2 a few times | |
* 5) Execute export_console_logs via the browser console (passing the log type as the only argument) to get the log history | |
* @see https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API | |
*/ | |
var CONFIG = { | |
enable: true, | |
wrap_methods: [ 'log', 'debug', 'warn', 'error' ], | |
log_limit: 10, | |
oem_methods: { | |
names: Object.keys( Object.getOwnPropertyDescriptors(console) ), | |
orig: Object.assign({}, window.console ) | |
} | |
} | |
function console_log_wrapper( method ){ | |
if ( ! method || typeof method !== 'string' ) | |
throw new Error('No method type to wrap defined') | |
if ( CONFIG.oem_methods.names.indexOf( method ) === -1 ) | |
throw new Error('No such console method: ' + method ) | |
// Set original value | |
if ( ! localStorage.getItem( 'console.'+method ) ){ | |
localStorage.setItem( 'console.'+method, 'Wrapper Initiated' ) | |
localStorage.setItem( 'count-console.'+method, 0 ) | |
} | |
return function(){ | |
var log_history = localStorage.getItem( 'console.'+method ).split('\n') | |
var log_count = parseInt(localStorage.getItem( 'count-console.'+method )) | |
var new_log_count = log_count+1 | |
CONFIG.oem_methods.orig.debug('Current log history:', log_history.join('\n')) | |
var log_args = Array.from(arguments) | |
var d = new Date() | |
var logmsg = "["+ d.toLocaleTimeString() + "] console."+method+"('"+ log_args.join("', '") +"')')" | |
log_history.unshift(logmsg) | |
localStorage.setItem( 'console.'+method, '['+new_log_count+'] '+logmsg + "\n" + log_history.slice( 0, CONFIG.log_limit-1 ).join('\n') ) | |
localStorage.setItem( 'count-console.'+method, new_log_count) | |
CONFIG.oem_methods.orig.debug('updated '+method+':' + localStorage.getItem( 'console.'+method )) | |
CONFIG.oem_methods.orig[ method ].apply( this, log_args ) | |
} | |
} | |
function export_console_logs( method ){ | |
var log_data = localStorage.getItem( 'console.' + method ) | |
var log_count = parseInt(localStorage.getItem( 'count-console.'+method )) | |
if ( ! log_data ) | |
alert('No log data') | |
else | |
alert('Data for console.'+method+':\n' + log_data) | |
} | |
if ( CONFIG.enable ) { | |
CONFIG.wrap_methods.forEach(function( method ){ | |
if ( CONFIG.oem_methods.names.indexOf( method ) === -1 ){ | |
alert('No original console method named ' + method) | |
return | |
} | |
var wrapped_fn = console_log_wrapper( method ) | |
console[method] = wrapped_fn | |
window.console[method] = wrapped_fn | |
CONFIG.oem_methods.orig.debug('[FACTORY] Wrapped console method:', method) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment