我的擴展程序在後臺頁面中創建一個對象,並將該對象中的所有配置變量存儲在所有
之間。目的是共同爲
所有內容腳本,因此後臺頁面將其發送到
內容腳本的連接請求被接收後:傳入鉻擴展中的對象
// In background.html
timObject = {
property1 : "Hello",
property2 : "MmmMMm",
property3 : ["mmm", 2, 3, 6, "kkk"],
method1 : function(){alert("Method had been called" +
this.property1)}
};chrome.extension.onConnect.addListener(function(port) {
console.assert(port.name == "forTimObject");
port.postMessage({object:timObject});
});// Now in content script:
var extensionObject = null;
var port = chrome.extension.connect({name: "forTimObject"});
port.onMessage.addListener(function(msg) {
if (msg.object != null)
extensionObject = msg.object;
else
alert("Object is null");
});alert(extensionObject.property1); // This is ok, alert box is displayed with the right contents
alert(extensionObject.method1) //Uncaught TypeError: Object # has no method 'method1'
我在做什麼錯在這裏?
在此先感謝!