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.

  
      // without systemJS:
 // const nmScannerModule = require('@ub-e/nm-scanner')
 // nmScannerModule.connect().then(...)

 System.import('@ub-e/nm-scanner').then(function (nmScannerModule) {
    return nmScannerModule.connect()
 }).then(function (nmScanner) {
   return nmScanner.scan()
 }).then(UB.logDebug)

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

 nm.invoke('methodName', {a: 'method param'})
  

# new UBNativeMessage (featureConfigNMFeatureConfig)

Arguments:

Methods

# extensionExists () → boolean static

Return true if browser extension was installed

# connect (timeOutoptNumber) → Promise.<UBNativeMessage> instance

Connect to native messages host. Check extension & host is installed and up to date (according to feature config passed to constructor).

Return:

resolved to UBNativeMessage or rejected to installation/upgrade message

Arguments:
  • timeOut: Number

    Connection timeOut in millisecond. Default to UBNativeMessage.callTimeOut

# disconnect () → * instance

Disconnect from native

# invoke (methodNameString, methodParamsoptObject, timeoutoptNumber) → Promise instance

Invoke feature method with optional params

Arguments:

# onMsgTimeOut (msgIDOrMsgstring) instance

Timeout (eitres called from setTimeout with msgIDOrMsg === [messageID] or from connection error with msgIDOrMsg === [message object]

Arguments: