无法让 port.emit 和 port.on 在 Firefox 附加组件中工作
Can't get port.emit and port.on to work in a Firefox add-on
我发送了这样的消息:
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();
单击小部件时,您不会将工作线程作为参数(单击发生时它不存在) - 您可以通过调用 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);
},
相关文章:
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- React组件等待所需道具进行渲染
- 如何创建带有插槽的vue js组件预加载程序
- KnockoutJS-组件-多个实例
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何在react js中将值从一个组件发送到另一个组件
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- React组件-设置页面标题
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 如何在salesforce aura闪电组件中使用jQuery.val()
- 为react组件传递道具的最佳方式
- [Vue warn]:未能解析组件
- 如何控制组件'的createContent函数被激发
- react组件中的绑定方法
- 如何在react js中移动第二个组件
- 多个组件是如何实现的
- Firefox 附加组件 SDK Worker.port 事件不起作用
- 无法让 port.emit 和 port.on 在 Firefox 附加组件中工作