I am a Firefox C++ extension newbie. I need to get access to DOM mutation events in my extension during page load. Firefox by default doesn’t send DOM mutation events during page load to improve page load performance.
I understand the reason, but understanding the consequences I still need access to the DOM mutation events. I read somewhere that nsIMutationObserver still gets invoked during page load (and is bit more efficient then DOM mutation events as don’t have to walk up the DOM tree looking for listeners), however it’s only available to native code.
So I have following questions :
- Is nsIMutationObserver and nsIMutationObserver2 available to Firefox extensions?
- If yes, how can I write a simple Firefox extension in C++ to get access to it and expose it to Javascript?
- If folks can point me to a existing extension that does this forwarding from C++ land to JS, that will be highly appreciated.
- Or can I use JS-CTypes to get access to that functionality from my Javascript based extension?
BTW, I asked this question in Firefox’s extension forum, but no replies there.
Thanks in advance
Yes, binary Firefox extensions can use it. Of course, the drawback is that your binary XPCOM component will only work with one Firefox release – it will have to be recompiled for each new release.
You create an XPCOM component (see example code) and implement
nsIMutationObserverinterface. You then attach this mutation observer to documents like this:For reference: nsINode interface
Sorry, don’t know any. But your XPCOM component can expose an additional interface that your JavaScript code will use – e.g. to register a callback. You have to consider that it might not be safe to run JavaScript when the mutation observer gets called. Important methods here:
nsContentUtils::IsSafeToRunScript()andnsContentUtils::AddScriptRunner()(see nsContentUtils.h).No, you cannot. These are Gecko internals, they aren’t exposed to js-ctypes.