3

I have been using an open source library called tree-mirror.js which uses mutation-summary.js to do the DOM mirroring.

Everything is good but iframes. When the main document contains an iframe, the dom changes to the document with in the iframe are not captured by these libraries. What I could figure out from reading the code of tree-mirror.js is that it binds mutation observer to the main document but did not understand if this can handle the iframe documents also automatically.

I am not sure if this is not supported by the libraries or I am missing something. Did anyone work with these libraries and encountered this issue? Kindly help.

4

1 回答 1

-1

编辑:感谢 wOxxOm 在上一个答案中指出错误的结论。

简而言之,MutationObserver API 似乎不支持观察 iframe 内部的变化。考虑以下示例:

var m = new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    console.log(mutation) 
  });
});

// We observe child list and subtree changes
m.observe(document.body, {childList: true, subtree: true});

// Let's create an iframe
var iframe = document.createElement("iframe");

// A mutation record will be logged here
document.body.appendChild(iframe);

// However this will NOT log a mutation record
iframe.contentDocument.body.appendChild(document.createElement("div"));

所以我担心你在这里不走运。就像上面评论中所说的那样,您必须在每个 iframe 中初始化一个新的变异观察者才能观察它们的 DOM 变化。没有提到的库支持这一点,因为它们主要是原生 MutationObserver API 的包装器。

于 2017-03-13T08:11:52.597 回答