2013-08-19 38 views

回答

1

有兩種方法來檢查chrome.tabs.executeScript調用是否成功:

  1. 檢查是否results屬性是一個數組(它是未定義的故障)。
  2. 檢查chrome.runtime.lastError屬性是否已設置(這是推薦的方式)。
chrome.tabs.executeScript(tabId, { 
    code: '// some code' 
}, function(result) { 
    if (chrome.runtime.lastError) { // or if (!result) 
     // Get the error message via chrome.runtime.lastError.message 
     return; 
    } 
}); 

前面的例子只有當同時插入內容腳本發生錯誤顯示。它不顯示運行時錯誤的任何錯誤。如果您想查明腳本中是否發生錯誤,請打開該選項卡的devtools。如果您需要知道背景頁面是否發生錯誤,請從內容腳本中返回一個值以表明腳本已經(不)正常運行。
最後一個表達式的值傳遞給chrome.tabs.executeScript(在數組中,因爲在設置了allFrames:true時傳遞了多個值)的回調。