admin管理员组文章数量:1433496
I am a total noob at JS (as well as with the web development in general) :(
Currently, I am working on a small script for Tampermonkey, which looks after specific elements in a particular class and plays a sound whenever any changes occur with the element. (Value changes, elements hides/shows, etc.).
Currently, I have the following code:
var audio = new Audio('URL_to_the_sound');
var mutationObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation);
audio.play();
});
});
var target = document.getElementsByClassName('CLASSNAME');
mutationObserver.observe(target, {
attributes: true,
characterData: true,
childList: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true
});
However, my browser responds with the following error:
Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.
I should mention that there are several elements in the same class.
What should I do? :(
I am a total noob at JS (as well as with the web development in general) :(
Currently, I am working on a small script for Tampermonkey, which looks after specific elements in a particular class and plays a sound whenever any changes occur with the element. (Value changes, elements hides/shows, etc.).
Currently, I have the following code:
var audio = new Audio('URL_to_the_sound');
var mutationObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation);
audio.play();
});
});
var target = document.getElementsByClassName('CLASSNAME');
mutationObserver.observe(target, {
attributes: true,
characterData: true,
childList: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true
});
However, my browser responds with the following error:
Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.
I should mention that there are several elements in the same class.
What should I do? :(
Share Improve this question asked Mar 20, 2020 at 15:15 Rudy GrinbergRudy Grinberg 432 silver badges9 bronze badges 7-
2
getElementsByClassName
returns a NodeList, not a single Node. You need to loop through it and create an observer for each element – Rory McCrossan Commented Mar 20, 2020 at 15:17 - @RoryMcCrossan Can you provide me with the example of the code? I have tried adding a loop function; however, the web-page is not loading with it. – Rudy Grinberg Commented Mar 20, 2020 at 15:22
-
Might be easier to just use
waitForKeyElements
. – woxxom Commented Mar 20, 2020 at 15:25 - @wOxxOm Could you please provide a code example for the matter? – Rudy Grinberg Commented Mar 20, 2020 at 15:41
- You can find a lot of those easily: here. The first result contains the script itself and the examples. – woxxom Commented Mar 20, 2020 at 15:44
2 Answers
Reset to default 2OK. I figured it out. I have used the following function:
waitForKeyElements (
"span.classname"
, soundfx, false
);
It works sort-of okay, I will play around with it to achieve better results. Thanks to all who mented on this post! :)
You need to iterate over each element returned by getElementsByClassName. This method returns HTMLCollection which has no any loop-like method, but you could convert it to Array, and then use forEach method:
var targets = document.getElementsByClassName("CLASSNAME");
Array.from(targets).forEach(target => {
mutationObserver.observe(target, {
attributes: true,
characterData: true,
childList: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true
});
});
Here is working example based on your code snippet: https://stackblitz./edit/js-qbetr2
本文标签: javascriptquotparameter 1 is not of type 39Node39quot when using MutationObserverStack Overflow
版权声明:本文标题:javascript - "parameter 1 is not of type 'Node'" when using MutationObserver - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745610411a2666080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论