我正在发送这样的消息:
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();