Featured post
google chrome - Object passing in chromium extension -
my extension creates object in background page , stores all
configuration variables in object. object common for
content scripts , hence background page sends the
content scripts after connection request received:
// 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});
});// 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 null");
} );alert(extensionobject.property1); // ok, alert box displayed right contents
alert(extensionobject.method1) //uncaught typeerror: object # has no method 'method1'
what doing wrong here?
in advance!
google chrome message passing mechanism works serializing data json:
communication between extensions , content scripts works using message passing [...] message can contain valid json object (null, boolean, number, string, array, or object).
if object sent using message passing, convert json. so, @ time of "stringify]2ing method "method1" lost cannot transformed valid json expression. unfortunately, fails silently , confusing rest of properties of object correctly serialized.
- Get link
- X
- Other Apps
Comments
Post a Comment