实施"被动事件监听器"在Chrome扩展程序中

时间:2018-05-17 17:43:43

标签: javascript google-chrome-extension

我使用Chrome扩展程序修改缩放增量,并在控制台中抛出此错误:

window.addEventListener("mousewheel", function( event ) {
    if(enabled){
        //console.log("Scrolling Before: ",scrolling);
        if(!zooming){
            if(( event.wheelDeltaY > 0 || event.wheelDeltaY < 0 ) && !event.ctrlKey && !scrolling)
                scrolling = true;
        }
        //console.log("Scrolling: ",scrolling);
        if(!zooming && !scrolling){
                if(event.ctrlKey){
                zooming = true;
                tempZoomLevel = zoomLevel;
                if( event.wheelDeltaY > 0  ) {
                    tempZoomLevel += zoomIncrement;
                }
                if( event.wheelDeltaY < 0 ) {
                    tempZoomLevel -= zoomIncrement;
                }
                if((Date.now() - lastZoomEvent) > zoomDelay){
                    lastZoomEvent = Date.now();
                    zooming = false;
                    zoomLevel = tempZoomLevel;
                    chrome.runtime.sendMessage({zoom: zoomLevel, lastZoom:lastZoomEvent, from:"content"}, function(response) {
                    });
                }
                else
                    zooming = false;
            }
            event.preventDefault();
            event.stopPropagation();
        }
        /*else{

        }*/
        return false;
    }
}, {capture: true} );  /* Passive event listeners Mikhoul */

我尝试修改扩展程序以添加标记以使事件处理程序被动,但它似乎无法工作,我不知道为什么?

以下是代码:

{capture: true}

最后你会看到我已经添加了&#34;标志&#34; v6.x但我总是在控制台中出现此错误:

at the documentation for configuration

如果您需要查看完整的代码,还有我已修改的整个插件的链接:https://i.imgur.com/SDmGdVo.png

我错过了什么让听众被动并停止在控制台中抛出此错误?

问候!

1 个答案:

答案 0 :(得分:1)

来自documentation

您需要在事件处理程序中设置passive: true,如下所示:

window.addEventListener("mousewheel", function( event ) {
  // all of your function code
  // ...
}, { passive: true})