最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - "parameter 1 is not of type 'Node'" when using MutationObserver - Stack Overflow

matteradmin1PV0评论

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
 |  Show 2 more ments

2 Answers 2

Reset to default 2

OK. 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

Post a comment

comment list (0)

  1. No comments so far