8

我正在尝试在不使用 Visual Studio 的情况下创建侧边栏小工具。我四处寻找调试它们的方法,但一切都表明 Visual Studio JIT 调试器是唯一的方法。

有没有人能够在没有 Visual Studio 的情况下调试侧边栏小工具?

4

2 回答 2

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,您还可以使用 VBScriptMsgBox()函数:

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 中!

于 2010-02-18T11:30:51.920 回答
9

在 Windows 7 中添加了一个新的注册表项,该注册表项在给定 PC 上运行时显示脚本错误:

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

设置该值后,您将在发生脚本错误时看到对话框。

于 2010-05-21T19:16:22.757 回答