SPB Git

Repository Public License

page-metadata.js 3.6 KB 90 lines javascript

1// ORIX Navigator — page metadata.2// World: orix.bridge (isolated). Frames: main only. Injection: document end.3// Reports favicons, theme-color, canonical URL, description, language and Open Graph tags; re-reports when4// <head> changes (single-page apps). Every string is length-capped here and validated again natively.5(function () {6  "use strict";7  if (window.__orixMetadata) return;8  window.__orixMetadata = true;9  var handler = window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.orixBridge;10  if (!handler) return;1112  function cap(value, max) {13    if (typeof value !== "string") return "";14    value = value.trim();15    return value.length > max ? value.slice(0, max) : value;16  }17  function absolute(href) {18    try {19      var url = new URL(href, document.baseURI);20      return (url.protocol === "http:" || url.protocol === "https:") ? cap(url.href, 4096) : "";21    } catch (e) { return ""; }22  }23  function meta(selector) {24    var node = document.querySelector(selector);25    return node ? cap(node.getAttribute("content") || "", 2048) : "";26  }27  function themeColor() {28    var nodes = document.querySelectorAll('meta[name="theme-color"]');29    for (var i = 0; i < nodes.length; i++) {30      var media = nodes[i].getAttribute("media");31      if (!media || (window.matchMedia && window.matchMedia(media).matches)) {32        return cap(nodes[i].getAttribute("content") || "", 64);33      }34    }35    return "";36  }37  function collect() {38    var icons = [];39    var links = document.querySelectorAll('link[rel~="icon"], link[rel="apple-touch-icon"], link[rel="apple-touch-icon-precomposed"], link[rel="mask-icon"]');40    for (var i = 0; i < links.length && icons.length < 16; i++) {41      var href = absolute(links[i].getAttribute("href") || "");42      if (!href) continue;43      icons.push({44        href: href,45        rel: cap(links[i].getAttribute("rel") || "", 64).toLowerCase(),46        sizes: cap(links[i].getAttribute("sizes") || "", 64).toLowerCase(),47        type: cap(links[i].getAttribute("type") || "", 64).toLowerCase()48      });49    }50    var canonical = document.querySelector('link[rel="canonical"]');51    return {52      url: cap(location.href, 8192),53      title: cap(document.title, 1024),54      description: meta('meta[name="description"]'),55      language: cap(document.documentElement.getAttribute("lang") || "", 35),56      themeColor: themeColor(),57      canonical: canonical ? absolute(canonical.getAttribute("href") || "") : "",58      icons: icons,59      openGraph: {60        title: meta('meta[property="og:title"]'),61        description: meta('meta[property="og:description"]'),62        image: absolute(meta('meta[property="og:image"]')),63        siteName: meta('meta[property="og:site_name"]'),64        type: cap(meta('meta[property="og:type"]'), 64),65        url: absolute(meta('meta[property="og:url"]'))66      }67    };68  }6970  var lastSent = "";71  function send() {72    var payload = collect();73    var encoded = JSON.stringify(payload);74    if (encoded === lastSent) return;75    lastSent = encoded;76    try { handler.postMessage({ v: 1, kind: "metadata", payload: payload }); } catch (e) {}77  }78  var timer = 0;79  function schedule() { clearTimeout(timer); timer = setTimeout(send, 200); }8081  send();82  window.addEventListener("load", schedule, { once: true });83  window.addEventListener("pageshow", schedule);84  window.addEventListener("popstate", schedule);85  window.addEventListener("hashchange", schedule);86  if (document.head && window.MutationObserver) {87    new MutationObserver(schedule).observe(document.head, { childList: true, subtree: true, attributes: true, characterData: true });88  }89})();90
Files main
Menu

Theme