简体   繁体   English

脚本未在Firefox中运行,但已在Chrome中运行

[英]Script not running in Firefox, but ran in Chrome

I made a script to register the thumb scroll wheel event (MX Master 2S if you're wondering). 我编写了一个脚本来注册拇指滚轮事件(如果您想知道的话,是MX Master 2S)。 However, this script ran perfectly fine in Chrome, but not in Firefox (Quantum). 但是,此脚本在Chrome中运行得很好,但在Firefox(Quantum)中却没有。 Why is that so? 为什么会这样?

var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
var elements = document.getElementsByClassName('pagination'); // get the elements
var search = (elements[0].innerHTML.match(regex));

//alert(search);
if(document.addEventListener){
    document.addEventListener("mousewheel", MouseWheelHandler, false);
    document.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
} else {
    document.attachEvent("onmousewheel", MouseWheelHandler);
}

function MouseWheelHandler(e) {
    var e = window.event || e;
  var ret = true;

  if (e.wheelDelta) {
    // Tilt to the left
    if (e.wheelDeltaX < 0) {
        str = window.location.toString();
        strsplit = str.split('/');
        preloc=Number(strsplit[4])+1;
        if (preloc > 0) {
        window.location.replace("https://somepage.com/page/"+preloc);}
        prelocstr=preloc.toString();
        if (prelocstr == "NaN") {
        window.location.replace(search[0]); }
      ret = false;
    }
    // Tilt to the right
    if (e.wheelDeltaX > 0) {
        str = window.location.toString();
        strsplit = str.split('/');
        preloc=Number(strsplit[4])-1;
        if (preloc > 0) {
        window.location.replace("https://somepage.com/page/"+preloc);}
      ret = false;
    }
  }

  event.returnValue = ret;
}

This script is made within Tampermonkey. 该脚本在Tampermonkey中制作。 Could anyone point me the mistake? 谁能指出我的错误? Thanks in advance! 提前致谢!

There's a newer standard for handling mouse wheel event that's standard across browsers: 有一个用于处理鼠标滚轮事件的新标准,这在所有浏览器中都是标准的:

https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent https://developer.mozilla.org/zh-CN/docs/Web/API/WheelEvent

https://developer.mozilla.org/en-US/docs/Web/Events/wheel https://developer.mozilla.org/zh-CN/docs/Web/Events/wheel

To use this event, do: 要使用此事件,请执行以下操作:

document.addEventListener("wheel", MouseWheelHandler);

And there's no need for: 并且不需要:

e = window.event || e

The event will be there. 活动将在那里。

Only DOMMouseScroll works with Firefox, but it uses a different API. DOMMouseScroll可与Firefox一起使用,但它使用不同的API。 So, you have to write a separate handler for Firefox, instead of using the MouseWheelHandler , or adjust the MouseWheelHandler to support both. 因此,您必须为Firefox编写一个单独的处理程序,而不是使用MouseWheelHandler ,或者调整MouseWheelHandler以支持两者。

As kshetline pointed out, there is now a new standard, which works with all modern browsers: https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent . 正如kshetline指出的那样,现在有一个新标准可以在所有现代浏览器中使用: https : //developer.mozilla.org/en-US/docs/Web/API/WheelEvent

The other two options don't work in Firefox, as stated here: 如下面所述,其他两个选项在Firefox中不起作用:

This feature is non-standard and is not on a standards track. 此功能是非标准的,不在标准轨道上。 Do not use it on production sites facing the Web: it will not work for every user. 请勿在面向Web的生产站点上使用它:它不适用于每个用户。 There may also be large incompatibilities between implementations and the behavior may change in the future. 实现之间也可能存在很大的不兼容性,并且将来的行为可能会更改。

Source: https://developer.mozilla.org/en-US/docs/Web/Events/mousewheel 资料来源: https : //developer.mozilla.org/en-US/docs/Web/Events/mousewheel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM