I created a few months ago Firefox add-on that recently failed. The add-in basically searches for a specific URL and then modifies the DOM for the page. I followed up on the (random) installation of the AVG Safe Search add-in. I found that with the AVG function disabled, the DOMContentLoaded event is fired once for a document (the behavior that I originally expected), but when it is turned on, the DOMContentLoaded event is fired twice for a document. My add-on inserts a column in an HTML table, so since the event fires twice, two repeating columns are inserted instead of one.
Here is the initialization code for my add-on:
var hLoadListener = function(event) { myAddon.initialize(event); } var hContentLoadedListener = function(event) { myAddon.onContentLoaded(event); } myAddon.initialize = function(aEvent) { gBrowser.addEventListener("DOMContentLoaded", hContentLoadedListener, false); }; myAddon.onContentLoaded = function(aEvent) { if (!(aEvent.originalTarget.nodeName === "#document")) { return; } var doc = aEvent.target; // document that triggered "onload" event if (!(doc instanceof HTMLDocument)) { return; } if (!doc.location) { return; } var href = doc.location.href; // URL of current page if (URLRegExp.test(href)) { // Modify the page DOM } }; window.addEventListener("load", hLoadListener, false);
This problem seems easy to fix by inserting a unique DOM element and then checking its existence at the beginning. My question is, should add-in developers expect this event behavior to be normal, or is this problem mainly due to a bug / side effect in the AVG add-in?
javascript dom event-handling firefox-addon
Jeremy giron
source share