I'm one of the developers of TryAgain, a Firefox add-on, that displays a custom error page when a website fails to load. It essentially replaces Firefox's netError.xhtml with a customized version.
In order to execute JavaScript from the extension code within the netError.xhtml, I've added a XUL <command> element to the error page as follows:
var increment_btn = doc.
createElementNS(
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
"xul:command"
);
increment_btn.setAttribute("id", "errorIncrement");
increment_btn.setAttribute("oncommand", "autoRetryThis();");
div.appendChild(increment_btn);
Whenever autoRetryThis() needs to be executed, I simply run doCommand() on the element. On my testing machine, this works fine under Firefox 2.0 through 4.0b10. I've received the following problem report, however:
Error: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMHTMLDocument.createElementNS]
The error points to the first line of the above code.
If you're wondering why I chose to create the element using createElementNS(), please refer to my previous question.
Remove the xul: from the name parameter of createElementNS.
Using the XUL namespace has been deprecated. To my understanding, this means that it is no longer possible to place XUL and XHTML controls within a single XML document.
My solution was to instead use a normal XHTML <button> tag, and call it's onclick() listener by dispatching the event:
try {
var evt = doc.createEvent('HTMLEvents');
evt.initEvent('click', false, false);
btn.dispatchEvent(evt);
} catch (e) {
btn.click();
}
Why not just do btn.click() in the first place? I discovered some incompatibilities between using this method and add-ons that monitored clicking on the document. Dispatching the event directly circumvents this.
User contributions licensed under CC BY-SA 3.0