Firefox port.emit和port.on无法在扩展中使用 [英] Firefox port.emit and port.on not working in extension

查看:63
本文介绍了Firefox port.emit和port.on无法在扩展中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行firefox扩展.我需要与后台脚本(main.js)交换数据,因此我尝试使用端口,但是它不起作用.

I am trying to make a firefox extension. I need to exchange data with the background script(main.js) so I am trying to use port but it doesn't work.

//Content.js
self.port.on("alert",function(){alert()});//Listen to message 
self.port.emit("message",{message:"Hello"});

在main.js中,这就是我添加工作程序的方式.因此,基本上,当内容脚本发送消息"时,后台脚本发送警报",而内容脚本警报.那不会发生

And in the main.js this is how I add the worker. So bascically, when the content script sends "message", the background script sends "alert" and the content script alerts. That doesn't happen

//main.js

pageMod.PageMod({
  include: ["https://play.google.com/*"],
  contentScriptWhen: 'ready',
  contentScriptFile: [data.url("jquery.js"),data.url("jquery.knob.js"),data.url("purl.js"),data.url("content.js")],
  contentScriptOptions: {
    inUrl: data.url("in.png"),
    outUrl: data.url("out.png"),
    logoUrl: data.url("logoimage.png")
  },
  contentStyleFile: [data.url("css/inject.css")],
  onAttach: function(worker) {//Ttach
    alert("hello there")
    worker.on("message",function(){

        worker.emit("alert",{message:"Hello"});
    })

  }
});

什么都没有发生.我无法知道是否发送了第一条消息(将脚本扩展到扩展名).我在做什么错了?

Nothing happens at all. i have no way of knowing whether hte first message is sent(cotnent script to extension). What am I doing wrong?

推荐答案

代码应该是worker.port.emit和worker.port.on而不是worker.on和worker.emit

The code should have been worker.port.emit and worker.port.on instead of worker.on and worker.emit

pageMod.PageMod({
  include: ["https://play.google.com/*"],
  contentScriptWhen: 'ready',
  contentScriptFile: [data.url("jquery.js"),data.url("jquery.knob.js"),data.url("purl.js"),data.url("content.js")],
  contentScriptOptions: {
    inUrl: data.url("in.png"),
    outUrl: data.url("out.png"),
    logoUrl: data.url("logoimage.png")
  },
  contentStyleFile: [data.url("css/inject.css")],
  onAttach: function(worker) {//Ttach
    alert("hello there")
    worker.port.on("message",function(){
//THIS IS THE CORRECT CODE
        worker.port.emit("alert",{message:"Hello"});
    })

  }
});

这篇关于Firefox port.emit和port.on无法在扩展中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