UBNativeMessage

UBNativeMessage

Class for communicate with native messages plugin content script. DOM element with id="ubExtensionPageMessageObj" must be present on the target page.

If target page is loaded into iframe then parent (iframe owner) page must contains a DOM element with id="ubExtensionPageMessageObj".

The preferred way to communicate with native messages plugin feature is a UBNativeMessage descendants, for example UBNativeScanner for scanning etc.

Usage:

 var nm = new UBNativeMessage('scanner');
 nm.connect().done(UB.logDebug);
 nm.connect.then(function(nm){
     UB.logDebug('connected to feature version', nm.featureVersion);
     return nm.invoke('methodName', {a: 10, b: 20})
 }).then(UB.logDebug).then(nm.disconnect.bind(nm));


 var nm = new UBNativeMessage();
 nm.onMessage = function(message){
    console.log(message);
 };
 nm.onDisconnected = function(sender){
    console.log('disconnected');
 };
 nm.connect(5000).done( function(nm){
     nm.sendMessage({text: 'Message : Hello!'});
 });

Constructor

new UBNativeMessage(featureopt)

Arguments:
  1. [feature] (String)  Feature we want from plugin. Feature<->application decoding is accessible via UBNativeMessage#features object

Members

features: Object static

Registered features.

Methods

invoke(methodName, methodParamsopt, timeoutopt) → Promise

Invoke feature method with optional params
Arguments:
  1. methodName (String)
  2. [methodParams] (Object)  Do not pass empty object {} here!
  3. [timeout] (Number)  operation timeout. Default to UBNativeMessage#callTimeOut

extensionExists()boolean static

Return true if browser extension was installed

connect(timeOutopt) → Promise

Connect to native messages host. Check extension & host is installed and up to date (according to UBNativeMessage.features).
Arguments:
  1. [timeOut] (Number)  Connection timeOut in millisecond. Default to UBNativeMessage.callTimeOut

disconnect() → *

Disconnect from native