Re: [whatwg] MutationObserver Spec for AddedNodes Timing & network requests

On 7/20/14, 5:21 PM, milakam wrote:
> A BeforeLoad replacement was never discussed as a target use case for
> MutationObservers, therefore this message.

MutationObservers happen when the DOM is modified.

Loads geneally speaking start off the preload scanner, before the DOM is 
even constructed.

So you can't really use a MutationObserver to prevent loads, as you 
discovered.

Obviously some UAs in some circumstances may delay some loads to after 
the mutation observer is fired.  That shouldn't be relied on.

> I think this is great solution
> and it would be fantastic if it can be standardized.

I think it would be terrible if registering a MutationObserver is forced 
to disable the preload scan (especially because you never know whether 
the MutationObserver will get registered at all until you start running 
scripts, at which point your preload scan is already going along), so I 
disagree with this being a great solution.

> 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

Uh...  That would be a clear spec violation.  Toggling the "src" of a 
<script> _after_ it has been inserted into a document (which is when 
that MutationObserver fires) is a no-op per spec.

That said, I can't reproduce the behavior you claim. 
http://jsfiddle.net/59Sxf/ alerts "evil" for me in Chrome.

> 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).

Right, as the spec clearly requires.

-Boris

Received on Monday, 21 July 2014 03:13:58 UTC