Add passive listeners

夙愿已清 提交于 2020-12-07 04:01:31

问题


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;
  }
})();

来源:https://stackoverflow.com/questions/62742894/add-passive-listeners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!