Spaces:
Running
Running
Download frame.js from EncryptidSystems/EdgeAI: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/spaces/EncryptidSystems/EdgeAI/resolve/main/frame.js
- Command line
-
hf download hf://spaces/EncryptidSystems/EdgeAI/frame.js
-
curl -L -o frame.js https://huggingface.co/spaces/EncryptidSystems/EdgeAI/resolve/main/frame.js
3.7 kB
| // blink.mojom.LocalFrame.JavaScriptExecuteRequest. | |
| // The page is the frame. It runs the script. The browser sends the request | |
| // with its own name. A quoted copy of the script is not a result, and neither | |
| // is an interface line. | |
| import { instantiate, run } from "./loader.js"; | |
| import { localFrameWindow, pageFetch } from "./browse.js"; | |
| const GUEST = "local_frame_exec.wasm"; | |
| let bytesP = null; | |
| export function requestValue(stdout) { | |
| const lines = String(stdout || "").split(/\r?\n/); | |
| for (let i = 0; i < lines.length; i++) { | |
| const s = lines[i].trim(); | |
| if (!s) continue; | |
| if (s.startsWith("interface:") || s.startsWith("func ")) continue; | |
| return s; | |
| } | |
| return ""; | |
| } | |
| // The guest quotes a script it does not evaluate. That quote is not a page. | |
| // A short prefix plus the question is the same echo. | |
| export function frameEcho(output, question) { | |
| const q = String(question || "").replace(/\s+/g, " ").trim(); | |
| if (q.length < 8) return false; | |
| let s = String(output || "").trim(); | |
| if (s.length >= 2 && s[0] === "\"" && s[s.length - 1] === "\"") s = s.slice(1, -1).trim(); | |
| s = s.replace(/\s+/g, " ").trim(); | |
| if (!s) return false; | |
| const a = s.toLowerCase(); | |
| const b = q.toLowerCase(); | |
| if (a === b) return true; | |
| // The guest repeats the question with a short lead-in. A page that merely | |
| // contains the subject is the lookup, not that echo. | |
| const at = a.lastIndexOf(b); | |
| if (at < 0) return false; | |
| const extra = a.length - b.length; | |
| return extra < 160 && extra < b.length; | |
| } | |
| export async function executeBytes(bytes, javascript) { | |
| const js = String(javascript ?? ""); | |
| let out = ""; | |
| const loaded = await instantiate(bytes, { | |
| args: [GUEST, js], | |
| stdout: (t) => { out += t; }, | |
| stderr: () => {}, | |
| }); | |
| await run(loaded); | |
| return requestValue(out); | |
| } | |
| function asText(value) { | |
| if (value == null) return ""; | |
| if (typeof value === "string") return value; | |
| if (typeof value === "number" || typeof value === "boolean") return String(value); | |
| return ""; | |
| } | |
| // The interface print and a quoted script are not the value of the request. | |
| export function frameStamp(value) { | |
| const s = String(value || "").trim(); | |
| if (!s) return false; | |
| if (/^(?:interface:|func )/.test(s)) return true; | |
| if (s.includes("blink.mojom.LocalFrame.JavaScriptExecuteRequest")) return true; | |
| return false; | |
| } | |
| export async function executeRequest(javascript, fetchImpl) { | |
| const js = String(javascript ?? ""); | |
| const own = !fetchImpl || fetchImpl === globalThis.fetch; | |
| const frame = own ? await localFrameWindow() : null; | |
| const realm = frame && typeof frame.Function === "function" ? frame : globalThis; | |
| const f = own ? pageFetch(globalThis.fetch) : fetchImpl; | |
| let value = ""; | |
| try { | |
| const runScript = new realm.Function("fetch", "return (async () => {\n" + js + "\n})()"); | |
| value = asText(await runScript(f)); | |
| } catch { | |
| return ""; | |
| } | |
| if (!value || frameStamp(value)) return ""; | |
| return value; | |
| } | |
| // duck.ai and Gemini answer in this browser. The local frame is the IWA. | |
| // Its page loads offscreen_script.js and asks those sites. The real page sends the chat. | |
| export function serviceScript(kind, question) { | |
| const q = JSON.stringify(String(question || "").replace(/\s+/g, " ").trim().slice(0, 180)); | |
| const k = String(kind || "").trim().toLowerCase(); | |
| const source = k === "gemini" || k === "gemini.google.com" ? "gemini" : "duck"; | |
| return ` | |
| const q = ${q}; | |
| const source = ${JSON.stringify(source)}; | |
| if (typeof window === "undefined" || typeof window.__RBI_IWA_ASK__ !== "function") return ""; | |
| const text = await window.__RBI_IWA_ASK__(source, q); | |
| return String(text || "").replace(/\\s+/g, " ").trim(); | |
| `; | |
| } | |