kango

Summary

Name Description
getExtensionInfo Returns an object describing the extension
addMessageListener Registers a message handler for the specified message name
removeMessageListener Removes a message handler that the addMessageListener method registered
dispatchMessage Dispatches a message
invokeAsync Calls function from background script
invokeAsyncCallback Calls asynchronous function from background script

Methods

getExtensionInfo

kango.getExtensionInfo()

Returns a KangoExtensionInfo object describing the extension.

Returns:object describing the extension
Return type:KangoExtensionInfo

Example:

var info = kango.getExtensionInfo();
kango.console.log(info.name); // shows extension name

addMessageListener

kango.addMessageListener(name, callback)

Registers a message handler for the specified message name. Can be used in content scripts.

Arguments:
  • name (string) – message name
  • callback (function(event)) – message handler function

Message handler event:

event = {
    data: variant,  // JSON serializable data object attached to the message
    target: object, // Point to object sent the message
    source: KangoMessageSource // Message source
}

target field could be either KangoBrowserTab or kango objects.

For instance if message came from background to content script then target will be a kango object. If message came from content to background target will be KangoBrowserTab.

The event.source.dispatchMessage call could be used inside message handler function to send message back.

source KangoMessageSource object.

See Messaging API section for more details.

Example:

// background script
kango.addMessageListener('GetData', function(event) {
    // send data back to content script
    event.source.dispatchMessage('SetData', {a: 1});
});

// content script
    kango.addMessageListener('SetData', function(event) {
    kango.console.log(event.data);
});

kango.dispatchMessage('GetData');

removeMessageListener

kango.removeMessageListener(name, callback)

Removes a message handler that the kango.addMessageListener() method registered. Can be used in content scripts.

Arguments:
  • name (string) – message name
  • callback (function(event)) – message handler function

dispatchMessage

kango.dispatchMessage(name, data)

Dispatches a message to background script.

See Messaging API section for more details.

Arguments:
  • name (string) – message name
  • data (variant) – a JSON serializable data to send

Example:

/*
    This will send a message to background scripts
    In this Example message named 'action' will have two fields *data* object
    It is possible to use any JSON serializable object
*/
kango.dispatchMessage('action', {name: 'DoABarrelRoll', count: 5});

invokeAsync

kango.invokeAsync(functionName, argument1, argument2, ..., argumentN, callback)

Calls function from background script. Can be used in content scripts and options page.

Arguments:
  • functionName (string) – function name to invoke
  • argumentN (variant) – variable number of arguments (must be JSON serializable)
  • callback (function(result)) – callback function

Example:

kango.invokeAsync('kango.storage.setItem', 'Item1', 1);

kango.invokeAsync('kango.storage.getItem', 'Item1', function(data) {
    kango.console.log(data);
});

kango.invokeAsync('kango.storage.removeItem', 'item1');


// invoke user defined function with name 'test_func'
// 'test_func' defined as function test_func(a, b){return a+b;} in background script
kango.invokeAsync('test_func', 1, 2, function(val) {
    kango.console.log(val);
});

// invoke function 'foo' from object 'MyObj'
// 'MyObj' definition: var MyObj = {foo: function(x) {return x + 2;}};
kango.invokeAsync('MyObj.foo', 1, function(val) {
    kango.console.log(val);
});

invokeAsyncCallback

kango.invokeAsyncCallback(functionName, argument1, argument2, ..., argumentN, callback)

Calls asynchronous function from background script.

Arguments:
  • functionName (string) – function name to invoke
  • argumentN (variant) – variable number of arguments (must be JSON serializable)
  • callback (function(result)) – callback function

Example:

// background script
function foo(x, callback) {
    window.setTimeout(1000, function() {
        var y = x + 1;
        callback(y);
    });
}

// content script
kango.invokeAsyncCallback('foo', 2, function(result) {
    alert(result);
});

Table Of Contents

Previous topic

API reference

Next topic

kango.console