简体   繁体   English

从chrome到非特权代码的Firefox插件SDK消息

[英]Firefox Addon SDK message from chrome to unprivileged code

I am trying to pass messages between a Firefox Addon worker script and the webpage's javascript. 我正在尝试在Firefox插件工作程序脚本和网页的javascript之间传递消息。 I found this which explains how to send a message to the script and get a reply back, but I want to simply send the message from chrome to the unprivileged code. 我发现了这一点 ,它解释了如何将消息发送到脚本并获得回复,但是我只想将消息从chrome发送到非特权代码。 I have already tried using the following two methods. 我已经尝试使用以下两种方法。

I am sending from a worker attached to a panel and I want to receive the message in some javascript that I have injected into the page DOM. 我是从附加到面板的工作人员发送的,我想用一些注入到页面DOM中的JavaScript接收消息。

To send 发送

    var element = document.createElement("MyExtensionDataElement");
    element.setAttribute('detail', "hi");
    document.documentElement.appendChild(element);
    console.log("created the event", element);

    var evt = document.createEvent("Events");
    evt.initEvent("MyExtensionEvent1", true, false);
    element.dispatchEvent(evt);

To receive 接受

    document.addEventListener("MyExtensionEvent", function(e) {
        myExtension.myListener(e);
    }, false, true);

And also via a simple CustomEvent 并通过一个简单的CustomEvent

To send 发送

var e = new CustomEvent("event",{detail:"string here"});
window.dispatchEvent(e);

To recieve 接收

    window.addEventListener("event",function(e){
        console.log(e.detail);
    });

The first one sends the message but it isnt received, and the second one fails to even create the CustomEvent in the first place. 第一个发送消息,但没有收到,第二个甚至都未能创建CustomEvent。 I'd appreciate any help on this matter and do apologize if the question seems amateurish. 对于这个问题,我们将不胜感激,如果问题是业余的,也请您道歉。 Im new to Firefox Addon Development. 我是Firefox插件开发的新手。

Your receive is wrong, you need to use add 4th argument and set it to try: 您的接收错误,您需要使用add第四个参数并将其设置为尝试:

So 所以

window.addEventListener("event",function(e){
    console.log(e.detail);
});

Goes to 前往

window.addEventListener("event",function(e){
    console.log(e.detail);
}, false, true);

See this topic - initCustomEvent pass data for method doesn't work anymore 请参阅本主题- 方法的initCustomEvent传递数据不再起作用

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

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