Add the subTree parameter, it should work, you want to control not only the children of the document ( head/body ), but also the descendants. (And that is the reason when it is set to document.body , it works).
observer.observe(document, { attributes: true, childList: true, characterData: true, subtree:true });
Fiddle
From the documentation
subtree: set to true if mutations are not intended for the target, but target descendants must also be specified.
So what you add is a descendant of document , not its descendant (or direct descendant). This is a child of the body (and therefore just mentioning childList and using document.body works). You must specify subTree if you want to deeply track changes.
Also note the note:
NOTE. At the very least, childList, attributes, or characterData must be set to true. Otherwise, "An invalid or invalid string was specified" an error is thrown.
PSL
source share