- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写一个简单的用户脚本,如果它包含特定的单词列表,它将自动隐藏 Facebook 帖子。核心功能有效,但我的 MutationObserver
似乎没有正确读取 mutation.addedNodes
的 className
。我遍历 mutation.addedNodes
并检查这些元素中是否有任何元素具有 userContentWrapper
类,但该测试的结果始终为 false——即使该元素确实具有类(class)。
var startObserver = function() {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var added = mutation.addedNodes;
for (var i = 0; i < added.length; i++) {
if (/\buserContentWrapper\b/.test(added[i].className)) {
processFilter(added[i]);
}
}
});
});
var obj = {childList: true, subtree: true, attributes: true};
observer.observe(document.documentElement, obj);
};
我只能假设观察者正在分析添加的节点,然后才完全形成所有属性。我怎样才能让观察者等待处理节点直到它完全完成?还是我不明白问题所在?
提前致谢...
最佳答案
一些添加的节点是容器,所以你应该检查它们的内部:
const observer = new MutationObserver(onMutation);
observer.observe(document, {
childList: true,
subtree: true,
});
function onMutation(mutations) {
const found = [];
for (const { addedNodes } of mutations) {
for (const node of addedNodes) {
if (!node.tagName) continue; // not an element
if (node.classList.contains('userContentWrapper')) {
found.push(node);
} else if (node.firstElementChild) {
found.push(...node.getElementsByClassName('userContentWrapper'));
}
}
}
found.forEach(processFilter);
}
MutationObserver 回调作为一个微任务执行,它会阻塞 DOM 和 JS 引擎,因此请尽量使其变快,尤其是当它运行在一个复杂的网站上时,例如生成大量 DOM 突变的 facebook。这可以在开发工具(F12 键)分析器/时间线面板中进行测试。
关于javascript - 获取添加的 DOM 节点的类名 (mutationObserver),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37979897/
我不是很熟悉 JavaScript 和垃圾回收的内部结构。我想避免在使用 MutationObserver 时可能产生内存问题的循环引用,并且我知道如果 GC 假设错误,我可以使 MutationOb
fiddle :https://jsfiddle.net/7gj26hqu/ 我想要一个MutationObserver可以检测自身内部的所有新节点。在示例中,我设置了 {childList: tru
我正在尝试检测元素中的 CSS 属性更改。我在网上搜索发现MutationObserver javascript API。但在我的测试脚本中,它没有按预期工作(它没有提醒属性名称和属性值)。 var
所以我正在尝试更改一些 JavaScript 从此 content.forumobserver = content.build.forumobserver = function() { var
我正在尝试在我的插件中使用 MutationObserver。因此,我注入(inject)一个内容脚本,然后设置观察者。这似乎有效,而且检测到的突变似乎无法序列化为 JSON。但实际上我想使用this
在下面的代码中,通过重复单击 2 个按钮,观察者 仅在每个观察到的目标上的第一个更改时触发? (输出显示更改。) 如何让它在每次更改时都生效? var cc = 1; document.querySe
我正在使用 MutationObserver检测何时将特定类添加到元素。 const observer = new MutationObserver((mutations) => { muta
我试图从页面中获取特定的已删除元素属性,该属性可以有 0 - n 个副本并且可以动态添加。我开始使用 DOMNodeRemoved 事件,最初了解到该事件已被弃用,然后转向 MutationObser
我正在尝试使用 Greasemonkey 为 Facebook 的时间线日志页面制作一个脚本。 我需要捕获两种事件: 1) URL 更改(由于更改是通过 Facebook 上的 AJAX 进行的,因此
以下代码允许我在单个节点上监听类更改: var target = $(".right-border")[0] var observer = new MutationObserver(function(
我想同步一些DOM节点属性。如果更改某个属性,则会更改其他元素属性。 我可以更改它,但我无法为其编写测试。该测试更改观察到的元素的属性,然后检查更改是否应用于其他元素。更改同步最终会发生,但不会在观察
对我来说,这个 API 中最令人困惑的是为什么使用它。我了解 ReactJS 和 RxJS,并且我已经习惯了 View 对数据更改使用react的概念。因此,观察 DOM 的变化(这肯定是在数据发生一
是否可以通过mutationObserver观察节点的innerHTML?我正在尝试使用 characterData: true,但它不会对内容更改进行任何回调。 完整选项列表: childList:
我试图等到 Mutation 事件开始覆盖它刚收到的值。在我尝试正常应用它之前,它一直被我无法控制的第 3 方提供的其他值覆盖。 /** Intercept changes in select2-dr
我打算使用 MutationObserver 来观察元素值的外观和变化,但老实说我不确定应该如何实现。 MO 的目标是 div.player-bar 而我想要完成的是检测 el-badge__cont
我在 chrome 扩展中有以下代码: var observer = new MutationObserver(function (mutations) { mutations.forEach(fun
我正在尝试使用 MutationObserver 来观察 body 元素中样式属性的变化,并将 body 样式属性的溢出属性更改为滚动。当我缩小浏览器窗口时,样式属性会发生变化。Firefox 最终崩
我有一个第 3 方脚本,可以在我的页面上加载一个图片库,其中包含来自场外的图像。 我的页面开始时是空的: 然后第 3 方脚本会添加其他内容(例如照片库的框架):
我正在使用 MutationObserver 来保存可拖动对象中的位置变化。 看起来像这样: let observer = new MutationObserver( (mutations) =>
以下脚本有效,但 chrome 显示有错误.. 我应该怎么做才能解决这个错误? function Do(){alert("test");} new MutationObserver(Do).obser
我是一名优秀的程序员,十分优秀!