2010-02-17 80 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

相關問題