Creating MessageChannel from framescript or bootstrap.js

I couldn’t find a way to create a MessageChannel in framescript so what I’m doing is spawning a webworker, having it create the ports, then transfer them both to the framescript:

var portWorker = new Worker(core.addon.path.scripts + 'msgchanWorker.js');
    portWorker.onmessage = function(e) {
        portWorker.terminate();
        var port = e.data.port1;
        var port2 = e.data.port2;
        console.log('port:', port, 'port2:', port2);
    }

I terminate the worker there too.
Here is code in worker:

var msgchan = new MessageChannel();
self.postMessage({
    port1: msgchan.port1,
    port2: msgchan.port2
}, [msgchan.port1, msgchan.port2]);

This works, but spawining a worker, especially during startup of the browser is not ideal.

Does anyone have any other ways to create a MessageChannel in these scopes?

Hi Noitidart Have you tried sending back the messages from the frame script to the main process using the messagemanager API and then handle it there ?

Yep I’m brushing up a API I wrote for that right now. It works pretty great. I should be done touching it up in a couple days. I can help you drop in what I have right now though if your repo is on github.

Thanks. I’m currently working on some frame script stuff now at the minute and cleaning up some code. I’ll see how I get on and If I can’t get it working I’ll message you in a few days. Thanks for the offer. I appreciate it. As for the repo I believe you are already familiar with it, random agent spoofer :wink:

Oh super sweet! Just realized RAS stood for!! hahaha

1 Like

Oh a note from sending message from framescript to bootstrap via messageManager API - communication between framescript and bootstrap is cross process, so transferring is not option. So getting messagechannel ports can’t be sent like that. :frowning: So my framescript-bootstrap comm part of the api, does the same thing, just no transferring of stuff.

The three communications layer are:

  • bootstrap-worker - transferring allowed
  • bootstrap-framescript
  • bootstrap-content / framescript-content - transferring allowed

I have not used bootstrap so I’m not too familiar with it but what I meant was to use the main addon context scope to facilitate communication between the areas that can’t naturally communicate. so framescript <-> messagemanager <-> chrome context <-> any other part of of the addon via exports/require

So if you can access boostrap from anything in /lib/ you can get at it from a frame script.

Another thing you could try is lazily initialize the worker so it is only created on first use rather than startup.

1 Like