![](/img/trans.png)
[英]uncaught security exception for the webkitNotifications.createNotification used in chrome extensions
[英]Exception handling in Chrome extensions
我似乎無法在 Chrome 擴展文檔中找到有關異常處理的任何內容。 所有的異步 api 都很難避免在代碼中到處亂放 try/catch 語句。
如何將全局異常處理程序添加到我的后台頁面,以便在發生異常時進行一些資源清理?
您可以使用chrome.runtime.lastError
在執行腳本回調中獲取錯誤:
chrome.tabs.executeScript(tabId, details, function() {
if (chrome.runtime.lastError) {
var errorMsg = chrome.runtime.lastError.message
if (errorMsg == "Cannot access a chrome:// URL") {
// Error handling here
}
}
})
我一直無法找到全局錯誤處理程序,但我能夠提出一個同樣有效的解決方案。
不過,這確實取決於您調用的方法。 我的大部分錯誤來自在chrome://
頁面或 chrome 網上商店頁面上調用chrome.tabs.executeScript()
。 此函數的最后一個參數是包含results
數組的回調。 我發現如果這是undefined
我會收到一個錯誤。 通過這種方式,我能夠設置一個簡單的錯誤處理功能,以在出現錯誤時通知用戶。
chrome.tabs.executeScript(null, {file: '/path/to/file.js'}, function(results) {
if (results === undefined) {
// Fire error handling code
}
});
再次,如果這適用於您正在調用的方法,但我能夠以這種方式做我想做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.