Add passive listeners

Viewed 951

So Google PageSpeed Insights is complaining about "Does not use passive listeners to improve scrolling performance", the complaint is more specifically about:

https://www.youtube.com/s/player/54668ca9/player_ias.vflset/en_US/base.js and on line 5402, which would be this:

g.h.dump=function(){var a=[],b;for(b in Gr)a.push(b+"="+encodeURIComponent(String(Gr[b])));return a.join("&")};

I have my youtube video in HTML in an iframe-tag

<iframe src="https://www.youtube.com/embed/..........." width="373" height="200" frameborder="0" allowfullscreen="allowfullscreen"></iframe>

The question is what should I do. I found the javascript code below which supposedly should resolve the issue, but I do not know where to put it. Should i put something in my functions.php-file? The https://www.youtube.com/s/player/54668ca9/player_ias.vflset/en_US/base.js is not a property of my domain so I do not have this js-file in my files (my Wordpress public_html), so I can not modify it. The javascript that I found at Wordpress and best practice with passive event listeners and could work:

(function() {
  var supportsPassive = eventListenerOptionsSupported();  

  if (supportsPassive) {
    var addEvent = EventTarget.prototype.addEventListener;
    overwriteAddEvent(addEvent);
  }

  function overwriteAddEvent(superMethod) {
    var defaultOptions = {
      passive: true,
      capture: false
    };

    EventTarget.prototype.addEventListener = function(type, listener, options) {
      var usesListenerOptions = typeof options === 'object';
      var useCapture = usesListenerOptions ? options.capture : options;

      options = usesListenerOptions ? options : {};
      options.passive = options.passive !== undefined ? options.passive : defaultOptions.passive;
      options.capture = useCapture !== undefined ? useCapture : defaultOptions.capture;

      superMethod.call(this, type, listener, options);
    };
  }

  function eventListenerOptionsSupported() {
    var supported = false;
    try {
      var opts = Object.defineProperty({}, 'passive', {
        get: function() {
          supported = true;
        }
      });
      window.addEventListener("test", null, opts);
    } catch (e) {}

    return supported;
  }
})();
0 Answers
Related