Addon SDK - 上下文菜单和页面修改工作者 [英] Addon SDK - context-menu and page-mod workers

查看:147
本文介绍了Addon SDK - 上下文菜单和页面修改工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理与页面模块进行通信的上下文菜单,并遇到问题。

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 / tab in view as long as I do not refresh the page. When I refresh the page a new worker is created and the context menu cannot communicate with the worker.

我现在有两个相同的工作人员,但它就像旧工作程序已过期。这意味着onMessage中的这个循环:不起作用,因为它拾取了第一个worker。

I now have two identical workers but it is like the old one has expired. That means this loop in onMessage: does not work because it picks up 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 have been looking to remove the old worker on refresh but there seems to be no option to do so. Am I fundamentally missing something about the handling of workers?

我收到的错误是:
错误:页面当前是隐藏的,不能再使用了它再次可见。

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

这与以下事实一致:就工人而言,我现在正在查看同一选项卡中的新页面。我认为worker.on('detach',function(){})应该处理这个问题,但似乎这只是关闭选项卡。

This is consistent with the fact that as far as the worker is concerned I am now looking at a new page in the same tab. I thought worker.on('detach', function(){}) was supposed to handle this but it seems this is only on the closing of the tab.

任何建议将不胜感激。

已添加
好​​一点休息后我决定使用其他地方推荐的detachWorker功能进行分离。我把它放在我的pageMod对象的顶部,如下所示

added OK after a little break I decided to use the detachWorker function recommended elsewhere for detach. I placed it at the top of my pageMod object as 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 issue (for now) although I don't think it is the correct approach. Any advances on a solution :).

推荐答案

也进入这个问题。 Worker对象似乎在过去的某些页面中保留(并且在历史记录中返回和转发时重复使用)。解决方案是收听 pagehide pageshow 事件,以便只保留当前显示的数组:

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

请注意 array.add 功能需要注意不添加重复项。

Note that array.add function takes care of not adding duplicates.

这篇关于Addon SDK - 上下文菜单和页面修改工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