2016-06-08 36 views
0

擴展:: uncaught_exception_handler:8錯誤在事件處理(不明):類型錯誤:無法讀取未定義接收錯誤在試圖傳遞消息background.js:

Background.js的特性「送別」:

chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){ 
if(message[message.length - 1] === true) 
    { 
     var AFArray = message; 
     sendResponse({farewell: AFArray}); 
    } 
if(message === "request") 
    { 

     sendResponse({farewell:"TEsting!"}); 
    }}); 

內容腳本:

function AFCopy() 
{ 
    var AFArrayCount = 0; 
    var AFArray = []; 
    //cycle over classes and apply label to array. 
    while(AFArrayCount <4) 
     { 
      if(document.getElementsByClassName("_xd6")[AFArrayCount]) 
       { 
        AFArray[AFArrayCount] = document.getElementsByClassName("_xd6")[AFArrayCount].getAttribute("LABEL"); 

        AFArrayCount++; 
       } 

     } 
    //assign copy signature 
    AFArray.push(true); 
    //test array is constructed correctly 
    console.log(AFArray); 

    //send array to background.js 
    chrome.runtime.sendMessage({greeting: AFArray}, function(response) { 
    console.log(response.farewell);}); 
} 

此功能在onClick事件運行。

+0

也試圖在如果要更換隻是普通的background.js內的語句「sendResponse({」測試!「});」。這也不起作用。 –

回答

2

您要發送的message是:{greeting:AFArray}並且該對象是您的聽衆收到的。

因此message[message.length - 1]沒什麼意義。

它應該是這樣的message.greeting[message.greeting.length - 1]