所以,假设我正在遍历 DOM 并隐藏<img>
页面上包含特定 ID/Class 字符串的所有元素,例如
var strings = ['[id*="example"]','[class*="example"]'];
...
var MyObject = {
"getAll": Array.prototype.slice.call(document.querySelectorAll(strings.join())),
...
在 Facebook 等页面上,内容是动态加载的,因此不会自动隐藏新图像。
我正在尝试使用 aMutationObserver
检查何时有任何与此数组匹配的新元素,但存在问题。
var observe = new MutationObserver(function(mutations){
mutations.forEach(function(mutation) {
// hide all these newly found imgs somehow
});
});
var config = { attributes: true, childList: true, characterData: true };
observe.observe(MyObject.getAll, config);
observe.disconnect();
出于某种原因,我收到错误:
notfounderror: failed to execute 'observe' on 'mutationobserver': the provided node was null.
即使MyObject.getAll
是一个内部有大量节点的数组?
基本上,我想documentSelectorAll('[id*="example"],[class*="example"]')
通过 MutationObserver 检查是否有任何新加载/可见的元素与我的数组匹配,然后我可以通过隐藏它等来采取适当的行动。