SPB Git

Repository Public License

morph.js 1.9 KB 53 lines javascript

1// ORIX Navigator — Morph: per-site CSS injection helper.2// World: orix.morph (isolated). Frames: all. Injection: document start.3// CSS only: the text is assigned to `textContent` of a <style> element, never parsed as HTML. Each sheet has an4// id so it can be replaced or removed. The native side validates the CSS (no @import, no remote url()).5(function () {6  "use strict";7  if (window.__orixMorph) return;8  var sheets = {};9  function hostMatches(host, patterns) {10    host = String(host || "").toLowerCase();11    for (var i = 0; i < patterns.length; i++) {12      var p = String(patterns[i]).toLowerCase();13      if (p === "*" || host === p || host.endsWith("." + p)) return true;14    }15    return false;16  }17  function mount(node) {18    var parent = document.head || document.documentElement;19    if (parent && node.parentNode !== parent) parent.appendChild(node);20  }21  function apply(id, css) {22    id = String(id);23    var node = sheets[id];24    if (!node) {25      node = document.createElement("style");26      node.setAttribute("data-orix-morph", id);27      sheets[id] = node;28    }29    node.textContent = String(css);30    mount(node);31    return true;32  }33  function remove(id) {34    id = String(id);35    var node = sheets[id];36    if (!node) return false;37    if (node.parentNode) node.parentNode.removeChild(node);38    delete sheets[id];39    return true;40  }41  // A style appended to <html> before <head> exists is moved into <head> once the document is parsed, and42  // re-attached if a framework replaces the head.43  document.addEventListener("DOMContentLoaded", function () {44    Object.keys(sheets).forEach(function (id) { mount(sheets[id]); });45  });46  window.__orixMorph = {47    apply: apply,48    remove: remove,49    applyForHosts: function (id, css, patterns) { return hostMatches(location.hostname, patterns) ? apply(id, css) : false; },50    list: function () { return Object.keys(sheets); }51  };52})();53
Files main
Menu

Theme