summaryrefslogtreecommitdiff
path: root/bin/wiki/ImportarDesdeURL/node_modules/smartquotes/lib/listen.js
blob: 50a0314d7a4607e40884819114bad1ae82954fbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const element = require('./element'),
      string  = require('./string');

function listen(root) {
  const observer = new MutationObserver( mutations => {
    mutations.forEach( mutation => {
      for(let node of mutation.addedNodes) {
        element(node);
      }
    });
  });
  listen.runOnReady( () => {
    observer.observe(root || document.body, {
      childList: true,
      subtree: true
    });
  });
  return observer;
}

listen.runOnReady = function(run) {
  // if called without arguments, run on the entire body after the document has loaded
  if (document.readyState !== 'loading') {
    // we're already ready
    run();
  } else if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', run, false);
  } else {
    const readyStateCheckInterval = setInterval(() => {
      if (document.readyState !== 'loading') {
        clearInterval(readyStateCheckInterval);
        run();
      }
    }, 10);
  }
};

module.exports = listen;