|
|
|
Debug Object
1
wizard04
An object including a few helpful functions for debugging. Functions displaying popups or alerts are limited to a certain number of consecutive times before asking to continue, in case they end up being called in an endless loop or something.
if(!window.config) var config = {};
config.Debug = {
maxPopups: 5 //default number of popups/alerts allowed before asking to continue
};
//static Debug object
var Debug = function()
{
//***** private properties and methods *****//
var maxPopups = window.config.Debug.maxPopups;
var debugWindow;
var popupCount = 0;
var continuePopups = true;
//write a message (HTML) to the debug window
function write(msg)
{
if(!debugWindow || !debugWindow.document)
{
debugWindow = window.open("", "debug"); //create the debug window
if(!debugWindow) //a popup blocker caught it
{
msg = msg.replace(/<br(\s+\/)?>/ig, "\n");
debugAlert(msg); //use an alert instead
return;
}
//debugWindow.document.open("text/html"); //it's text/html by default
debugWindow.document.write("<html><head><title>Debug: "+document.title+"</title></head><body></body></html>");
//note: <title> only works in Opera
debugWindow.document.close();
}
debugWindow.document.title = "Debug: "+document.title; //doesn't work in Opera
debugWindow.document.body.innerHTML += msg+"<br />";
}
//write some code to the debug window
function writeCode(code)
{
code = code.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
write('<pre style="background-color:#EEEEEE; margin:1em 0; min-height:2.5em; overflow:auto;">'+code+'</pre>');
}
//log a message to the javascript/error console, if supported
function logToConsole(msg)
{
if(window.console) window.console.log(msg);
}
//open a new window with specified content (employs maxPopups)
function popup(content, mimeType)
{
if(!continuePopups) return;
if(popupCount >= maxPopups && popupCount % maxPopups == 0) //for every maxPopups popups/alerts
{
//This is a safety mechanism in case popups/alerts are displayed in large quantities
// (e.g., if it was called inside a loop)
continuePopups = window.confirm(popupCount+" debug popups have been displayed. Continue?");
if(!continuePopups) return;
}
var popWindow = window.open("", ""); //create a new window
if(!popWindow) //a popup blocker caught it
{
debugAlert(content); //use an alert instead
return;
}
if(mimeType) popWindow.document.open(mimeType); //it's text/html by default
//As far as I know, text/xml isn't supported. In Firefox it becomes text/plain instead of text/html.
//Also, setting mimeType to text/plain doesn't always work; use popupCode function below instead
popWindow.document.write(content);
popWindow.document.close();
popWindow.document.title = "Debug Popup: "+document.title;
popupCount++;
}
//open a new window with specified content as code (employs maxPopups)
function popupCode(content)
{
content = content.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
popup("<pre>"+content+"</pre>");
}
//displays an alert (employs maxPopups)
function debugAlert(txt)
{
if(!continuePopups) return;
if(popupCount >= maxPopups && popupCount % maxPopups == 0) //for every maxPopups popups/alerts
{
//This is a safety mechanism in case popups/alerts are displayed in large quantities
// (e.g., if it was called inside a loop)
continuePopups = window.confirm(popupCount+" debug popups have been displayed. Continue?");
if(!continuePopups) return;
}
window.alert(txt);
popupCount++;
}
//from http://code.google.com/p/trimpath/wiki/TrimBreakpoint
//allows you to evaluate expressions in the current scope
//usage: Debug.breakpoint(function(s){return eval(s);}, "Note about breakpoint", "1+2");
//the first argument is required as shown (to access the closure)
function breakpoint(evalFunc, msg, initialExpr)
{
msg = msg || "";
var result = "";
var expr = initialExpr || "";
if(expr)
{
try{
result = evalFunc(expr);
}catch(e){
result = e;
}
}
while(true)
{
expr = prompt("BREAKPOINT: " + msg + "\nEnter an expression to evaluate, or Cancel to continue.\n\nResult:\n" +
result, expr);
if(!expr) return; //cancelled or left blank
try{
result = evalFunc(expr);
}catch(e){
result = e;
}
}
}
function resetPopups()
{
popupCount = 0;
continuePopups = true;
maxPopups = window.config.Debug.maxPopups; //in case it changed
}
//***** public properties and methods *****//
return {
write: write,
writeCode: writeCode,
log: logToConsole,
popup: popup,
popupCode: popupCode,
alert: debugAlert,
breakpoint: breakpoint,
resetPopups: resetPopups
}
}(); //initialize Debug
config.Debug = {
maxPopups: 5 //default number of popups/alerts allowed before asking to continue
};
//static Debug object
var Debug = function()
{
//***** private properties and methods *****//
var maxPopups = window.config.Debug.maxPopups;
var debugWindow;
var popupCount = 0;
var continuePopups = true;
//write a message (HTML) to the debug window
function write(msg)
{
if(!debugWindow || !debugWindow.document)
{
debugWindow = window.open("", "debug"); //create the debug window
if(!debugWindow) //a popup blocker caught it
{
msg = msg.replace(/<br(\s+\/)?>/ig, "\n");
debugAlert(msg); //use an alert instead
return;
}
//debugWindow.document.open("text/html"); //it's text/html by default
debugWindow.document.write("<html><head><title>Debug: "+document.title+"</title></head><body></body></html>");
//note: <title> only works in Opera
debugWindow.document.close();
}
debugWindow.document.title = "Debug: "+document.title; //doesn't work in Opera
debugWindow.document.body.innerHTML += msg+"<br />";
}
//write some code to the debug window
function writeCode(code)
{
code = code.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
write('<pre style="background-color:#EEEEEE; margin:1em 0; min-height:2.5em; overflow:auto;">'+code+'</pre>');
}
//log a message to the javascript/error console, if supported
function logToConsole(msg)
{
if(window.console) window.console.log(msg);
}
//open a new window with specified content (employs maxPopups)
function popup(content, mimeType)
{
if(!continuePopups) return;
if(popupCount >= maxPopups && popupCount % maxPopups == 0) //for every maxPopups popups/alerts
{
//This is a safety mechanism in case popups/alerts are displayed in large quantities
// (e.g., if it was called inside a loop)
continuePopups = window.confirm(popupCount+" debug popups have been displayed. Continue?");
if(!continuePopups) return;
}
var popWindow = window.open("", ""); //create a new window
if(!popWindow) //a popup blocker caught it
{
debugAlert(content); //use an alert instead
return;
}
if(mimeType) popWindow.document.open(mimeType); //it's text/html by default
//As far as I know, text/xml isn't supported. In Firefox it becomes text/plain instead of text/html.
//Also, setting mimeType to text/plain doesn't always work; use popupCode function below instead
popWindow.document.write(content);
popWindow.document.close();
popWindow.document.title = "Debug Popup: "+document.title;
popupCount++;
}
//open a new window with specified content as code (employs maxPopups)
function popupCode(content)
{
content = content.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
popup("<pre>"+content+"</pre>");
}
//displays an alert (employs maxPopups)
function debugAlert(txt)
{
if(!continuePopups) return;
if(popupCount >= maxPopups && popupCount % maxPopups == 0) //for every maxPopups popups/alerts
{
//This is a safety mechanism in case popups/alerts are displayed in large quantities
// (e.g., if it was called inside a loop)
continuePopups = window.confirm(popupCount+" debug popups have been displayed. Continue?");
if(!continuePopups) return;
}
window.alert(txt);
popupCount++;
}
//from http://code.google.com/p/trimpath/wiki/TrimBreakpoint
//allows you to evaluate expressions in the current scope
//usage: Debug.breakpoint(function(s){return eval(s);}, "Note about breakpoint", "1+2");
//the first argument is required as shown (to access the closure)
function breakpoint(evalFunc, msg, initialExpr)
{
msg = msg || "";
var result = "";
var expr = initialExpr || "";
if(expr)
{
try{
result = evalFunc(expr);
}catch(e){
result = e;
}
}
while(true)
{
expr = prompt("BREAKPOINT: " + msg + "\nEnter an expression to evaluate, or Cancel to continue.\n\nResult:\n" +
result, expr);
if(!expr) return; //cancelled or left blank
try{
result = evalFunc(expr);
}catch(e){
result = e;
}
}
}
function resetPopups()
{
popupCount = 0;
continuePopups = true;
maxPopups = window.config.Debug.maxPopups; //in case it changed
}
//***** public properties and methods *****//
return {
write: write,
writeCode: writeCode,
log: logToConsole,
popup: popup,
popupCode: popupCode,
alert: debugAlert,
breakpoint: breakpoint,
resetPopups: resetPopups
}
}(); //initialize Debug




There are currently no comments for this snippet.