Spaces:
Running

File size: 3,703 Bytes
1d919a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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();
  `;
}