DOMContentLoaded event fires twice to load one page

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?

+8
javascript dom event-handling firefox-addon
source share
2 answers

I don’t know if I will consider this β€œnormal”, however the possibilities for external applications to influence the operation of your plugin are endless.

However, I think that no matter what the AVG causes this anomaly, a reasonable thing, as you said, is to check if the column exists before insertion, since AVG may not be the only external application that affects firefox event triggers.

I am very tired of the events related to the DOM, because in my own plugin, and testing it throughout the development showed PLENTY anomalies based on so many variables (different OS, different versions of FF, different applications on the host computer, different plugins within any FF users, etc.)

Summarizing:

  • Error in AVG? Maybe.
  • Is the potential for your plugin performance to impact LOTS of other sources? Absolutely!
  • Solution : IMHO- Always verify that a change has been made before making the actual change for all DOM elements just to be safe.
+3
source share

Try reading the jQuery bindReady method:

https://github.com/jquery/jquery/blob/master/src/core.js

You will find:

 if ( readyBound ) { return; } readyBound = true; // Use the handy event callback document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); // A fallback to window.onload, that will always work window.addEventListener( "load", jQuery.ready, false ); 
+1
source share

Source: https://habr.com/ru/post/649834/


All Articles