I authored a Firefox add-on several months ago that recently failed. The add-on basically looks for a particular URL and then modifies the DOM for the page. I traced the failure to the (accidental) installation of the "AVG Safe Search" add-on. I found that, with the AVG add-on disabled, the DOMContentLoaded event fires once for the document (behavior I originally expected), but with it enabled, the DOMContentLoaded event fires twice for the document. My add-on inserts a column into an HTML table, so because the event fires twice, two duplicate columns are inserted rather than one.
Here's the distilled initialization code of 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's DOM
}
};
window.addEventListener("load", hLoadListener, false);
This issue seems easy to fix by inserting a unique DOM element and then testing for it's existence at the start. My question is whether add-on developers should expect this event behavior as normal or whether this issue is primarily a bug/side-effect in the AVG add-on?