YannBergonzat

dump()

Feb 21st, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function dump_r(obj, levelmax, level) {
  2.     level = level || 0;
  3.    
  4.     if(levelmax != 0 && level >= levelmax) return;
  5.    
  6.     var out = '';
  7.    
  8.     if(typeof(obj) == "object" || typeof(obj) == "array") {
  9.         for (var i in obj) {
  10.             for(var j=0; j<level; j++) {
  11.                 out += "    ";
  12.             }
  13.             if(typeof(obj[i]) == "object" || typeof(obj[i]) == "array") {
  14.                 out += i + ": (" + typeof(obj[i]) + ")\n";
  15.                 out += dump_r(obj[i], levelmax, level+1) || '';
  16.             }
  17.             else {
  18.                 out += i + ": (" + typeof(obj[i]) +") " + obj[i] + "\n";
  19.             }
  20.         }
  21.     }
  22.     else {
  23.         out += "(" + typeof(obj) + ") " + obj;
  24.     }
  25.  
  26.     return out;
  27. }
  28.  
  29. function dump(obj, inalert, levelmax) {
  30.     levelmax = levelmax || 3;
  31.     level_max = levelmax;
  32.    
  33.     inalert = inalert || false;
  34.    
  35.     if(!inalert) {
  36.         var pop = document.createElement('pre');
  37.             pop.style.position = "fixed";
  38.             pop.style.left = "0";
  39.             pop.style.top = "0";
  40.             pop.style.zIndex = "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";
  41.             pop.style.fontSize = "0.8em";
  42.             pop.style.fontFamily = "Consolas";
  43.             pop.style.width = (window.innerWidth - 10)+"px";
  44.             pop.style.height = (window.innerHeight - 10)+"px";
  45.             pop.style.overflow = "auto";
  46.             pop.style.padding = "5px";
  47.             pop.style.background = "white";
  48.             pop.style.color = "black";
  49.             pop.style.margin = "0";
  50.             pop.ondblclick = function() {
  51.                 document.body.removeChild(this);
  52.             };
  53.                        
  54.             pop.innerHTML = dump_r(obj, levelmax).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;") + '<p style="position:absolute; right:20px;top:0;position:fixed;">Double-cliquez pour fermer</p>';
  55.            
  56.             document.body.appendChild(pop);
  57.     }
  58.     else {
  59.         alert(dump_r(obj, levelmax));
  60.     }
  61. }
Add Comment
Please, Sign In to add comment