VulpesOS/assets/webapps/sync.gaiamobile.org/shared/js/iac_handler.js
OverSu b38dea0597 Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
2026-09-30 23:13:03 +02:00

49 lines
1.3 KiB
JavaScript

'use strict';
var IACHandler = {
_eventPrefix: 'iac-',
_ports: {},
init: function onInit() {
var self = this;
window.navigator.mozSetMessageHandler('connection',
function onConnected(request) {
var keyword = request.keyword;
var port = request.port;
// We will save each port by keyword; ports may get overwritten
// sometimes, but that's ok because there should only be one *active*
// port with a given keyword at any time.
self._ports[keyword] = port;
port.onmessage = function onReceivedMessage(evt) {
var message = evt.data;
/*
* System will emit a event when received message
* e.g. iac-ftucomms
* iac-[keywords]
*/
var evtName = self._eventPrefix + keyword;
var iacEvt = document.createEvent('CustomEvent');
/*
* You can get the message by accessing
* `event.detail`
*
* It will store the whole message you passed from postMessage
*/
iacEvt.initCustomEvent(evtName,
/* canBubble: */ true, /* cancelable */ false, message);
window.dispatchEvent(iacEvt);
};
});
},
getPort: function(keyword) {
return this._ports[keyword];
}
};
IACHandler.init();