我得到"期望一个条件表达式,而是看到一个任务"即使它有效

时间:2018-04-04 09:19:21

标签: javascript tampermonkey

我将脚本smoothscroll.js加载到tampermonkey。 tampermonkey IDE显示该脚本有很多错误,但脚本工作得很好。但是,我决定修复这些错误,这很容易(缺少分号,缺少()和其他一些问题)。

但是,我很难解决以下错误。有一个do-while循环,其赋值为条件表达式。



function overflowingAncestor(el) {
  var elems = [];
  var rootScrollHeight = root.scrollHeight;
  do {
    var cached = cache[uniqueID(el)];
    if (cached) {
      return setCache(elems, cached);
    }
    elems.push(el);
    if (rootScrollHeight === el.scrollHeight) {
      if (!isFrame || root.clientHeight + 10 < rootScrollHeight) {
        return setCache(elems, document.body);
      }
    } else if (el.clientHeight + 10 < el.scrollHeight) {
      overflow = getComputedStyle(el, "").getPropertyValue("overflow-y");
      if (overflow === "scroll" || overflow === "auto") {
        return setCache(elems, el);
      }
    }
  } while (el = el.parentNode); //<--- Error
}
&#13;
&#13;
&#13;

我认为修复只是将while (el = el.parentNode);替换为while (el == el.parentNode);,但应用此功能后,smoothscroll将不再有效。

我知道我可以让它像它一样,但我试着理解为什么错误被抛出,即使它正在工作?

如果您想现场试用,请转到this page并将以下脚本加载到您的篡改引擎。

&#13;
&#13;
// ==UserScript==
// @name         Test
// @namespace    http://bs.to/
// @version      2.2
// @description  Test
// @author       Me
// @match        https://bs.to/andere-serien
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==

