2011-06-21 81 views
0

我正在編寫Google Chrome瀏覽器擴展。將內容腳本中的消息中的數組傳遞給Google Chrome擴展中的背景頁

我想從內容腳本傳遞一個小陣列到消息中的背景頁面。我可以簡單地引用數組名稱還是需要我先從它構造一個JSON對象?

下面是代碼:

在內容SCRIPT

var req; 
var detailWin; 

//drag off the f_foto class 
var searchResult = document.getElementsByClassName("f_foto"); 
alert("Found Class f_foto "+searchResult.length+" times."); 

//collect profile links 
for (var i = 0; i<searchResult.length; ++i) 
{ 
    var profileLink=searchResult[i].getElementsByTagName("a"); 
    profileLinks[i]=profileLink[0].href; 
    // alert(i+1+" of "+searchResult.length+" "+profileLinks[i]+" length of "+profileLinks[i].length); 
} 
for (var i = 0; i<searchResult.length; ++i) 
{ 
    //tell bkgd page to open link 
    chrome.extension.sendRequest({cmd: "openProfile", url: profileLinks[i]}); 

    //BETTER TO SEND WHOLE ARRAY. 
    //LIKE THIS? chrome.extension.sendRequest({cmd: "openProfile", urlList: profileLinks}); 
    //OR SHOULD I MAKE A JSON OBJECT OUT OF IT? 
} 

//IN THE BACKGROUND PAGE 

var detailTabId = null; 
var profileLinks = new Array(); 
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { 
    if(request.cmd == "openProfile") { 
//IF RECEIVING AN ARRAY, PROCESS IT LIKE THIS? 
// profileLinks= request.urlList; 
// console.log=("Received "+ urlList.length + " links."); 
     chrome.tabs.create({url: request.url}, function(tab){ 
     //save tab id so we can close this tab later 
     detailTabId = tab.id; 
     //profile tab is created, inject profile script 
     chrome.tabs.executeScript(tab.id, {file: "profile.js"}); 
     }); 
    } 
}); 

回答

1

數組是一個JSON對象的結構,所以沒有必要做的比你現在正在做什麼其他的事情。

相關問題