Skip to content

Commit 19a231b

Browse files
committed
fix: executeScript 改用 MAIN world,DOM 操作改用 ISOLATED world
- executeScript: 动态代码固定 MAIN world(ISOLATED 下 new Function 被 MV3 CSP 拦截) - click/fill/scroll/waitFor/readPage: 静态函数改为 ISOLATED world(纯 DOM 操作无需 eval) - execute_script tool: 移除 world 参数,不再暴露给用户 - ExecuteScriptOptions: 移除 world 字段 - scriptcat.d.ts / scriptcat.zh-CN.d.ts: 同步更新类型定义
1 parent 250db4f commit 19a231b

5 files changed

Lines changed: 20 additions & 27 deletions

File tree

src/app/service/agent/tools/execute_script.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ export const EXECUTE_SCRIPT_DEFINITION: ToolDefinition = {
55
name: "execute_script",
66
description:
77
"Execute JavaScript code. " +
8-
"target='page': run in a browser tab with DOM access. world param (page only): " +
9-
"ISOLATED (default) — extension-isolated context, can fetch extension blob URLs (blob:chrome-extension://...) AND manipulate page DOM, ideal for bridging OPFS files to page operations; " +
10-
"MAIN — shares page's window/globals (access page JS variables, call page functions), but cannot access extension URLs. " +
11-
"target='sandbox': isolated computation environment, no DOM, no world param.",
8+
"target='page': run in a browser tab with DOM access, shares page's window/globals (can access page JS variables, call page functions). " +
9+
"target='sandbox': isolated computation environment, no DOM.",
1210
parameters: {
1311
type: "object",
1412
properties: {
@@ -22,12 +20,6 @@ export const EXECUTE_SCRIPT_DEFINITION: ToolDefinition = {
2220
type: "number",
2321
description: "Target tab ID for page execution. Defaults to active tab. Ignored for sandbox.",
2422
},
25-
world: {
26-
type: "string",
27-
enum: ["MAIN", "ISOLATED"],
28-
description:
29-
"JS execution world for page target. MAIN shares page globals, ISOLATED is extension-isolated. Default: ISOLATED. Ignored for sandbox.",
30-
},
3123
},
3224
required: ["code", "target"],
3325
},
@@ -47,7 +39,7 @@ function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
4739
export type ExecuteScriptDeps = {
4840
executeInPage: (
4941
code: string,
50-
options?: { tabId?: number; world?: "MAIN" | "ISOLATED" }
42+
options?: { tabId?: number }
5143
) => Promise<{ result: unknown; tabId: number }>;
5244
executeInSandbox: (code: string) => Promise<unknown>;
5345
timeoutMs?: number; // 可选超时(ms),默认 30s,测试用
@@ -76,8 +68,7 @@ export function createExecuteScriptTool(deps: ExecuteScriptDeps): {
7668

7769
if (target === "page") {
7870
const tabId = args.tab_id as number | undefined;
79-
const world = (args.world as "MAIN" | "ISOLATED" | undefined) || undefined;
80-
const { result, tabId: actualTabId } = await withTimeout(deps.executeInPage(code, { tabId, world }), timeoutMs);
71+
const { result, tabId: actualTabId } = await withTimeout(deps.executeInPage(code, { tabId }), timeoutMs);
8172
return JSON.stringify({ result: result ?? null, target: "page", tab_id: actualTabId });
8273
}
8374

src/app/service/agent/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export type ChatStreamEvent =
114114
durationMs?: number;
115115
}
116116
| { type: "error"; message: string; errorCode?: string }
117+
| { type: "retry"; attempt: number; maxRetries: number; error: string; delayMs: number }
117118
| { type: "compact_done"; summary: string; originalCount: number }
118119
| {
119120
type: "sync";
@@ -160,7 +161,8 @@ export type AgentModelSafeConfig = Omit<AgentModelConfig, "apiKey">;
160161
export type ModelApiRequest =
161162
| { action: "list"; scriptUuid: string }
162163
| { action: "get"; id: string; scriptUuid: string }
163-
| { action: "getDefault"; scriptUuid: string };
164+
| { action: "getDefault"; scriptUuid: string }
165+
| { action: "getSummary"; scriptUuid: string };
164166

165167
// ---- CAT.agent.conversation 用户脚本 API 类型 ----
166168

@@ -423,7 +425,6 @@ export type WaitForResult = {
423425
// GM API 请求类型
424426
export type ExecuteScriptOptions = {
425427
tabId?: number;
426-
world?: "MAIN" | "ISOLATED";
427428
};
428429

429430
export type MonitorResult = {

src/app/service/service_worker/agent_dom.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class AgentDomService {
9191
target: { tabId },
9292
func: readPageContent,
9393
args: [{ selector, maxLength, removeTags } as ReadPageInjectedOptions],
94-
world: "MAIN",
94+
world: "ISOLATED",
9595
});
9696

9797
if (!results || results.length === 0) {
@@ -200,7 +200,7 @@ export class AgentDomService {
200200
target: { tabId },
201201
func: executeScroll,
202202
args: [direction, selector || null],
203-
world: "MAIN",
203+
world: "ISOLATED",
204204
});
205205

206206
if (!results || results.length === 0) {
@@ -222,7 +222,7 @@ export class AgentDomService {
222222
target: { tabId },
223223
func: checkElement,
224224
args: [selector],
225-
world: "MAIN",
225+
world: "ISOLATED",
226226
});
227227

228228
if (results?.[0]?.result) {
@@ -235,10 +235,9 @@ export class AgentDomService {
235235
return { found: false };
236236
}
237237

238-
// 在页面中执行 JavaScript 代码
238+
// 在页面中执行 JavaScript 代码(动态代码必须跑 MAIN world,ISOLATED 会被扩展 CSP 拦截 new Function)
239239
async executeScript(code: string, options?: ExecuteScriptOptions): Promise<{ result: unknown; tabId: number }> {
240240
const tabId = await this.resolveTabId(options?.tabId);
241-
const world = options?.world || "ISOLATED";
242241

243242
const results = await chrome.scripting.executeScript({
244243
target: { tabId },
@@ -248,7 +247,7 @@ export class AgentDomService {
248247
return fn();
249248
},
250249
args: [code],
251-
world,
250+
world: "MAIN",
252251
});
253252

254253
if (!results || results.length === 0) {
@@ -398,7 +397,7 @@ export class AgentDomService {
398397
(el as HTMLElement).click();
399398
},
400399
args: [selector],
401-
world: "MAIN",
400+
world: "ISOLATED",
402401
});
403402

404403
// 等待页面稳定
@@ -431,7 +430,7 @@ export class AgentDomService {
431430
el.dispatchEvent(new Event("change", { bubbles: true }));
432431
},
433432
args: [selector, value],
434-
world: "MAIN",
433+
world: "ISOLATED",
435434
});
436435

437436
return {

src/types/scriptcat.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,8 +1209,6 @@ declare namespace CATAgentDom {
12091209
interface ExecuteScriptOptions {
12101210
/** Target tab ID. */
12111211
tabId?: number;
1212-
/** Script execution world. MAIN shares page globals, ISOLATED is extension-isolated. Default: ISOLATED. */
1213-
world?: "MAIN" | "ISOLATED";
12141212
}
12151213

12161214
/** Result of `stopMonitor()` — collected DOM changes during monitoring. */
@@ -1492,6 +1490,9 @@ declare namespace CATAgentModel {
14921490

14931491
/** Get the default model ID. Returns empty string if none set. */
14941492
getDefault(): Promise<string>;
1493+
1494+
/** Get the summary (lightweight) model ID. Returns empty string if none set. */
1495+
getSummary(): Promise<string>;
14951496
}
14961497
}
14971498

src/types/scriptcat.zh-CN.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,6 @@ declare namespace CATAgentDom {
12161216
interface ExecuteScriptOptions {
12171217
/** 目标标签页 ID。 */
12181218
tabId?: number;
1219-
/** 脚本执行世界。MAIN 共享页面全局变量,ISOLATED 为扩展隔离环境。默认:ISOLATED。 */
1220-
world?: "MAIN" | "ISOLATED";
12211219
}
12221220

12231221
/** `stopMonitor()` 的结果 — 监控期间收集的 DOM 变更。 */
@@ -1499,6 +1497,9 @@ declare namespace CATAgentModel {
14991497

15001498
/** 获取默认模型 ID。未设置时返回空字符串。 */
15011499
getDefault(): Promise<string>;
1500+
1501+
/** 获取摘要(轻量)模型 ID。未设置时返回空字符串。 */
1502+
getSummary(): Promise<string>;
15021503
}
15031504
}
15041505

0 commit comments

Comments
 (0)