//Smoothscroll.js
(function() {
  var defaultOptions = {
    frameRate: 300,
    animationTime: 1500,
    stepSize: 120,
    pulseAlgorithm: true,
    pulseScale: 8,
    pulseNormalize: 1,
    accelerationDelta: 20,
    accelerationMax: 1,
    keyboardSupport: true,
    arrowScroll: 50,
    touchpadSupport: true,
    fixedBackground: true,
    excluded: ""
  };
  var options = defaultOptions;
  var isExcluded = false;
  var isFrame = false;
  var direction = {
    x: 0,
    y: 0
  };
  var initDone = false;
  var root = document.documentElement;
  var activeElement;
  var observer;
  var deltaBuffer = [120, 120, 120];
  var key = {
    left: 37,
    up: 38,
    right: 39,
    down: 40,
    spacebar: 32,
    pageup: 33,
    pagedown: 34,
    end: 35,
    home: 36
  };
  options = defaultOptions;

  function initTest() {
    var disableKeyboard = false;
    if (disableKeyboard) {
      removeEvent("keydown", keydown);
    }
    if (options.keyboardSupport && !disableKeyboard) {
      addEvent("keydown", keydown);
    }
  }

  function init() {
    if (!document.body)
      return;
    var body = document.body;
    var html = document.documentElement;
    var windowHeight = window.innerHeight;
    var scrollHeight = body.scrollHeight;
    root = (document.compatMode.indexOf('CSS') >= 0) ? html : body;
    activeElement = body;
    initTest();
    initDone = true;
    if (top != self) {
      isFrame = true;
    } else if (scrollHeight > windowHeight && (body.offsetHeight <= windowHeight || html.offsetHeight <= windowHeight)) {
      var pending = false;
      var refresh = function() {
        if (!pending && html.scrollHeight != document.height) {
          pending = true;
          setTimeout(function() {
            html.style.height = document.height + 'px';
            pending = false;
          }, 500);
        }
      };
      html.style.height = 'auto';
      setTimeout(refresh, 10);
      if (root.offsetHeight <= windowHeight) {
        var underlay = document.createElement("div");
        underlay.style.clear = "both";
        body.appendChild(underlay);
      }
    }
    if (!options.fixedBackground && !isExcluded) {
      body.style.backgroundAttachment = "scroll";
      html.style.backgroundAttachment = "scroll";
    }
  }
  var que = [];
  var pending = false;
  var lastScroll = +new Date();

  function scrollArray(elem, left, top, delay) {
    if (delay === undefined) {
      delay = 1000;
    }
    directionCheck(left, top);
    if (options.accelerationMax != 1) {
      var now = +new Date();
      var elapsed = now - lastScroll;
      if (elapsed < options.accelerationDelta) {
        var factor = (1 + (30 / elapsed)) / 2;
        if (factor > 1) {
          factor = Math.min(factor, options.accelerationMax);
          left *= factor;
          top *= factor;
        }
      }
      lastScroll = +new Date();
    }
    que.push({
      x: left,
      y: top,
      lastX: (left < 0) ? 0.99 : -0.99,
      lastY: (top < 0) ? 0.99 : -0.99,
      start: +new Date()
    });
    if (pending) {
      return;
    }
    var scrollWindow = (elem === document.body);
    var step = function(time) {
      var now = +new Date();
      var scrollX = 0;
      var scrollY = 0;
      for (var i = 0; i < que.length; i++) {
        var item = que[i];
        var elapsed = now - item.start;
        var finished = (elapsed >= options.animationTime);
        var position = (finished) ? 1 : elapsed / options.animationTime;
        if (options.pulseAlgorithm) {
          position = pulse(position);
        }
        var x = (item.x * position - item.lastX) >> 0;
        var y = (item.y * position - item.lastY) >> 0;
        scrollX += x;
        scrollY += y;
        item.lastX += x;
        item.lastY += y;
        if (finished) {
          que.splice(i, 1);
          i--;
        }
      }
      if (scrollWindow) {
        window.scrollBy(scrollX, scrollY);
      } else {
        if (scrollX)
          elem.scrollLeft += scrollX;
        if (scrollY)
          elem.scrollTop += scrollY;
      }
      if (!left && !top) {
        que = [];
      }
      if (que.length) {
        requestFrame(step, elem, (delay / options.frameRate + 1));
      } else {
        pending = false;
      }
    };
    requestFrame(step, elem, 0);
    pending = true;
  }

  function wheel(event) {
    if (!initDone) {
      init();
    }
    var target = event.target;
    var overflowing = overflowingAncestor(target);
    if (!overflowing || event.defaultPrevented || isNodeName(activeElement, "embed") || (isNodeName(target, "embed") && /\.pdf/i.test(target.src))) {
      return true;
    }
    var deltaX = event.wheelDeltaX || 0;
    var deltaY = event.wheelDeltaY || 0;
    if (!deltaX && !deltaY) {
      deltaY = event.wheelDelta || 0;
    }
    if (!options.touchpadSupport && isTouchpad(deltaY)) {
      return true;
    }
    if (Math.abs(deltaX) > 1.2) {
      deltaX *= options.stepSize / 120;
    }
    if (Math.abs(deltaY) > 1.2) {
      deltaY *= options.stepSize / 120;
    }
    scrollArray(overflowing, -deltaX, -deltaY);
    event.preventDefault();
  }

  function keydown(event) {
    var target = event.target;
    var modifier = event.ctrlKey || event.altKey || event.metaKey || (event.shiftKey && event.keyCode !== key.spacebar);
    if (/input|textarea|select|embed/i.test(target.nodeName) || target.isContentEditable || event.defaultPrevented || modifier) {
      return true;
    }
    if (isNodeName(target, "button") && event.keyCode === key.spacebar) {
      return true;
    }
    var shift, x = 0,
      y = 0;
    var elem = overflowingAncestor(activeElement);
    var clientHeight = elem.clientHeight;
    if (elem == document.body) {
      clientHeight = window.innerHeight;
    }
    switch (event.keyCode) {
      case key.up:
        y = -options.arrowScroll;
        break;
      case key.down:
        y = options.arrowScroll;
        break;
      case key.spacebar:
        shift = event.shiftKey ? 1 : -1;
        y = -shift * clientHeight * 0.9;
        break;
      case key.pageup:
        y = -clientHeight * 0.9;
        break;
      case key.pagedown:
        y = clientHeight * 0.9;
        break;
      case key.home:
        y = -elem.scrollTop;
        break;
      case key.end:
        var damt = elem.scrollHeight - elem.scrollTop - clientHeight;
        y = (damt > 0) ? damt + 10 : 0;
        break;
      case key.left:
        x = -options.arrowScroll;
        break;
      case key.right:
        x = options.arrowScroll;
        break;
      default:
        return true;
    }
    scrollArray(elem, x, y);
    event.preventDefault();
  }

  function mousedown(event) {
    activeElement = event.target;
  }
  var cache = {};
  setInterval(function() {
    cache = {};
  }, 10 * 1000);
  var uniqueID = (function() {
    var i = 0;
    return function(el) {
      return el.uniqueID || (el.uniqueID = i++);
    };
  })();

  function setCache(elems, overflowing) {
    for (var i = elems.length; i--;)
      cache[uniqueID(elems[i])] = overflowing;
    return overflowing;
  }

  function overflowingAncestor(el) {
    var elems = [];
    var rootScrollHeight = root.scrollHeight;
    do {
      var cached = cache[uniqueID(el)];
      if (cached) {
        return setCache(elems, cached);
      }
      elems.push(el);
      if (rootScrollHeight === el.scrollHeight) {
        if (!isFrame || root.clientHeight + 10 < rootScrollHeight) {
          return setCache(elems, document.body);
        }
      } else if (el.clientHeight + 10 < el.scrollHeight) {
        overflow = getComputedStyle(el, "").getPropertyValue("overflow-y");
        if (overflow === "scroll" || overflow === "auto") {
          return setCache(elems, el);
        }
      }
    } while (el = el.parentNode);
  }

  function addEvent(type, fn, bubble) {
    window.addEventListener(type, fn, (bubble || false));
  }

  function removeEvent(type, fn, bubble) {
    window.removeEventListener(type, fn, (bubble || false));
  }

  function isNodeName(el, tag) {
    return (el.nodeName || "").toLowerCase() === tag.toLowerCase();
  }

  function directionCheck(x, y) {
    x = (x > 0) ? 1 : -1;
    y = (y > 0) ? 1 : -1;
    if (direction.x !== x || direction.y !== y) {
      direction.x = x;
      direction.y = y;
      que = [];
      lastScroll = 0;
    }
  }
  var deltaBufferTimer;

  function isTouchpad(deltaY) {
    if (!deltaY)
      return;
    deltaY = Math.abs(deltaY);
    deltaBuffer.push(deltaY);
    deltaBuffer.shift();
    clearTimeout(deltaBufferTimer);
    var allDivisable = (isDivisible(deltaBuffer[0], 120) && isDivisible(deltaBuffer[1], 120) && isDivisible(deltaBuffer[2], 120));
    return !allDivisable;
  }

  function isDivisible(n, divisor) {
    return (Math.floor(n / divisor) == n / divisor);
  }
  var requestFrame = (function() {
    return window.requestAnimationFrame || window.webkitRequestAnimationFrame || function(callback, element, delay) {
      window.setTimeout(callback, delay || (1000 / 60));
    };
  })();

  function pulse_(x) {
    var val, start, expx;
    x = x * options.pulseScale;
    if (x < 1) {
      val = x - (1 - Math.exp(-x));
    } else {
      start = Math.exp(-1);
      x -= 1;
      expx = 1 - Math.exp(-x);
      val = start + (expx * (1 - start));
    }
    return val * options.pulseNormalize;
  }

  function pulse(x) {
    if (x >= 1)
      return 1;
    if (x <= 0)
      return 0;
    if (options.pulseNormalize == 1) {
      options.pulseNormalize /= pulse_(1);
    }
    return pulse_(x);
  }
  var isChrome = /chrome/i.test(window.navigator.userAgent);
  var wheelEvent = null;
  if ("onwheel" in document.createElement("div"))
    wheelEvent = "wheel";
  else if ("onmousewheel" in document.createElement("div"))
    wheelEvent = "mousewheel";
  if (wheelEvent && isChrome) {
    addEvent(wheelEvent, wheel);
    addEvent("mousedown", mousedown);
    addEvent("load", init);
  }
})();
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

