简体   繁体   English

附加生成器:多名工作人员正在使用端口?

[英]Add-on Builder: Multiple Workers Using port?

Referring to this question: Add-on Builder: ContentScript and back to Addon code? 提到此问题: 加载项生成器:ContentScript并返回到加载项代码?

Here is my addon code: 这是我的附加代码:

var widget = widgets.Widget({
  id: "addon",
  contentURL: data.url("icon.png"),
  onClick: function() {
    var workers = [];
    for each (var tab in windows.activeWindow.tabs) {
        var worker = tab.attach({contentScriptFile: [data.url("jquery.js"), data.url("myScript.js")]});
        workers.push(worker);
    }
  }
});

And here is myScript.js: 这是myScript.js:

var first = $(".avatar:first");
if (first.length !== 0) {
    var url = first.attr("href");
    self.port.emit('got-url', {url: url});
}

Now that I have multiple workers where do I put 现在我有多名工人,我该放在哪里

worker.port.on('got-url', function(data) {
            worker.tab.url = data.url;
        });

Since in the other question I only had one worker but now I have an array of workers. 因为在另一个问题中我只有一个工人,但是现在我有一系列工人。

The code would be: 该代码将是:

// main.js:
var data = require("self").data;
var windows = require("windows").browserWindows;

var widget = require("widget").Widget({
    id: "addon",
    label: "Some label",
    contentURL: data.url("favicon.png"),
    onClick: function() {
        //var workers = [];
        for each (var tab in windows.activeWindow.tabs) {

            var worker = tab.attach({
                contentScriptFile: [data.url("jquery.js"), 
                data.url("inject.js")]
            });

            worker.port.on('got-url', function(data) {
                console.log(data.url);
                // worker.tab.url = data.url;
            });

            worker.port.emit('init', true);
            console.log("got here");
            //workers.push(worker);
        }
    }
});

// inject.js
$(function() {
    self.port.on('init', function() {
        console.log('in init');
        var first = $(".avatar:first");
        if (first.length !== 0) {
            var url = first.attr("href");
            console.log('injected!');
            self.port.emit('got-url', {url: url});
        }    
    });
});

Edit : sorry, should have actually run the code, we had a timing issue there where the content script was injected before the worker listener was set up, so the listener was not yet created when the 'got-url' event was emitted. 编辑 :抱歉,应该实际上已经运行了代码,我们在设置工作程序侦听器之前已在其中注入了内容脚本,这是一个计时问题,因此在发出“ get-url”事件时尚未创建侦听器。 I work around this by deferring any action in the content script until the 'init' event is emitted into the content script. 我通过将内容脚本中的任何操作推迟到内容脚本中发出“ init”事件之前来解决此问题。

Here's a working example on builder: 这是关于构建器的工作示例:

https://builder.addons.mozilla.org/addon/1045470/latest/ https://builder.addons.mozilla.org/addon/1045470/latest/

The remaining issue with this example is that there is no way to tell if a tab has been injected by our add-on, so we will 'leak' or use more memory every time the widget is clicked. 这个示例剩下的问题是,我们无法判断插件是否已注入选项卡,因此每次单击窗口小部件时,我们都会“泄漏”或使用更多内存。 A better approach might be to inject the content script using a page-mod when it is loaded, and only emit the 'init' event in the widget's onclick handler. 更好的方法可能是在加载页面脚本时使用page-mod注入内容脚本,并仅在小部件的onclick处理程序中发出“ init”事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM