Addon SDK - context menu and work pages

I am working on a context menu that links to a page mod and is facing a problem.

I can send a message with the right mouse button on the page / tab until I refresh the page. When I refresh the page, a new working menu is created and the context menu cannot communicate with the working one.

Now I have two identical workers, but he looks like an old one, expired. This means that this loop in onMessage: does not work because it raises the first worker.

for (index = 0; index < workers.length; index += 1) {
    if (workers[index].tab.index === tabs.activeTab.index) {
    workers[index].port.emit("rightClick", string, ss.storage.product);
    }
}

I was looking to remove the old worker when upgrading, but there seems to be no way to do this. Did I fundamentally miss something about working with workers?

The error I get is: Error: The page is currently hidden and can no longer be used until it is visible again.

This is consistent with the fact that with respect to the worker, a new page is now viewing me on the same tab. I thought work.on ('detach', function () {}) should have handled this, but it looks like it is only when the tab is closed.

Any advice would be appreciated.

added OK after a short break, I decided to use the detachWorker function, recommended elsewhere for disconnecting. I placed it at the top of my pageMod object as shown below

// Clean up duplicate worker
for (index in workers) {
    if(workers[index].url === worker.url && workers[index].tab.index === worker.tab.index) {
        detachWorker(workers[index], workers);
    }
}

This fixes the problem (for now), although I don't think this is the right approach. Any success in solving :).

+4
source share
3 answers

. , , ( ). , pagehide pageshow, :

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.
  }
});

, array.add .

+6

, . , , detach, :

const { add, remove } = require("sdk/util/array");
const workers = [];

require("sdk/page-mod").PageMod({
  include: "*",
  contentScript: "alert('executed')",
  onAttach: function(worker) {
    add(workers, worker);
    console.log('attached: ', workers.length);

    worker.on('detach', function() {
      remove(workers, worker);
      console.log('detached: ', workers.length);
    })
  }
});

, . , , , , : (workers[index].tab === tabs.activeTab). , Add-on SDK 1.14 id, . for…in : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in . ...:

for (let worker of workers) { ... }
+1

ZERO is right, the event is detachemitted on reboot.
But when I open another page on the same tab, there is no disconnection event! So, for example, when I loaded some page A on tab 1 and click on the link that opens another page B on the same tab 1, I have to disable the worker for page A manually (?)

var workers = [];    

require('page-mod').PageMod({
  include: '*',
  contentScriptWhen: 'end',
  contentScript: [require('self').data.url('bla.js')],
  onAttach: function(worker) {
    var w = workers.length;
    while (w--) {
      if (worker.tab === workers[w].tab) {
        workers.splice(w, 1);
        break;
      }
    }
    workers.push(worker);
    // other stuff
  }
});
0
source

All Articles