为什么更改contentUrl后,附加面板的内容脚本不再响应端口消息? [英] Why is my add-on panel´s content script no longer responding to port messages after changing contentUrl?

查看:53
本文介绍了为什么更改contentUrl后,附加面板的内容脚本不再响应端口消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用addon-sdk构建Firefox插件.

I am building a Firefox add-on using the addon-sdk.

附加组件具有一个按钮,可在单击时显示一个面板.该面板有一个运行它的内容脚本.现在,我需要根据用户当前的选项卡使面板看起来有所不同,并偶尔显示一个外部URL.

The add-on has one button that displays a panel on click. The panel has a content script running it. Now, I need the panel to look different depending on the user's current tab and occasionally show an external url.

因此,main.js脚本跟踪用户的当前选项卡,并使用panel.port.emit()将消息发送到内容脚本,并且内容脚本更改面板的HTML以匹配所需的内容.

So, the main.js script tracks the current tab of the user and sends messages to the content script using panel.port.emit() and the content script changes the HTML of the panel to match whatever is needed.

但是,正如我提到的,有时候面板只是显示一个外部URL,所以我需要将其重置为原始URL.这是我的代码:

However, as I mentioned, some times the panel is just showing an external url, so I need to reset it back to its original url. This is my code:

function panelListMode(currentTab, data){

  panel.resize(350,300);

  //This is the line causing the trouble
  panel.contentURL = self.data.url("panel.html");

  console.log("sending message");
  panel.port.emit("generateList",data);
}

如果我在更改contentURL之后发出消息,则内容脚本似乎没有收到任何东西.现在,我说那一行是引起问题的那一行,因为如果我评论它,那么内容脚本会收到消息.

If I emit the message after changing the contentURL the content script does not seem to receive a thing. I now that specific line is the one causing the problem because if I comment it the content script receives the message.

某件事告诉我这是因为面板需要再次加载DOM,而我需要等到它准备好之后,内容脚本才能执行任何操作.但是...内容脚本不是只包含脚本吗?

Something tells me this is because the panel needs to load the DOM again and I need to wait until it is ready before the content script is able to do anything. But... aren't content scripts something apart from just included scripts?

据我所知,面板没有任何就绪"事件可供我发出消息.

As far as I am able to tell the panel has no "ready" event of sorts that I can listen to for emitting the message.

我想念什么?

编辑:经过一些实验,我能够找到更多内容.如果我将panel.html复制到panel2.html所在的目录并更改函数,就可以了:

After some experimentation I have been able to find some more. If I copy panel.html to the same directory as panel2.html and change my function so it goes:

function panelListMode(currentTab, data){

  panel.resize(350,300);

  //This is the line causing the trouble
  panel.contentURL = self.data.url("panel2.html");

  console.log("sending message");
  panel.port.emit("generateList",data);
}

问题消失了.我做了一些实验,似乎当我尝试将contentURL更改为面板中已设置的html时,内容脚本会停止一起工作.

The problem is gone. I have experimented a bit and it seems that when I try to change the contentURL to the html that is already set in the panel the content script stops working all together.

这是一个非常奇怪的行为...我遇到错误了吗?

This is some really odd behavior... have I encountered a bug?

推荐答案

据我所知,面板没有任何就绪"事件可供我听取,以发出消息.

As far as I am able to tell the panel has no "ready" event of sorts that I can listen to for emitting the message.

我认为确实如此,至少在内部看来如此,但我不确定它们是否被暴露.

I think it does, at least internally it seems to but I'm not sure whether they're exposed.

但是,即使不是这样,您也可以在内容脚本附加并注册自己的侦听器后,从内容脚本中简单地触发您自己的端口消息.在插件中收到该消息后,您可以继续使用该侧上的端口.

But even if it doesn't you can simply fire your own port message from the content script once it has been attached and registered its own listeners. Upon receiving that message in the addon you can then resume using the port on that side.

这篇关于为什么更改contentUrl后,附加面板的内容脚本不再响应端口消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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