Hi,
We are currently working on a great improvement in the page-mod/worker API.
For now, the only way to communicate between a content script and an add-on was postMessage.
This was quite limiting and leads to big switch/case when you had to manage multiple kind of events.
To improve that, we are going to provide a new attribute on Worker objects, here, called `port`.
This attribute will have an `emit` and `on` methods, like in many others place in the API. (i.e. an EventEmitter object)
- emit(name, data) allow to send an event with a name and a data object
- on(name, callback) allow to register a function that will be called when the content script emit an event with name equal to name argument.
Here is an example of code:
let worker = require('tabs').activeTab.attach({
contentScript: 'new ' + function WorkerScope() {
self.on('addon-to-content', function (data) {
self.emit('content-to-addon', data);
});
}
});
worker.port.on('content-to-addon', function (data) {
// data is equal to "event data"
});
worker.port.emit('addon-to-content', 'event data');
We are convinced that this new feature is going to ease the use of content scripts,
but we would like to be sure that this new attribute name is appropriate and really clear for everyone.
Here, I named this attribute `port`, but we are thinking about `sink` or `pipe` or `event` or `events` or ...
So it would be really awesome to have anybody thought about the best name for this new attribute!
If you want to track this new feature, here is the bugzilla item:
https://bugzilla.mozilla.org/show_bug.cgi?id=635748----
Alex