【发布时间】:2011-07-07 22:29:32
【问题描述】:
我的最终目标是创建一个 firefox 扩展,将 HTML 按钮插入网站并让它触发在我的扩展的自定义代码模块中定义的函数。
我试图实现 Nickolay 对此question 的回答。当我单击我创建的按钮时,虽然我在 Firebug 中收到以下错误:
“未捕获的异常:[Exception...”组件返回失败代码:0x80070057(NS_ERROR_ILLEGAL_VALUE)[nsIDOMEventTarget.dispatchEvent]”nsresult:“0x80070057(NS_ERROR_ILLEGAL_VALUE)”位置:“JS frame ::”
我的代码:
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";
}
知道如何解决这个问题吗?我还应该提到,我对开发浏览器扩展完全陌生。
【问题讨论】:
标签: javascript firefox firefox-addon mozilla