Last Updated: February 25, 2016
·
4.376K
· kajyr

MutationObserver polyfill

A small polyfill to handle mutation observer subtree modifications events

(function() {
  var MutationObserver;

  if (window.MutationObserver != null) {
    return;
  }

  MutationObserver = (function() {
    function MutationObserver(callBack) {
      this.callBack = callBack;
    }

    MutationObserver.prototype.observe = function(element, options) {
      this.element = element;
      return this.interval = setInterval((function(_this) {
        return function() {
          var html;
          html = _this.element.innerHTML;
          if (html !== _this.oldHtml) {
            _this.oldHtml = html;
            return _this.callBack.apply(null);
          }
        };
      })(this), 200);
    };

    MutationObserver.prototype.disconnect = function() {
      return window.clearInterval(this.interval);
    };

    return MutationObserver;

  })();

  window.MutationObserver = MutationObserver;

}).call(this);

And for who might appreciate, here's the coffeescript version ;-)

1 Response
Add your response

That was super useful since PhantomJS pre 2.0 still doesn't support MutationObserver. Thanks! :)

over 1 year ago ·