I have been working on a context-menu that communicates with a page mod and come up against an issue.
I am able to send a communication with right click to the page
Ran into this too. The Worker objects seem to stick around for some past pages (and reused when going Back and Forward in history). The solution is to listen to the pagehide
and pageshow
events so as to only keep the currently shown workers in the array:
var pageMod = require("sdk/page-mod");
var array = require('sdk/util/array');
var pageWorkers = [];
pageMod.PageMod({
onAttach: function(worker) {
array.add(pageWorkers, worker);
worker.on('pageshow', function() { array.add(pageWorkers, this); });
worker.on('pagehide', function() { array.remove(pageWorkers, this); });
worker.on('detach', function() { array.remove(pageWorkers, this); });
// do other work.
}
});
Note that array.add
function takes care of not adding duplicates.