3
我正在研究Chrome擴展,並遇到以下問題。 當某個事件發生時,我的背景頁面使用chrome.tabs.create
API創建一個新選項卡(頁面)併發送一個對象。消息傳遞中丟失了對象類型
發送的對象(稱爲項目)是一個對象的列表,具有稱爲項目的特定類(原型)。
這裏是一些代碼:
// in background.html
chrome.tabs.create({index: 0, url: "results.html"}, function(tab) {
chrome.tabs.sendRequest(tab.id, {'items': itemsList}, function(response) {
console.log(response.farewall);
});
});
在另一方面,在新創建的頁面,我收到發送對象
// newpage.html
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
console.dir(request.items);
sendResponse({});
}
);
的問題是,當我收到的對象名單,在newpage.html
中,對象類型丟失。事實上,在新的background page
中使用console.dir()
時,itemsList中的對象類型被正確報告,但在newpage.html
的接收項目列表對象中沒有被正確報告。
我可以手動通過串手動序列化,他background.html
的數據,並在newpage.html
手動反序列化,但我想知道是否有更好的辦法承受這一點,並防止在對象類型(即項目)該列表丟失。
如果'itemsList'已經是一個json對象,爲什麼不傳遞沒有''items''鍵的對象呢?多數民衆贊成在我通常這樣做,我可以直接在接收端使用'request.item_name'訪問對象項目...也許我誤解了你的問題? –