Mutation Observer 不适用于受支持的 Microsoft Edge 版本
Mutation Observer not working on a supported Microsoft Edge Version
我有以下使用 mutationobserver 的代码,它可以在浏览器 chrome、firefox、opera 中运行,但不能在浏览器边缘运行。
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
if(mutation.target.innerHTML == 'Online - Chat Us'){
//if button text changes from "Offline" to "Online - Chat Us"
if(mutation.removedNodes[0].nodeValue === 'Offline' && mutation.addedNodes[0].nodeValue === 'Online - Chat Us'){
//change top button to "Online - Chat Us" && false the disabled attribute.
document.getElementById('mySecondButton').innerHTML = mutation.addedNodes[0].nodeValue;
document.getElementById('mySecondButton').disabled = false;
}
}
}else if(mutation.type === 'characterData'){
//console.log(mutation);
}
});
});
仅在调试器的边缘浏览器中,我收到以下错误:SCRIPT5007:SCRIPT5007:无法获取未定义或空引用的 属性 'nodeValue' 引用了这行代码 if(mutation.target.innerHTML === 'Chat Now'){
从下面的图表中我可以看出我使用的是受支持的版本。
edge浏览器版本截图
在Edge中,innerHTML、innerText、textContent的变化导致的不是一个而是两个突变记录:一个是旧节点的移除,一个是新节点的插入。所有其他浏览器只有一个变异记录,其中包含一个删除的节点和一个插入的节点。我为脚本提供了一些空检查以将突变记录缩小到只有一个,这很好。
我有以下使用 mutationobserver 的代码,它可以在浏览器 chrome、firefox、opera 中运行,但不能在浏览器边缘运行。
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
if(mutation.target.innerHTML == 'Online - Chat Us'){
//if button text changes from "Offline" to "Online - Chat Us"
if(mutation.removedNodes[0].nodeValue === 'Offline' && mutation.addedNodes[0].nodeValue === 'Online - Chat Us'){
//change top button to "Online - Chat Us" && false the disabled attribute.
document.getElementById('mySecondButton').innerHTML = mutation.addedNodes[0].nodeValue;
document.getElementById('mySecondButton').disabled = false;
}
}
}else if(mutation.type === 'characterData'){
//console.log(mutation);
}
});
});
仅在调试器的边缘浏览器中,我收到以下错误:SCRIPT5007:SCRIPT5007:无法获取未定义或空引用的 属性 'nodeValue' 引用了这行代码 if(mutation.target.innerHTML === 'Chat Now'){
从下面的图表中我可以看出我使用的是受支持的版本。
edge浏览器版本截图
在Edge中,innerHTML、innerText、textContent的变化导致的不是一个而是两个突变记录:一个是旧节点的移除,一个是新节点的插入。所有其他浏览器只有一个变异记录,其中包含一个删除的节点和一个插入的节点。我为脚本提供了一些空检查以将突变记录缩小到只有一个,这很好。