SPB Git

Repository Public License

lens.js 5.4 KB 124 lines javascript

1// ORIX Navigator — Lens: text selection reporting and element picking.2// World: orix.lens (isolated). Frames: main only. Injection: document end.3// Selection changes are reported (debounced). Element picking is off until the native side calls4// `__orixLens.startPicking()`; a pick reports one element and stops.5(function () {6  "use strict";7  if (window.__orixLens) return;8  var handler = window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.orixBridge;9  if (!handler) return;10  function cap(value, max) {11    value = typeof value === "string" ? value : "";12    return value.length > max ? value.slice(0, max) : value;13  }14  function rectOf(r) {15    return { x: Math.round(r.left), y: Math.round(r.top), width: Math.round(r.width), height: Math.round(r.height) };16  }17  function post(kind, payload) {18    try { handler.postMessage({ v: 1, kind: kind, payload: payload }); } catch (e) {}19  }2021  // --- Selection -------------------------------------------------------------------------------------------22  function currentSelection() {23    var sel = window.getSelection ? window.getSelection() : null;24    var text = sel ? String(sel) : "";25    var rect = { x: 0, y: 0, width: 0, height: 0 };26    if (sel && sel.rangeCount > 0 && !sel.isCollapsed) {27      try { rect = rectOf(sel.getRangeAt(0).getBoundingClientRect()); } catch (e) {}28    }29    return { url: cap(location.href, 8192), text: cap(text, 20000), isCollapsed: !sel || sel.isCollapsed || text.length === 0, rect: rect };30  }31  var selectionTimer = 0, lastSelection = "";32  document.addEventListener("selectionchange", function () {33    clearTimeout(selectionTimer);34    selectionTimer = setTimeout(function () {35      var payload = currentSelection();36      var key = payload.text + "|" + payload.rect.x + "," + payload.rect.y;37      if (key === lastSelection) return;38      lastSelection = key;39      post("selection", payload);40    }, 150);41  });4243  // --- Element picking -------------------------------------------------------------------------------------44  var picking = false, overlay = null, hovered = null;45  function cssPath(el) {46    var parts = [];47    while (el && el.nodeType === 1 && parts.length < 12) {48      var part = el.tagName.toLowerCase();49      if (el.id && /^[A-Za-z][\w-]*$/.test(el.id)) { parts.unshift(part + "#" + el.id); break; }50      var parent = el.parentElement;51      if (parent) {52        var same = Array.prototype.filter.call(parent.children, function (c) { return c.tagName === el.tagName; });53        if (same.length > 1) part += ":nth-of-type(" + (same.indexOf(el) + 1) + ")";54      }55      parts.unshift(part);56      el = parent;57    }58    return parts.join(" > ");59  }60  function describe(el) {61    var href = "", src = "";62    try { var a = el.closest("a[href]"); if (a) href = new URL(a.getAttribute("href"), document.baseURI).href; } catch (e) {}63    try { var s = el.currentSrc || el.getAttribute("src"); if (s) src = new URL(s, document.baseURI).href; } catch (e) {}64    return {65      url: cap(location.href, 8192),66      tag: el.tagName.toLowerCase(),67      selector: cap(cssPath(el), 1024),68      text: cap((el.innerText || el.textContent || "").trim(), 20000),69      html: cap(el.outerHTML || "", 50000),70      href: cap(href, 8192), src: cap(src, 8192),71      rect: rectOf(el.getBoundingClientRect())72    };73  }74  function moveOverlay(el) {75    if (!overlay) return;76    var r = el.getBoundingClientRect();77    overlay.style.left = r.left + "px"; overlay.style.top = r.top + "px";78    overlay.style.width = r.width + "px"; overlay.style.height = r.height + "px";79  }80  function onMove(event) {81    var el = document.elementFromPoint(event.clientX, event.clientY);82    if (!el || el === overlay) return;83    hovered = el; moveOverlay(el);84  }85  function onClick(event) {86    if (!event.isTrusted) return;87    event.preventDefault(); event.stopPropagation(); event.stopImmediatePropagation();88    var el = hovered || document.elementFromPoint(event.clientX, event.clientY);89    stop();90    if (el) post("elementPicked", describe(el));91  }92  function onKey(event) { if (event.key === "Escape") { stop(); post("elementPicked", { cancelled: true }); } }93  function start() {94    if (picking) return true;95    picking = true;96    overlay = document.createElement("div");97    overlay.setAttribute("data-orix-lens", "overlay");98    overlay.style.cssText = "position:fixed;z-index:2147483647;pointer-events:none;border:2px solid #5b8cff;background:rgba(91,140,255,.18);border-radius:4px;left:0;top:0;width:0;height:0;transition:all 60ms ease-out;";99    (document.body || document.documentElement).appendChild(overlay);100    document.addEventListener("mousemove", onMove, true);101    document.addEventListener("click", onClick, true);102    document.addEventListener("keydown", onKey, true);103    return true;104  }105  function stop() {106    if (!picking) return false;107    picking = false; hovered = null;108    document.removeEventListener("mousemove", onMove, true);109    document.removeEventListener("click", onClick, true);110    document.removeEventListener("keydown", onKey, true);111    if (overlay && overlay.parentNode) overlay.parentNode.removeChild(overlay);112    overlay = null;113    return true;114  }115116  window.__orixLens = {117    selection: currentSelection,118    startPicking: start,119    stopPicking: stop,120    isPicking: function () { return picking; },121    describe: function (selector) { var el = document.querySelector(selector); return el ? describe(el) : null; }122  };123})();124
Files main
Menu

Theme