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 (featureConfig: NMFeatureConfig)
Arguments:
featureConfig
: NMFeatureConfigFeature we want from plugin
Methods
# extensionExists () → boolean static
Return true if browser extension was installed
# connect (timeOutopt: Number) → 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
: NumberConnection timeOut in millisecond. Default to UBNativeMessage.callTimeOut
# disconnect () → * instance
Disconnect from native
# invoke (methodName: String, methodParamsopt: Object, timeoutopt: Number) → Promise instance
Invoke feature method with optional params
Arguments:
methodName
: StringmethodParams
: ObjectDo not pass empty object {} here!
timeout
: Numberoperation timeout. Default to UBNativeMessage#callTimeOut
# onMsgTimeOut (msgIDOrMsg: string) instance
Timeout (eitres called from setTimeout with msgIDOrMsg === [messageID] or from connection error with msgIDOrMsg === [message object]