SPB Git

Repository Public License

scroll-reporter.js 1.8 KB 42 lines javascript

1// ORIX Navigator — scroll position reporter (collapsing dock, reading progress).2// World: orix.bridge (isolated). Frames: main only. Injection: document end.3// At most one message per animation frame, and only when the rounded values changed.4(function () {5  "use strict";6  if (window.__orixScroll) return;7  window.__orixScroll = true;8  var handler = window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.orixBridge;9  if (!handler) return;10  var queued = false, last = "";11  function measure() {12    queued = false;13    var root = document.scrollingElement || document.documentElement;14    if (!root) return;15    var payload = {16      x: Math.round(window.scrollX || 0),17      y: Math.round(window.scrollY || 0),18      contentWidth: Math.round(root.scrollWidth || 0),19      contentHeight: Math.round(root.scrollHeight || 0),20      viewportWidth: Math.round(window.innerWidth || 0),21      viewportHeight: Math.round(window.innerHeight || 0)22    };23    var key = payload.x + "," + payload.y + "," + payload.contentHeight + "," + payload.viewportHeight + "," + payload.contentWidth + "," + payload.viewportWidth;24    if (key === last) return;25    last = key;26    try { handler.postMessage({ v: 1, kind: "scroll", payload: payload }); } catch (e) {}27  }28  function schedule() {29    if (queued) return;30    queued = true;31    // rAF does not fire in a web view that is not being displayed; the timer keeps reports flowing there.32    var done = false;33    function run() { if (done) return; done = true; measure(); }34    if (window.requestAnimationFrame) window.requestAnimationFrame(run);35    setTimeout(run, 120);36  }37  window.addEventListener("scroll", schedule, { passive: true, capture: true });38  window.addEventListener("resize", schedule, { passive: true });39  window.addEventListener("load", schedule, { once: true });40  schedule();41})();42
Files main
Menu

Theme