要使linter满意,请将作业移至while之前的行,然后while(el)

do {
    // snip
    el = el.parentNode;
} while (el);

替代地

do {
    // snip

} while ((el = el.parentNode));

不确定所有“linters”是否会很好用,但jshint通常会

答案 1 :(得分:1)

我不熟悉tampermonkey,但是根据你的描述,它似乎比JavaScript解析器更严格。无论如何,要理解为什么while (el = el.parentNode)有效,你需要知道两件事。

(1)JavaScript中的条件(ifwhile中使用的条件以及传统for循环的第二个表达式)不必是严格的布尔表达式,例如x == 1。它们可以是你喜欢的任何表达方式。 JavaScript将评估该表达式,然后确定结果是否为&#34; truthy&#34;或&#34; falsy&#34; (那些是实际的话)。 A&#34; falsy&#34;值为falsenullundefined,0或空字符串。任何其他价值都是&#34; truthy&#34; (包括空数组或空对象)。 Truthy值意味着将执行if分支,或者在您的情况下,循环将继续。

(2)JavaScript中的值赋值是一个类似于任何其他表达式的表达式,并且表达式总是有结果。在值赋值的情况下,它是指定的值。严格来说,左侧变量最终包含指定值的事实是&#34;副作用&#34;。

考虑到这两个事实,这里发生了while (el = el.parentNode)

的情况
  1. 解析器评估el.parentNode。这可能是一个节点,如果null没有父级,则为el。(如果它是文档本身)。
  2. el.parentNode的值已分配给el。分配的值的此操作的结果。
  3. 然后评估该结果是真实的或虚假的。如果el没有父级,则el.parentNodenull,这是一个假值,因此循环停止。如果不是,则循环继续。
  4. 因此,JavaScript允许条件中的变量赋值,但似乎tampermonkey没有。这可能是因为99%的时间您确实希望在条件中使用==,并且很容易意外地输入=而不是==。这可能导致很多挫折和难以发现的错误,因为如上所述,JavaScript会愉快地处理它而不会出错,因此很多短语都会将其标记为错误。

    此外,el == el.parentNode没有意义,因为节点永远不能与其父节点相等。