如何从 Firefox 扩展中注入的 HTML 按钮触发事件?
我的最终目标是创建一个 Firefox 扩展,将 HTML 按钮插入到网站上,并让它触发在我的扩展中的自定义代码模块中定义的函数。
我尝试实现 Nickolay 对此的回答 问题。当我单击我创建的按钮时,我在 Firebug 中收到以下错误:
“未捕获的异常:[异常...”组件返回失败代码:0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMEventTarget.dispatchEvent]”nsresult:“0x80070057 (NS_ERROR_ILLEGAL_VALUE)”位置: "JS框架::"
我的代码:
onPageLoad: function(aEvent) {
Components.utils.import("chrome://my_ext/content/writeFile.jsm", my_ext);
//alert(foo()); - foo() is an function in the above code module I import
var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
var event = doc.createEvent("Events");
event.initEvent("my-custom-event", true, true);
var fblike = doc.getElementById("LikePluginPagelet");
var button = doc.createElement("input");
button.setAttribute("type", "button");
button.setAttribute("value", "My Button");
button.setAttribute('onclick', 'dispatchEvent(event)');
fblike.appendChild(button, fblike);
var docx = event.originalTarget;
if(docx && docx.addEventListener)
docx.addEventListener("my-custom-event", function() {alert(foo()); }, false);
},
我的writeFile.jsm:
var EXPORTED_SYMBOLS = ["foo"];
function foo() {
return "foo test";
}
知道如何解决这个问题吗?我还应该提到,我对开发浏览器扩展完全陌生。
My end goal is to create a firefox extension that inserts an HTML button onto a site and have it fire a function defined in a custom code module in my extension.
I tried to implement Nickolay's answer to this question. When I click the button I created though I get the following error in Firebug:
"uncaught exception: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMEventTarget.dispatchEvent]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: "
My code:
onPageLoad: function(aEvent) {
Components.utils.import("chrome://my_ext/content/writeFile.jsm", my_ext);
//alert(foo()); - foo() is an function in the above code module I import
var doc = aEvent.originalTarget; // doc is document that triggered "onload" event
var event = doc.createEvent("Events");
event.initEvent("my-custom-event", true, true);
var fblike = doc.getElementById("LikePluginPagelet");
var button = doc.createElement("input");
button.setAttribute("type", "button");
button.setAttribute("value", "My Button");
button.setAttribute('onclick', 'dispatchEvent(event)');
fblike.appendChild(button, fblike);
var docx = event.originalTarget;
if(docx && docx.addEventListener)
docx.addEventListener("my-custom-event", function() {alert(foo()); }, false);
},
My writeFile.jsm:
var EXPORTED_SYMBOLS = ["foo"];
function foo() {
return "foo test";
}
Any idea on how I can fix this? I should also mention that I am completely new to developing browser extensions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
button.setAttribute('onclick', 'dispatchEvent(event)')
不会执行您认为会执行的操作 - 单击按钮时,它会获取click
事件,并且尝试发送它。但是,不允许第二次调度事件。您似乎想要的是这样的:这将创建一个函数闭包,该函数闭包将有权访问周围代码的变量 - 包括
button
和event
。关于闭包的进一步阅读:http://www.javascriptkit.com/javatutors/closures.shtmlbutton.setAttribute('onclick', 'dispatchEvent(event)')
will not do what you think it will do - when the button is clicked it takes theclick
event and tries to dispatch it. Dispatching an event a second time isn't allowed however. What you seem to want is this:This creates a function closure that will have access to the variable of the surrounding code - including
button
andevent
. Further reading on closures: http://www.javascriptkit.com/javatutors/closures.shtml终于开始工作了。我不需要添加第二个监听器,只需要一行:
Button.addEventListener("点击", foo, false);
我之前尝试过 foo() 而不仅仅是 foo ,但失败了,但现在工作得很好。
Finally got it to work. I did not need to add the second listener and it only took one line:
button.addEventListener("click", foo, false);
I had previously tried foo() instead of just foo and that failed but works just fine now.