提问人:DonMarc0 提问时间:9/12/2023 更新时间:9/12/2023 访问量:16
JS - 监听消息的 EventListener 的等效 Mutation Observer 是什么?
JS - What is the equivalent Mutation Observer of an EventListener that listens to messages?
问:
我目前正在我的 Web 应用程序中实现 iframe。此 iFrame 是一个所见即所得的编辑器,应根据其中的内容自动调整大小。稍后,我还需要将内容从编辑器传达给我的父应用程序。一段时间以来,我使用EventListeners来监听“message”事件,如下所示:
window.addEventListener("message", doSomething(event);
我的 iFrame 中的代码如下所示:
window.top.postMesssage("a message lol", "https://example.com")
最近控制台告诉我 EventListeners 已弃用,我应该改用 Mutation Observers (MO)。我应该在我的 MO 中使用哪个选项来侦听消息?
我找到了一个选项列表,但它们似乎不符合目的。
let options = {
childList: true,
attributes: true,
characterData: false,
subtree: false,
attributeFilter: ['one', 'two'],
attributeOldValue: false,
characterDataOldValue: false
};
这里 tiptapeditor 是 iFrame 节点,我希望在编辑器中输入时至少得到任何形式的响应。
const targetNode = document.getElementById('tiptapeditor')
const observerConfig = { attributes: true, childList: true, characterData: true };
const callback = (mutations) => {
for (const mutation of mutations) {
if (mutation.type == "childList") {
console.log("Child mutation detected")
}
if (mutation.type == "chatacterData") {
console.log("Character mutation was detected")
}
if (mutation.type === "attributes") {
console.log(`The ${mutation.attributeName} attribute was modified.`);
}
}
}
let observer = new MutationObserver(callback)
observer.observe(targetNode, observerConfig)
答: 暂无答案
评论