Addon SDK - context-menu and page-mod workers

后端 未结 3 1758
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 08:42

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

3条回答
  •  伪装坚强ぢ
    2020-12-18 09:05

    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.

提交回复
热议问题