title: MutationObserver error : How could I wait for the creation of an element?
body: <p>I'm trying to make a Greasemonkey script to add a button in a Facebook page.
  But I need to wait for the creation of an element to add the button on it.<br>
  I'm trying to use <code>MutationObserver</code> (<a href="https://stackoverflow.com/a/16726669/3156085">As suggested here</a>) to do it but there is an error.</p>
  
  <pre><code>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 &lt; 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');
  </code></pre>
  
  <p>But I got the following logs:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>What is the cause of this error?
  How could I fix this?</p>
  
  <p>If the element is created before the execution of my script, will the <code>MutationObserver</code> still be triggered? (IE, if the element already exists when I create the MutationObserver).</p>
  
  <p><strong>NB:</strong> I tried to use <code>$(document).ready()</code>, but it's triggered before the creation of the element I want to add the button to.</p>
  
  <p><strong>NB-bis</strong>: I didn't post the whole script.</p>