2010-02-17 81 views

回答

16

多年来,我没有使用Visual Studio来处理小工具。有几种方法可以在没有它的情况下调试小工具,但不是那么广泛。例如,如果没有适当的调试器连接到进程,则不能使用debugger;命令。你可以做的是使用一个程序像DebugView赶上由System.Debug.outputString()方法输出消息:

function test() 
{ 
    System.Debug.outputString("Hello, I'm a debug message"); 
} 

这样您就可以输出变量转储和你的代码的某些阶段信息等实用资讯,让您随时跟踪它然而你喜欢。

作为替代方案,您可以使用window.prompt()来滚动您自己的调试/脚本暂停消息。 alert()已针对小工具禁用,并且confirm()被覆盖为始终返回true,但它们必须忽略prompt()

function test() 
{ 
    // execute some code 

    window.prompt(someVarToOutput, JSON.stringify(someObjectToExamine)); 

    // execute some more code 
} 

如果您想要在代码执行过程中检查对象的状态,JSON.stringify()方法真的有用。

相反的window.prompt,您还可以使用VBScript MsgBox()功能:

window.execScript(//- Add MsgBox functionality for displaying error messages 
     'Function vbsMsgBox (prompt, buttons, title)\r\n' 
    + ' vbsMsgBox = MsgBox(prompt, buttons, title)\r\n' 
    + 'End Function', "vbscript" 
); 

vbsMsgBox("Some output message", 16, "Your Gadget Name"); 

最后,你可以捕捉所有误差与使用window.onerror事件处理程序脚本。

function window.onerror (msg, file, line) 
{ 
    // Using MsgBox 
    var ErrorMsg = 'An error has occurred'+(line&&file?' in '+file+' on line '+line:'')+'. The message returned was:\r\n\r\n'+ msg + '\r\n\r\nIf the error persists, please report it.'; 
    vbsMsgBox(ErrorMsg, 16, "Your Gadget Name"); 

    // Using System.Debug.outputString 
    System.Debug.outputString(line+": "+msg); 

    // Using window.prompt 
    window.prompt(file+": "+line, msg);   

    // Cancel the default action 
    return true; 
} 

window.onerror事件甚至可以让你输出的行号和文件(仅与IE8准确),其中发生的错误。

祝你好运调试,并记住当你发布你的小工具时不要在任何window.prompts或MsgBoxes离开!

+0

非常有用。谢谢。 我需要一个库来使用JSON对象吗?还是它是Windows gadget主机环境的一部分? – bshacklett 2010-02-18 19:17:27

+0

@bshacklett:它内置于IE8中,但您的小工具需要处于IE8模式。你也可以从http://json.org/js.html获得解析器和字符串 – 2010-02-18 19:25:47

+0

他们是否仅仅通过在系统上安装IE8或者我必须指定什么来运行IE8模式? – bshacklett 2010-02-18 23:27:20

9

在Windows 7新的注册表项已被添加在运行时给定的PC上显示脚本错误:

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Sidebar] 
"ShowScriptErrors"=dword:00000001 

与该值设定,当出现脚本错误,你会看到对话框。

+0

我看不到弹出的对话框。还有什么可能需要这个工作? – 2016-09-22 19:05:07

相关问题