1

我正在发送这样的消息:

self.port.emit("nodes_grubed", textNodesValues);

并想对此做出反应:

worker.port.on("nodes_grubed", requestRenderedtext);

但它不起作用。我应该改变什么?你可以在这里试试我的插件。

main.js

var widgets = require("widget");
var tabs = require("tabs");
var self = require("self");

function requestRenderedtext(textNodesText) {
  console.log('text nodes requesting');
} 

var widget = widgets.Widget({
  id: "mozilla-link",
  label: "Mozilla website",
  contentURL: "http://www.mozilla.org/favicon.ico",
  onClick: 
    function(worker) {
      tabs.activeTab.attach({
        contentScriptFile: self.data.url("process.js")
      });
      worker.on("nodes_grubed",requestRenderedtext);
      worker.port.on("nodes_grubed",requestRenderedtext);
    },
  onAttach:
    function(worker) {
      worker.on("nodes_grubed",requestRenderedtext);
      worker.port.on("nodes_grubed",requestRenderedtext);
    }
});
widget.on("nodes_grubed",requestRenderedtext);
widget.port.on("nodes_grubed",requestRenderedtext);

process.js

function getTextNodes() {
  var walker = document.createTreeWalker(
    document.body, 
    NodeFilter.SHOW_TEXT, 
    null, 
    false
  );

  var node;
  textNodes = [];
  var textNodesValues=[];
  while(node = walker.nextNode()) {
    if (node.parentNode.tagName!='SCRIPT') {
      textNodesValues.push(node.nodeValue);
      textNodes.push(node)   
    }
  }
  if (textNodesValues.length!=0) {
    console.log('emmiting nodes');
    self.port.emit("nodes_grubed", textNodesValues);
  }
}

getTextNodes();
4

1 回答 1

2

单击小部件时,您不会将工作人员作为参数(单击发生时它不存在)-您通过调用来创建它tabs.activeTab.attach()。您应该使用 的返回值tabs.activeTab.attach(),如下所示:

  onClick: 
    function() {
      var worker = tabs.activeTab.attach({
        contentScriptFile: self.data.url("process.js")
      });
      worker.port.on("nodes_grubed", requestRenderedtext);
    },
于 2012-05-09T07:44:33.123 回答