Last active
August 29, 2015 14:05
-
-
Save nicoptere/a53b6e8726f8a32d4181 to your computer and use it in GitHub Desktop.
lol() : improving the console.log through dynamic LULZ injection
This file contains 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
/** | |
* lol() | |
* improving the console.log through dynamic LULZ injection | |
* | |
* history | |
* - v0.0 RC - 2014-08-08 - 13h01 : initial release | |
* - v0.1 RC - 2014-08-08 - 13h29 : DogeScript support | |
* | |
* Licensed under WTFPL | |
* | |
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
* Version 2, December 2004 | |
* | |
* Copyright (c) 2014 Nicolas Barradeau <barradeau@gmail.com>; | |
* http://www.barradeau.com/ | |
* | |
* Everyone is permitted to copy and distribute verbatim or modified | |
* copies of this license document, and changing it is allowed as long | |
* as the name is changed. | |
* | |
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
* | |
* 0. You just DO WHAT THE FUCK YOU WANT TO. | |
* | |
* | |
* resources: | |
* - http://tobyho.com/2012/07/27/taking-over-console-log/ | |
* - http://mistupid.com/internet/chattalk.htm | |
* - http://the-toast.net/2014/02/06/linguist-explains-grammar-doge-wow/ | |
*/ | |
/* | |
* CORE SCRIPT : | |
* | |
**/ | |
function lol( enableDogeScript ) | |
{ | |
//default LULZ set | |
var thesaurus = [ | |
"lol", | |
"lolz", | |
"lulz", | |
"lmao", | |
"lmfao", | |
"rofl" | |
]; | |
//DogeScript set | |
enableDogeScript = enableDogeScript || false; | |
var useDogeScript = enableDogeScript == true; | |
if( useDogeScript ) | |
{ | |
thesaurus = [ | |
"such", | |
"much", | |
"very", | |
"many", | |
"so" | |
]; | |
var singleStatements = [ | |
"wow", | |
"amaze", | |
"excite" | |
]; | |
var punctuation = [ | |
".", | |
"!" | |
]; | |
} | |
var console = (window.console = window.console || {}); | |
var original = console[ 'log' ]; | |
console[ "log" ] = function(){ | |
var count = 0; | |
var args = Array.prototype.slice.apply( arguments ); | |
if( !useDogeScript ) | |
{ | |
var id = parseInt( Math.random() * thesaurus.length ); | |
var message = thesaurus[ id ].toUpperCase(); | |
//OMG? | |
if( Math.random() > .5 ) | |
{ | |
message = ( (Math.random() > .5 ) ? "OMG " : "OMFG " ) + message; | |
} | |
//how many exclamationMarks? | |
count = 1 + parseInt( Math.random() * 10 ); | |
while( count-- ) | |
{ | |
message += " !"; | |
} | |
//prepend or append ? | |
if( Math.random() < .5 ) | |
{ | |
message += " =>"; | |
args.unshift( message ); | |
} | |
else | |
{ | |
message = "<= " + message; | |
args.push( message ); | |
} | |
original.apply( console, args ); | |
} | |
else | |
{ | |
var tmp = []; | |
args.forEach( function( item ) | |
{ | |
var lineBreaks = ""; | |
count = parseInt( Math.random() * 5 ); | |
while( count-- )lineBreaks +='\n'; | |
var whiteSpace = ""; | |
count = parseInt( Math.random() * 10 ); | |
while( count-- )whiteSpace +='\t'; | |
var dogeWord = lineBreaks + whiteSpace + thesaurus[ parseInt( thesaurus.length * Math.random() ) ]; | |
tmp.push( dogeWord, item ); | |
var dogePunctuation = ( Math.random() < .5 ) ? "\n" : "!\n"; | |
if( dogePunctuation != "") tmp.push( dogePunctuation ); | |
}); | |
var finalWord = singleStatements[ parseInt( singleStatements.length * Math.random() ) ] + punctuation[ parseInt( punctuation.length * Math.random() ) ]; | |
if( Math.random() > .5 ) tmp.push( finalWord ); | |
//let the magic happen | |
original.apply( console, tmp ); | |
} | |
}; | |
} | |
/* | |
* sample usage : | |
* | |
**/ | |
console.log( "regular console.log:" ); | |
console.log( "a", 123, {b:0, c:function(){} } ); | |
console.log( "boring.\n" ); | |
lol(); | |
console.log( "improved console.log:" ); | |
console.log( "a", 123, {b:0, c:function(){} } ); | |
/** | |
excpeted result: | |
regular console.log: tmp.html:306 | |
a 123 Object {b: 0, c: function} | |
boring. | |
improved console.log: <= OMG LULZ ! ! | |
LOL ! => a 123 Object {b: 0, c: function} | |
*/ | |
/* | |
// enable DogeScript : | |
lol( true ); | |
much a | |
much 123 ! | |
such Object {b: 0, c: function} | |
wow! | |
//*/ | |
/* | |
NB: | |
calling lol() then lol( true ) will mix both. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment