[whatwg] MutationObserver Spec for AddedNodes Timing & network requests

Hi everyone,

A BeforeLoad replacement was never discussed as a target use case for
MutationObservers, therefore this message.

I'm currently creating a JS cross-browser user script and noticed that
only Chromium notifies the MutationObserver for an AddedNode before a
network request is sent (IE and FF don't and won't do anything with the
changes).

I found the following chromium discussion
https://code.google.com/p/chromium/issues/detail?id=333318 which states
the MO as a replacement for BeforeLoad. I think this is great solution
and it would be fantastic if it can be standardized.

My own usecase / testcase I created:
<script type="text/javascript">
MutationObserver = window.MutationObserver;
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
  	var addedNodes = mutation.addedNodes;
	for (var i = 0; i < addedNodes.length; i++) {
		if (addedNodes[i].nodeName == 'SCRIPT') {
			addedNodes[i].src = 'good.js';
		}
	}
	
  });
});
observer.observe(document, {childList: true, subtree: true});
</script>
<script type="text/javascript" src="evil.js"></script>

In chrome this works out of the box and "good.js" is called directly
(without sending any network requests beforehand, if it would be stored
externally). In FF only "evil.js" is called. The source is updated
within DOM afterwards, but there's no network request or parsing of the
updated script (apart from the DOM change it has no effect).
In FF it's currently only possible with an internal extension like (e.g.
usecase
http://stackoverflow.com/questions/5330048/event-before-load-event-for-firefox-extension):
Components.classes["@mozilla.org/observer-service;1"]
  .getService(Components.interfaces.nsIObserverService)
  .addObserver({
    observe: function(aSubject, aTopic, aData) {
      if ("http-on-modify-request" == aTopic) {
        var url = aSubject
          .QueryInterface(Components.interfaces.nsIHttpChannel)
          .originalURI.spec;
        if (url && url.match('facebook')) {
          aSubject.cancel(Components.results.NS_BINDING_SUCCEEDED);
        }
    }
  }
}, "http-on-modify-request", false);

 I didn't test it in in Konqueror.

Greetz
MM

Received on Sunday, 20 July 2014 21:21:44 UTC