与“加载”有关的问题Firefox扩展开发中的事件处理程序 [英] Issues with "load" event handler in Firefox Extension Developement

查看:226
本文介绍了与“加载”有关的问题Firefox扩展开发中的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个firefox扩展名,但是由于正确地获取了正确的事件,所以有与用户交互的问题。我的分机应完成以下任务:

I'm writing a firefox extension, but have issues with user interaction because of getting the right events properly. My extension shall complete the following tasks:


  • 查看某些结构上当前浏览的浏览器标签

  • 执行一些后端服务器调用

  • 打开对话框

  • 将用户重定向到目标页面

  • check the currently viewed browser tab on certain structures
  • do some backend server calls
  • open dialog
  • redirecting the user to a landing page

到目前为止这么好,它的作品。我使用以下eventHandler启动序列:

So far so good, it works. I start the sequence with the following eventHandler:

window.gBrowser.selectedTab.addEventListener("load",function(){ Fabogore.Load();},true);

打开对话框后,我尝试从对话框中删除EventHandler:

after opening the dialog, I try to remove the EventHandler from within the Dialog:

window.opener.gBrowser.selectedTab.removeEventListener("load",function(){Fabogore.Load();},true);

但是序列被一次又一次地触发,事件监听器会获取每个标签的每个加载事件,尽管我使用了selectedTab?所以对话框一次又一次地弹出来。我也尝试在原始Javascript中关闭事件处理程序。

But the sequence gets triggered again and again, the eventlistener fetches every load event of every single tab, although i used the selectedTab? So the Dialog pops up again and again. I've also tried closing the event Handler in the original Javascript.

任何猜测?感谢提前:)

Any guesses? Thanks in advance :)

推荐答案

这很容易解释 - 您添加为监听器的功能,您删除的功能不同。尝试运行此代码:

This is easily explained - the function you add as a listener and the function you remove are different. Try running this code:

alert(function(){ Fabogore.Load();} == function(){ Fabogore.Load();});

每次都会显示 false 您在代码中定义了一个功能,创建一个新的功能。要解决您的问题,您需要定义一个函数,存储对它的引用,并将其用于添加和删除侦听器:

This will show you false, each time you define a function in your code a new function is created. To solve your problem you need to define one function, store the reference to it and use it both to add and remove the listener:

var listener = function(){ Fabogore.Load();};
var listenerTab = window.gBrowser.selectedTab;
listenerTab.addEventListener("load", listener, true);
[...]
listenerTab.removeEventListener("load", listener, true);

请注意,我还存储了 window.gBrowser.selectedTab 在变量中 - 当你决定删除你的侦听器时,所选的选项卡可能已经改变了。您要从您添加的标签中删除该监听器,而不是其他选项卡。

Note that I also stored the value of window.gBrowser.selectedTab in a variable - by the time you decide to remove your listener the selected tab might change already. You want to remove the listener from the tab you added it on and not some other tab.

这篇关于与“加载”有关的问题Firefox扩展开发中的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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