vmo*_*eco 0 javascript firefox greasemonkey mutation-observers
我正在尝试制作Greasemonkey脚本以在Facebook页面中添加按钮.但我需要等待创建一个元素来添加按钮.
我正在尝试使用MutationObserver(如此处所示)来执行此操作,但存在错误.
function init() {
console.log('Launched : ' + launched);
if (!launched) {
launched = true;
main();
}
}
console.log('barfoo');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (!mutation.addedNodes) return;
console.log(mutation.addedNodes.type());
for (var i = 0; i < mutation.addedNodes.length; i++) {
if (mutation.addedNodes[i].matches('div[class="pam _5shk uiBoxWhite bottomborder"]')) {
console.log('init');
init();
}
}
})
});
console.log('bar');
try {
observer.observe(document.body, {
childlist: true,
subtree: true,
attributes: false,
characterData: false,
});
} catch (error) {
console.log(error);
}
console.log('foobar');
Run Code Online (Sandbox Code Playgroud)
但是我得到了以下日志:
barfoo
bar
DOMException [TypeError: "The expression cannot be converted to return the specified type."
code: 0
nsresult: 0x805b0034
location: file:///home/vmonteco/.mozilla/firefox/f3xgmo8e.default/gm_scripts/Facebook_cleaner/Facebook_cleaner.user.js:230] Facebook_cleaner.user.js:239:3
foobar
Run Code Online (Sandbox Code Playgroud)
这个错误的原因是什么?我怎么能解决这个问题?
如果在执行我的脚本之前创建了元素,那么MutationObserver仍然会被触发吗?(IE,如果我创建MutationObserver时元素已经存在).
注意:我试图使用$(document).ready(),但它是在创建我要添加按钮的元素之前触发的.
NB-bis:我没有发布整个剧本.
不幸的是,该错误信息使用完全模糊,但你有一个错字:
observer.observe(document.body, {
childlist: true,
subtree: true,
attributes: false,
characterData: false,
});
Run Code Online (Sandbox Code Playgroud)
应该
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false,
});
Run Code Online (Sandbox Code Playgroud)
例如childlist=>childList
在这种情况下,在Chrome中运行相同的代码会提供比"无法转换为返回指定类型"更有用的消息.它说
options对象必须将'attributes','characterData'或'childList'中的至少一个设置为true.
| 归档时间: |
|
| 查看次数: |
4766 次 |
| 最近记录: |