QxQ
QxQ

Reputation: 675

Sharing Functions between Chrome Extensions

I am making an extension (A) for Chrome that communicates with another extension (B). I want A to provide the B a function, but it won't send. I can send strings just fine.

A has the following code. rect is the function in this code.

chrome.extension.onRequestExternal.addListener(
    function(request, sender, sendResponse) {
       obj = {}
       obj.permisions = "all"
       obj.rect = Rect
       alert(obj.permisions+","+obj.rect)
       sendResponse(obj); 
});

...this code works just fine. The alert shows a box that says "all", then prints out the function.

B has the following code.

chrome.extension.sendRequest(ext[i].id, {}, function(lib) {
    alert(lib.permisions+","+lib.rect)
});

The alert on this one shows "all,undefined". Can functions not be passed between extensions?

Upvotes: 1

Views: 136

Answers (1)

Chris Cashwell
Chris Cashwell

Reputation: 22899

While you can certainly communicate between extensions, you can only pass valid JSON. Unfortunately, valid JSON only includes simple data types(String, Number, Boolean, Array, Object* or Null).

One way to do it would be to pass the function as a String and use eval on the receiving end. Could be unsafe, but is doable.

* While a function is technically an Object, in this context Object refers to name:value pairs of the aforementioned simple data types.

Upvotes: 4

Related Questions