V. Appendix
[1] Simple javascript inline debugger code
<!---->
Copy the codeThe code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head><title>Javascript Inline Debugger</title></head>
<body>
<script language="javascript" type="text/javascript" src="js_inline_debugger.js"></script>
<input type="button" value="hahaha" onclick="javascript: alert();" style="margin-left: 300px;" />
</body>
</html>
/*
FileName: js_inline_debugger.js
Author: luoluo
Contact: luoluonet_at_yahoo.cn
Date: 2007-6-27
Version: 0.1
Usage:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
</head>
<body>
<script language="javascript" type="text/javascript" src="js_inline_debugger.js"></script>
</body>
</html>
Instruction:
It is a simple javascript inline debugger. You must add xhtml1-transitional dtd to your
html document if you wanna to use the script.
*/
//--------------------------------------------------------------------------//
// Common functions
//--------------------------------------------------------------------------//
// Determine whether it is IE
function isIE() {
return && ;
}
// Remove spaces on both sides of the string
= function() {
var re = /(^\s*)|(\s*)$/g;
return (re, "");
}
// Delete an element in the array
= function(i) {
var o = this[i];
for (var j = i; j < - 1; j ++) {
this[j] = this[j + 1];
}
-- ;
return o;
}
// Determine whether the same element exists in an array
= function(o) {
for (var i = 0; i < ; i ++) {
if (this[i] == o) {
return i;
}
}
return -1;
}
// html encoding
function htmlEncode(s) {
s = (/&/g, "&");
s = (/</g, "<");
s = (/>/g, ">");
s = (/\"/g, """);
s = (/\'/g, """);
return s;
}
// js encoding
function jsEncode(s) {
s = (/\\/g, "\\\\");
s = (/\n/g, "\\n");
s = (/\"/g, "\\\"");
s = (/\'/g, "\\\'");
return s;
}
//--------------------------------------------------------------//
// Command line window tool
//--------------------------------------------------------------//
function Console(parentNode, processInputProc) {
// Window
var panel = ("div");
= "250px";
= "250px";
= "#666666";
= "1px";
= "#ffffff";
= "solid";
= "absolute";
= 100;
(panel);
// Title bar
var title = ("div");
= "250px";
= "15px";
= "#dddddd";
= "12px";
= "verdana,tahoma";
(title);
// Drag window function of title bar
var isDragging = false;
var startPos = new Position(, );
var startMousePos = new Position(0, 0);
= function(evt) {
= "pointer";
}
= function(evt) {
if (isDragging == true) {
return;
}
var event = evt || ;
= ;
= ;
isDragging = true;
}
= function(evt) {
if (isDragging == false) {
return;
}
activateWindow();
var event = evt || ;
= ( - ) + + "px";
= ( - ) + + "px";
}
= function(evt) {
if (isDragging == false) {
return;
}
var event = evt || ;
= ( - ) + ;
= + "px";
= ( - ) + ;
= + "px";
isDragging = false;
}
// Close window function
var closeButton = ("div");
= "13px";
= "13px";
= "#ffffff";
= = "left";
= "12px";
= "1px";
= "#999999";
= "solid";
= "2px";
= "<div style=\"margin-top: -2px;margin-left: 3px;\">x</div>";
(closeButton);
var isVisible = true;
// The window closes
= function(evt) {
isVisible = false;
= "none";
}
// Title bar text
var titleLabel = ("div");
= "14px";
= "200px";
= "11px";
= "#ffffff";
= = "left";
= "verdana,tahoma";
= "Javascript Inline Debugger";
// = isIE() ? -14 : "-14px";
= isIE() ? 18 : "18px";
(titleLabel);
// The middle display part
var showPanel = ("div");
= "250px";
= isIE() ? "221px" : "219px";
= "11px";
= "0";
= "0";
= "auto";
= isIE() ? -1 : "0";
(showPanel);
// Command input box
var cmdArea = ("div");
(cmdArea);
var cmdTextbox = ("input");
= "text";
= "250px";
= "12px";
= "12px";
= "0";
= "0";
= isIE() ? -2 : "0";
= "0";
= "1px";
= "2px";
(cmdTextbox);
// The window is activated or not activated
var isActive = false;
// Activate the window
var activateWindow = function() {
if (! isVisible) {
return;
}
if (isActive) {
return;
}
();
= "#015DF4";
isActive = true;
}
= activateWindow;
// The window is not activated
var deactivateWindow = function() {
if (! isVisible) {
return;
}
if (! isActive) {
return;
}
= "#cccccc";
isActive = false;
}
= activateWindow;
= deactivateWindow;
// Output function
var dbgPrint = function(s) {
+= htmlEncode(s).replace(/\n|(\r\n)/g, "<br />");
if (parseInt() > parseInt()) {
+= parseInt() - parseInt();
}
}
// Call the input processing callback function
= function(evt) {
var event = evt || ;
if ( == 0x0d) {
processInputProc(, dbgPrint);
= "";
}
}
}
// Coordinate class
function Position(x, y) {
= x;
= y;
}
//--------------------------------------------------------------------------//
// Inline debugger class
//--------------------------------------------------------------------------//
function InlineDebugger() {
var bpList = new Array();
var id_eval;
// Set breakpoint
var bp = function(funcName) {
// Check whether eval is hooked
if ((new String(eval)).indexOf("[native code]") < 0) {
return "error: eval function was hooked by other codes in the front.\n";
}
// Save the unhooked eval
id_eval = eval;
var re = /^[a-zA-Z0-9_\.]+$/i;
if (! (funcName)) {
return "error: bad argument of command bp \"" + funcName + "\".\n";
}
try {
id_eval("if (typeof(" + funcName + ") == \"object\" || typeof(" + funcName + ") == \"function\") {var obj = " + funcName + ";}");
} catch (e) {
return "error: " + e + ", invalid function name \"" + funcName + "\".\n";
}
if (obj == undefined) {
return "error: the argument of command bp \"" + funcName + "\" is not a function object.\n";
}
if ((new String(obj)).indexOf("function") < 0) {
return "error: the argument of command bp \"" + funcName + "\" is a property, a function is required.\n";
}
if ((funcName) >= 0) {
return "error: there is a breakpoint on function \"" + funcName + "\"\n";
}
try {
var sc = "window." + (/\./g, "_") + "_bak = " + funcName + ";\n" +
funcName + " = " +
"function() {\n" +
" var args = \"\";\n" +
" for (var i = 0; i < ; i ++) {\n" +
" args += arguments[i] + (i == ( - 1) ? \"\" : \",\");\n" +
" }\n" +
" if (confirm(\"function \\\"" + funcName + "\\\" was called, execute it?\\n\\narguments:\\n\" + args + \"\\n\\ncaller:\\n\" + " + funcName + ".caller)) {\n" +
" id_eval(\"" + (/\./g, "_") + "_bak(\\\"\" + jsEncode(args) + \"\\\")\");\n" +
" }\n" +
"};" +
"\n";
id_eval(sc);
(funcName);
return "* breakpoint on function \"" + funcName + "\" added successfully.\n";
} catch (e) {
return "unkown error: " + e + ".\n";
}
}
// Enumerate all breakpoints
var bl = function() {
if ( == 0) {
return "* no breakpoint.\n";
}
var bps = "* " + + " breakpoints: \n";
for (var i = 0; i < ; i ++) {
bps += i + " - " + bpList[i] + "\n";
}
return bps;
}
// Clear a breakpoint
var bc = function(n) {
try {
n = parseInt(n);
} catch (e) {
return "error: bc command requires a numeric argument.\n";
}
if ( == 0) {
return "error: no breakpoint.\n";
}
var funcName = (n);
try {
eval(funcName + " = window." + (/\./g, "_") + "_bak;");
return "* breakpoint on function \"" + funcName + "\" deleted successfully.\n";
} catch (e) {
return "error: " + e + ".\n";
}
}
// help
var help = function() {
var s = "debug commands:\n\n" +
"bp <function name> - set a breakpoint on a function, . \"bp \".\n" +
"bl - list all breakpoints.\n" +
"bc <breakpoint number> - remove a breakpoint by specified number, . \"bc 0\".\n" +
"help - help information.\n"
"\n";
return s;
}
// Process commands
= function(cmd) {
cmd = ();
var cmdParts = (/\s+/g);
var cmdName;
var cmdArg;
if ( == 1) {
cmdName = cmd;
} else {
cmdName = cmdParts[0];
cmdArg = cmdParts[1];
}
switch (cmdName) {
case "bp":
if (cmdArg == undefined) {
return "error: bp command requires an argument.\n";
} else {
return bp(cmdArg);
}
break;
case "bl":
return bl();
break;
case "bc":
if (cmdArg == undefined) {
return "error: bc command requires an argument \"number of breakpoint\".\n";
} else {
return bc(cmdArg);
}
break;
case "help":
return help();
break;
default: return "error: command \"" + cmdName + "\" not found, you can get information by \"help\" command.\n";
break;
}
}
}
//-----------------------------------------------------------------------------//
// Main process
//-----------------------------------------------------------------------------//
/*try {
debugger;
} catch (e) {}*/
var id = new InlineDebugger();
var console = new Console(, function(s, printProc){printProc((s));});