Starting in Firefox 38, we have the ability to load "process" scripts.
These are similar to frame scripts, but they run once in each process. They
landed in bug 1133594.
Here is some example code:
-- addon.js --
let ppmm = Cc["@
mozilla.org/parentprocessmessagemanager;1
"].getService(Ci.nsIProcessScriptLoader);
ppmm.loadProcessScript("chrome://whatever/process-script.js", true);
ppmm.addMessageListener("Hello", function(msg) { ... });
-- process-script.js --
if (Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT) {
dump("Welcome to the process script in a content process");
} else {
dump("Welcome to the process script in the main process");
}
sendAsyncMessage("Hello"); // Message is sent using
childprocessmessagemanager
The dump statement will run once in each running content process as well as
in the main process. You can use Components from a process script. When
your add-on is shut down, you should remove process scripts via
ppm.removeDelayedProcessScript("chrome://whatever/process-script.js"). This
will stop the process script from being loaded into new content processes.
You'll also need to send a message to the existing scripts telling them to
unload.
Unfortunately we don't have any documentation on these yet.
Before Firefox 38 (which will be released very soon), you can Cu.import a
JSM from a frame script, as Giorgio described. The JSM will run once per
content process.
-Bill