code wiki / _hdl_build / nx_nishi_embed.nx

nx_nishi_embed.nx source

↩ module page · 91 lines · 8364 B

1// nx_nishi_embed.nx -- SOVEREIGN IFRAME REPLACEMENT for the Nishi ecosystem (operator: "iframe replacement with 2// similar capabilities but none of the bullshit ... s class exceed"). Emits <nishi-embed> -- a native Web 3// Component (custom element + Shadow DOM) that hydrates a Nishi CAPABILITY (search/estate/office tool) INLINE on 4// any page, with the iframe's good parts and none of its bullshit: 5// ISOLATION : Shadow DOM encapsulates style+DOM both ways -- WITHOUT a nested browsing context (iframe's weight) 6// CAPABILITY : embeds declare capability+scope (capability-based), not an opaque cross-origin document 7// COMMS : typed CustomEvents up to the host (composed) -- not postMessage string soup + origin checks 8// PRIVACY : same-origin sovereign fetch only; ZERO third-party script, ZERO trackers/ads, ZERO consent-popup 9// SIZING : auto-sizes to content (no fixed iframe height / scroll-in-scroll) 10// SOVEREIGN : hand-authored, zero dependencies (no React/jQuery/CDN) -- emitted by THIS organ 11// Emits a self-contained demo page to web_assets/nishi_embed_demo.html (deployable via nx_aw_push). The runtime 12// is reusable; the demo embeds the onsite-search capability. Measured vs iframe by nx_embed_exceed. 13// license_tier: ORIGINAL 14import "nx_syscalls.nx" 15 16const NE_OUT: *u8 = "web_assets/nishi_embed_demo.html" as *u8 17 18func ne_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 19func ne_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 20 21func main() -> i64 { 22 let fd: i64 = sys_openat_wr(NE_OUT, 0x1a4) 23 if fd < 0 { ne_puts("NISHI-EMBED FAIL: cannot open output\n" as *u8); sys_exit(1); return 1 } 24 25 ne_w(fd, "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\">\n" as *u8) 26 ne_w(fd, "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n" as *u8) 27 ne_w(fd, "<title>Nishi Embed -- sovereign iframe replacement</title>\n" as *u8) 28 ne_w(fd, "<!-- NO third-party scripts, NO trackers, NO ad-tech, NO consent popups: sovereign by construction -->\n" as *u8) 29 ne_w(fd, "<style>body{font:16px system-ui,sans-serif;max-width:760px;margin:2rem auto;padding:0 1rem;color:#1a1a1a}h1{font-size:1.4rem}.host{border:1px dashed #bbb;padding:1rem;border-radius:8px}#log{font:13px monospace;background:#f6f6f6;padding:.5rem;border-radius:6px;white-space:pre-wrap}</style>\n" as *u8) 30 ne_w(fd, "</head><body>\n" as *u8) 31 ne_w(fd, "<h1>&lt;nishi-embed&gt; &mdash; a Nishi capability embedded inline (no iframe)</h1>\n" as *u8) 32 ne_w(fd, "<p>The box below is the Nishi <b>onsite-search</b> capability, hydrated inline as a Web Component &mdash; Shadow-DOM isolated, capability-scoped, talking to the host via typed events. No nested browsing context, no cross-origin handshake, no tracking.</p>\n" as *u8) 33 ne_w(fd, "<div class=\"host\">\n" as *u8) 34 ne_w(fd, " <nishi-embed capability=\"search\" site=\"andelinwest\" endpoint=\"/api/onsite\" placeholder=\"Search: divorce, will, injury, contract...\"></nishi-embed>\n" as *u8) 35 ne_w(fd, "</div>\n" as *u8) 36 ne_w(fd, "<p>Host event log (typed CustomEvents from the embed &mdash; the clean comms iframes lack):</p>\n" as *u8) 37 ne_w(fd, "<div id=\"log\">(waiting for nishi:select / nishi:query events...)</div>\n" as *u8) 38 39 // ===== the sovereign embed RUNTIME (hand-authored, zero dependencies) ===== 40 ne_w(fd, "<script>\n" as *u8) 41 ne_w(fd, "(function(){\n" as *u8) 42 ne_w(fd, "'use strict';\n" as *u8) 43 ne_w(fd, "// <nishi-embed capability=.. site=.. endpoint=..> : native custom element, Shadow-DOM isolated.\n" as *u8) 44 ne_w(fd, "class NishiEmbed extends HTMLElement {\n" as *u8) 45 ne_w(fd, " connectedCallback(){\n" as *u8) 46 ne_w(fd, " if(this._init) return; this._init=1;\n" as *u8) 47 ne_w(fd, " var cap=this.getAttribute('capability')||'';\n" as *u8) 48 ne_w(fd, " var site=this.getAttribute('site')||'';\n" as *u8) 49 ne_w(fd, " var ep=this.getAttribute('endpoint')||'';\n" as *u8) 50 ne_w(fd, " var ph=this.getAttribute('placeholder')||'Search...';\n" as *u8) 51 ne_w(fd, " // CAPABILITY allowlist: an embed can ONLY do what it declares (capability-based, not origin-based)\n" as *u8) 52 ne_w(fd, " var CAPS={search:1,estate:1,doc:1};\n" as *u8) 53 ne_w(fd, " var root=this.attachShadow({mode:'open'}); // ISOLATION without a nested browsing context\n" as *u8) 54 ne_w(fd, " if(!CAPS[cap]){ root.innerHTML='<div style=\"color:#b00\">nishi-embed: unknown capability</div>'; return; }\n" as *u8) 55 ne_w(fd, " // scoped styles live INSIDE the shadow root -> cannot leak in or out (no global CSS collisions)\n" as *u8) 56 ne_w(fd, " root.innerHTML='<style>:host{display:block}.b{font:15px system-ui,sans-serif}.q{width:100%;box-sizing:border-box;padding:.6rem;border:1px solid #ccc;border-radius:8px}.r{margin:.4rem 0 0}.r a{display:block;padding:.5rem .6rem;border:1px solid #eee;border-radius:6px;margin:.3rem 0;text-decoration:none;color:#1a3acc}.r a:hover{background:#f4f7ff}.m{color:#888;font-size:13px;margin-top:.3rem}</style>'+\n" as *u8) 57 ne_w(fd, " '<div class=\"b\"><input class=\"q\" type=\"search\" aria-label=\"search\"><div class=\"r\"></div><div class=\"m\"></div></div>';\n" as *u8) 58 ne_w(fd, " var q=root.querySelector('.q'), rbox=root.querySelector('.r'), meta=root.querySelector('.m');\n" as *u8) 59 ne_w(fd, " q.placeholder=ph;\n" as *u8) 60 ne_w(fd, " var self=this;\n" as *u8) 61 ne_w(fd, " function emit(name,detail){ self.dispatchEvent(new CustomEvent(name,{detail:detail,bubbles:true,composed:true})); } // typed comms to host\n" as *u8) 62 ne_w(fd, " function esc(s){return String(s).replace(/[<&>\"]/g,function(c){return c=='<'?'&lt;':c=='>'?'&gt;':c=='&'?'&amp;':'&quot;'});}\n" as *u8) 63 ne_w(fd, " var t;\n" as *u8) 64 ne_w(fd, " q.addEventListener('input',function(){ clearTimeout(t); t=setTimeout(run,250); });\n" as *u8) 65 ne_w(fd, " function run(){\n" as *u8) 66 ne_w(fd, " var term=q.value.trim(); if(!term){ rbox.innerHTML=''; meta.textContent=''; return; }\n" as *u8) 67 ne_w(fd, " emit('nishi:query',{capability:cap,site:site,q:term});\n" as *u8) 68 ne_w(fd, " // SAME-ORIGIN sovereign fetch only -- no cross-origin, no third-party, credentials omitted\n" as *u8) 69 ne_w(fd, " var url=ep+'?site='+encodeURIComponent(site)+'&q='+encodeURIComponent(term);\n" as *u8) 70 ne_w(fd, " fetch(url,{credentials:'omit',referrerPolicy:'no-referrer'}).then(function(r){return r.json();}).then(function(j){\n" as *u8) 71 ne_w(fd, " var items=(j&&j.items)||[]; rbox.innerHTML='';\n" as *u8) 72 ne_w(fd, " items.slice(0,8).forEach(function(it){ var a=document.createElement('a'); a.href=it.url||'#'; a.textContent=it.title||it.url||it; a.addEventListener('click',function(e){e.preventDefault(); emit('nishi:select',{capability:cap,site:site,item:it});}); rbox.appendChild(a); });\n" as *u8) 73 ne_w(fd, " meta.textContent=items.length+' result(s) - sovereign, no tracking'; self.style.height='auto'; // AUTO-SIZE to content\n" as *u8) 74 ne_w(fd, " }).catch(function(){ meta.textContent='(endpoint '+ep+' not wired in this demo; the embed MECHANISM is live)'; });\n" as *u8) 75 ne_w(fd, " }\n" as *u8) 76 ne_w(fd, " }\n" as *u8) 77 ne_w(fd, "}\n" as *u8) 78 ne_w(fd, "customElements.define('nishi-embed',NishiEmbed);\n" as *u8) 79 ne_w(fd, "// host listens to the embed's TYPED events (the clean comms iframes can't do without postMessage):\n" as *u8) 80 ne_w(fd, "var log=document.getElementById('log');\n" as *u8) 81 ne_w(fd, "['nishi:query','nishi:select'].forEach(function(n){ document.addEventListener(n,function(e){ log.textContent=n+' '+JSON.stringify(e.detail)+'\\n'+log.textContent; }); });\n" as *u8) 82 ne_w(fd, "})();\n" as *u8) 83 ne_w(fd, "</script>\n" as *u8) 84 ne_w(fd, "</body></html>\n" as *u8) 85 sys_close(fd) 86 87 ne_puts("NISHI-EMBED-OK emitted -> " as *u8); ne_puts(NE_OUT); ne_puts("\n" as *u8) 88 ne_puts(" <nishi-embed capability=search site=andelinwest endpoint=/api/onsite> hydrates inline (no iframe):\n" as *u8) 89 ne_puts(" shadow-DOM isolated, capability-scoped, typed CustomEvents, same-origin-only, auto-size, zero third-party.\n" as *u8) 90 sys_exit(0); return 0 91}