返回 DeepSeek-Reasonix
pageScripts.ts
根目录 / desktop / electron / src / main / browser / pageScripts.ts
1 import { createHash } from "node:crypto";
2 import { readFileSync } from "node:fs";
3 import { dirname, resolve } from "node:path";
4 import { fileURLToPath } from "node:url";
5
6 export type { RefInput, ResolveInput, ResolvedElement, ResolveFailure, ResolveOutput, SelectInput, SelectOutput, LocateOutput, IdentityInput } from "./pageActions.js";
7
8 export const SNAPSHOT_SCRIPT_SOURCE = "pageSnapshot";
9 export const RESOLVE_SCRIPT_SOURCE = "pageResolve";
10 export const SELECT_SCRIPT_SOURCE = "pageSelect";
11 export const FOCUS_SCRIPT_SOURCE = "pageFocus";
12 export const IDENTITY_SCRIPT_SOURCE = "pageIdentity";
13 export const LOCATE_SCRIPT_SOURCE = "pageLocate";
14
15 let cachedSource: string | undefined;
16
17 export function pageRuntimeSource(): string {
18 if (cachedSource !== undefined) return cachedSource;
19 const directory = typeof __dirname === "string" ? __dirname : resolve(dirname(fileURLToPath(import.meta.url)), "../../../dist");
20 const source = readFileSync(resolve(directory, "browser-page.js"), "utf8");
21 const manifest = JSON.parse(readFileSync(resolve(directory, "browser-page.json"), "utf8")) as { version?: number; sha256?: string };
22 if (manifest.version !== 1 || manifest.sha256 !== createHash("sha256").update(source).digest("hex")) {
23 throw new Error("browser page runtime integrity check failed");
24 }
25 cachedSource = source;
26 return source;
27 }
28
29 export function scriptCall(method: string, input: unknown): string {
30 if (!/^page[A-Za-z]+$/.test(method)) throw new Error("invalid browser page method");
31 return `/* reasonix:${method} */ (() => {\n${pageRuntimeSource()}\nreturn ReasonixPageRuntime[${JSON.stringify(method)}](${JSON.stringify(input)});\n})()`;
32 }
33
33 lines TYPESCRIPT