[我有以下代码利用了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中,innerHTML,innerText和textContent的更改不但会导致两个突变记录:一个会导致删除旧节点,而一个会导致插入新节点。所有其他浏览器只有一个变异记录,其中包含一个已删除节点和一个插入节点。我为脚本提供了一些空检查,以将突变记录的范围缩小到一个,这很不错。