cursor-switch 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -75
- package/SECURITY.md +22 -19
- package/bin/cswitch.js +31 -8
- package/dist/cli.d.ts +0 -4
- package/dist/cli.js +404 -241
- package/dist/core/account-store.d.ts +0 -7
- package/dist/core/account-store.js +22 -28
- package/dist/core/cdp-engine.d.ts +11 -49
- package/dist/core/cdp-engine.js +155 -110
- package/dist/core/config-store.d.ts +7 -6
- package/dist/core/config-store.js +33 -12
- package/dist/core/cursor-paths.d.ts +8 -18
- package/dist/core/cursor-paths.js +18 -37
- package/dist/core/dashboard-launcher.d.ts +0 -20
- package/dist/core/dashboard-launcher.js +3 -53
- package/dist/core/extract-client.d.ts +57 -0
- package/dist/core/extract-client.js +219 -0
- package/dist/core/extract-menu.d.ts +3 -0
- package/dist/core/extract-menu.js +360 -0
- package/dist/core/extract-service.d.ts +64 -0
- package/dist/core/extract-service.js +435 -0
- package/dist/core/fingerprint.d.ts +0 -4
- package/dist/core/fingerprint.js +0 -6
- package/dist/core/oauth-receiver.d.ts +1 -11
- package/dist/core/oauth-receiver.js +509 -527
- package/dist/core/official-api.d.ts +0 -17
- package/dist/core/official-api.js +4 -23
- package/dist/core/outlook-launcher.d.ts +0 -31
- package/dist/core/outlook-launcher.js +194 -248
- package/dist/core/process-manager.d.ts +0 -14
- package/dist/core/process-manager.js +7 -22
- package/dist/core/pure-login-service.d.ts +54 -38
- package/dist/core/pure-login-service.js +483 -395
- package/dist/core/refund-client.d.ts +0 -22
- package/dist/core/refund-client.js +7 -27
- package/dist/core/refund-service.d.ts +0 -16
- package/dist/core/refund-service.js +8 -34
- package/dist/core/safe-storage.d.ts +27 -0
- package/dist/core/safe-storage.js +284 -0
- package/dist/core/sqlite-bridge.d.ts +10 -11
- package/dist/core/sqlite-bridge.js +112 -47
- package/dist/core/storage-bridge.d.ts +14 -5
- package/dist/core/storage-bridge.js +84 -24
- package/dist/core/switch-orchestrator.d.ts +96 -0
- package/dist/core/switch-orchestrator.js +341 -0
- package/dist/core/token-exchange.d.ts +81 -0
- package/dist/core/token-exchange.js +275 -0
- package/dist/core/token-parser.d.ts +56 -20
- package/dist/core/token-parser.js +354 -43
- package/dist/core/update-checker.d.ts +1 -29
- package/dist/core/update-checker.js +28 -43
- package/dist/index.d.ts +1 -3
- package/dist/index.js +1 -3
- package/dist/menu.d.ts +0 -7
- package/dist/menu.js +445 -548
- package/dist/types.d.ts +12 -4
- package/dist/types.js +0 -4
- package/dist/utils/account-text-importer.d.ts +9 -0
- package/dist/utils/account-text-importer.js +96 -0
- package/dist/utils/browser-utils.d.ts +0 -24
- package/dist/utils/browser-utils.js +0 -57
- package/dist/utils/clipboard.d.ts +0 -4
- package/dist/utils/clipboard.js +0 -5
- package/dist/utils/credential-bundle-ui.d.ts +36 -0
- package/dist/utils/credential-bundle-ui.js +246 -0
- package/dist/utils/live-menu.d.ts +37 -9
- package/dist/utils/live-menu.js +510 -81
- package/dist/utils/plan-formatter.d.ts +0 -26
- package/dist/utils/plan-formatter.js +10 -44
- package/dist/utils/prompt-utils.d.ts +0 -18
- package/dist/utils/prompt-utils.js +0 -23
- package/dist/utils/proxy-bootstrap.d.ts +12 -0
- package/dist/utils/proxy-bootstrap.js +141 -0
- package/dist/utils/security.d.ts +2 -4
- package/dist/utils/security.js +36 -4
- package/dist/utils/table-renderer.d.ts +0 -4
- package/dist/utils/table-renderer.js +24 -8
- package/dist/utils/time-formatter.d.ts +0 -4
- package/dist/utils/time-formatter.js +0 -8
- package/dist/utils/token-preflight-ui.d.ts +28 -0
- package/dist/utils/token-preflight-ui.js +222 -0
- package/dist/utils/window-focus.d.ts +3 -0
- package/dist/utils/window-focus.js +106 -0
- package/package.json +12 -5
- package/dist/core/welcome-offer-service.d.ts +0 -52
- package/dist/core/welcome-offer-service.js +0 -369
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 本地账号安全持久化仓库 (100% Local-First)
|
|
3
|
-
* 支持 ~/.cursor-switch/accounts.json 独立管理,且能无缝兼容同步 AoZai 数据
|
|
4
|
-
*/
|
|
5
1
|
import type { AccountInfo } from '../types.js';
|
|
6
2
|
export declare class AccountStore {
|
|
7
3
|
private dataDir;
|
|
@@ -10,9 +6,6 @@ export declare class AccountStore {
|
|
|
10
6
|
getDataDir(): string;
|
|
11
7
|
getFilePath(): string;
|
|
12
8
|
private ensureDir;
|
|
13
|
-
/**
|
|
14
|
-
* 自动探测并从外部备用目录导入历史数据
|
|
15
|
-
*/
|
|
16
9
|
autoDetectExternal(): string | null;
|
|
17
10
|
loadAll(): AccountInfo[];
|
|
18
11
|
saveAll(accounts: AccountInfo[]): void;
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 本地账号安全持久化仓库 (100% Local-First)
|
|
3
|
-
* 支持 ~/.cursor-switch/accounts.json 独立管理,且能无缝兼容同步 AoZai 数据
|
|
4
|
-
*/
|
|
5
1
|
import fs from 'node:fs';
|
|
6
2
|
import path from 'node:path';
|
|
7
3
|
import os from 'node:os';
|
|
8
4
|
import { generateMachineIds } from './fingerprint.js';
|
|
5
|
+
import { writeSecureFileSync } from '../utils/security.js';
|
|
9
6
|
export class AccountStore {
|
|
10
7
|
dataDir;
|
|
11
8
|
filePath;
|
|
@@ -28,7 +25,6 @@ export class AccountStore {
|
|
|
28
25
|
ensureDir() {
|
|
29
26
|
if (!fs.existsSync(this.dataDir)) {
|
|
30
27
|
fs.mkdirSync(this.dataDir, { recursive: true });
|
|
31
|
-
// 在 Unix/macOS 系统上设置严格的 700 权限(仅本人可读写)
|
|
32
28
|
if (process.platform !== 'win32') {
|
|
33
29
|
try {
|
|
34
30
|
fs.chmodSync(this.dataDir, 0o700);
|
|
@@ -37,9 +33,6 @@ export class AccountStore {
|
|
|
37
33
|
}
|
|
38
34
|
}
|
|
39
35
|
}
|
|
40
|
-
/**
|
|
41
|
-
* 自动探测并从外部备用目录导入历史数据
|
|
42
|
-
*/
|
|
43
36
|
autoDetectExternal() {
|
|
44
37
|
const candidates = [
|
|
45
38
|
path.join(os.homedir(), '.cursor_data', 'account_cache.json'),
|
|
@@ -54,7 +47,6 @@ export class AccountStore {
|
|
|
54
47
|
}
|
|
55
48
|
loadAll() {
|
|
56
49
|
this.ensureDir();
|
|
57
|
-
// 如果自己的 accounts.json 不存在,但检测到了 AoZai 的 account_cache.json,自动同步初始化
|
|
58
50
|
if (!fs.existsSync(this.filePath)) {
|
|
59
51
|
const ext = this.autoDetectExternal();
|
|
60
52
|
if (ext) {
|
|
@@ -71,7 +63,6 @@ export class AccountStore {
|
|
|
71
63
|
try {
|
|
72
64
|
const content = fs.readFileSync(this.filePath, 'utf8');
|
|
73
65
|
const accounts = JSON.parse(content);
|
|
74
|
-
// 确保每个账号都有绑定的专属机器指纹
|
|
75
66
|
let modified = false;
|
|
76
67
|
for (const acc of accounts) {
|
|
77
68
|
if (!acc.machine_ids || !acc.machine_ids['telemetry.devDeviceId']) {
|
|
@@ -90,17 +81,8 @@ export class AccountStore {
|
|
|
90
81
|
}
|
|
91
82
|
saveAll(accounts) {
|
|
92
83
|
this.ensureDir();
|
|
93
|
-
const tmp = `${this.filePath}.tmp_${Date.now()}`;
|
|
94
84
|
const jsonStr = JSON.stringify(accounts, null, 2);
|
|
95
|
-
|
|
96
|
-
fs.renameSync(tmp, this.filePath);
|
|
97
|
-
// 强化文件权限 600(非 Windows 平台)
|
|
98
|
-
if (process.platform !== 'win32') {
|
|
99
|
-
try {
|
|
100
|
-
fs.chmodSync(this.filePath, 0o600);
|
|
101
|
-
}
|
|
102
|
-
catch { }
|
|
103
|
-
}
|
|
85
|
+
writeSecureFileSync(this.filePath, jsonStr);
|
|
104
86
|
}
|
|
105
87
|
getByEmail(email) {
|
|
106
88
|
const list = this.loadAll();
|
|
@@ -109,11 +91,17 @@ export class AccountStore {
|
|
|
109
91
|
}
|
|
110
92
|
getByIndexOrEmail(identifier) {
|
|
111
93
|
const list = this.loadAll();
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
94
|
+
const raw = String(identifier).trim();
|
|
95
|
+
if (typeof identifier === 'number' || /^\d+$/.test(raw)) {
|
|
96
|
+
const idx = Number(raw) - 1;
|
|
97
|
+
if (idx >= 0 && idx < list.length) {
|
|
98
|
+
return list[idx];
|
|
99
|
+
}
|
|
100
|
+
if (typeof identifier === 'number') {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
115
103
|
}
|
|
116
|
-
const query =
|
|
104
|
+
const query = raw.toLowerCase();
|
|
117
105
|
return list.find((a) => a.email.toLowerCase().includes(query) || (a.username && a.username.toLowerCase().includes(query)));
|
|
118
106
|
}
|
|
119
107
|
getCurrentAccount() {
|
|
@@ -123,14 +111,11 @@ export class AccountStore {
|
|
|
123
111
|
addOrUpdate(account) {
|
|
124
112
|
const list = this.loadAll();
|
|
125
113
|
const idx = list.findIndex((a) => a.email.toLowerCase() === account.email.toLowerCase());
|
|
126
|
-
// 自动补齐专属硬件机器指纹
|
|
127
114
|
if (!account.machine_ids || !account.machine_ids['telemetry.devDeviceId']) {
|
|
128
115
|
account.machine_ids = generateMachineIds();
|
|
129
116
|
}
|
|
130
117
|
if (idx >= 0) {
|
|
131
|
-
// 保留原有机器码与创建时间
|
|
132
118
|
const existing = list[idx];
|
|
133
|
-
// 🛡️ 防御性订阅计划合并:绝不允许失败/离线时的 free 冲掉原有的团队商业版、Pro 等高价值订阅
|
|
134
119
|
let mergedSubscription = account.subscription_type;
|
|
135
120
|
const isExistingPaid = existing.subscription_type &&
|
|
136
121
|
existing.subscription_type !== 'free' &&
|
|
@@ -140,8 +125,13 @@ export class AccountStore {
|
|
|
140
125
|
mergedSubscription = existing.subscription_type;
|
|
141
126
|
}
|
|
142
127
|
}
|
|
143
|
-
// 🛡️ 防御性计费周期合并:若新传入的为空,保留原有记录的到期时间与配额数据
|
|
144
128
|
const mergedUsage = account.current_period_usage || existing.current_period_usage;
|
|
129
|
+
const mergedCheckoutLinks = existing.checkout_links || account.checkout_links
|
|
130
|
+
? {
|
|
131
|
+
...(existing.checkout_links || {}),
|
|
132
|
+
...(account.checkout_links || {}),
|
|
133
|
+
}
|
|
134
|
+
: undefined;
|
|
145
135
|
list[idx] = {
|
|
146
136
|
...existing,
|
|
147
137
|
...account,
|
|
@@ -150,7 +140,11 @@ export class AccountStore {
|
|
|
150
140
|
machine_ids: account.machine_ids || existing.machine_ids,
|
|
151
141
|
created_at: existing.created_at || account.created_at,
|
|
152
142
|
username: account.username || existing.username,
|
|
143
|
+
refresh_token: account.refresh_token || existing.refresh_token,
|
|
153
144
|
workos_cursor_session_token: account.workos_cursor_session_token || existing.workos_cursor_session_token,
|
|
145
|
+
email_password: account.email_password || existing.email_password,
|
|
146
|
+
cursor_password: account.cursor_password || existing.cursor_password,
|
|
147
|
+
checkout_links: mergedCheckoutLinks,
|
|
154
148
|
};
|
|
155
149
|
}
|
|
156
150
|
else {
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 原生零依赖 Chrome DevTools Protocol (CDP) 控制引擎
|
|
3
|
-
* 基于 Node.js 内置 HTTP 与原生 WebSocket 构建,不依赖任何第三方包
|
|
4
|
-
* 兼容 Windows / macOS / Linux / WSL 各类操作系统
|
|
5
|
-
*/
|
|
6
1
|
export interface CdpTarget {
|
|
7
2
|
id: string;
|
|
8
3
|
type: string;
|
|
@@ -14,62 +9,29 @@ export declare class CdpEngine {
|
|
|
14
9
|
private ws;
|
|
15
10
|
private messageId;
|
|
16
11
|
private responseHandlers;
|
|
12
|
+
private eventHandlers;
|
|
17
13
|
private targetWsUrl;
|
|
18
14
|
constructor(targetWsUrl: string);
|
|
19
|
-
/**
|
|
20
|
-
* 动态分配一个本地可用随机空闲端口
|
|
21
|
-
*/
|
|
22
15
|
static getFreePort(): Promise<number>;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
16
|
+
isConnected(): boolean;
|
|
17
|
+
getTargetWsUrl(): string;
|
|
18
|
+
on(method: string, handler: (params: any) => void): void;
|
|
19
|
+
off(method: string, handler: (params: any) => void): void;
|
|
20
|
+
static createTarget(port: number, url?: string): Promise<CdpTarget | null>;
|
|
21
|
+
static closeTarget(port: number, targetId: string): Promise<void>;
|
|
22
|
+
static waitForPageTarget(port: number, timeoutMs?: number, preferredUrlSubstrings?: string[]): Promise<CdpTarget | null>;
|
|
30
23
|
private static fetchJson;
|
|
31
|
-
/**
|
|
32
|
-
* 连接到页面 WebSocket CDP 端口
|
|
33
|
-
*/
|
|
34
24
|
connect(timeoutMs?: number): Promise<void>;
|
|
35
|
-
/**
|
|
36
|
-
* 发送标准 CDP JSON-RPC 消息并等待响应
|
|
37
|
-
*/
|
|
38
25
|
send<T = any>(method: string, params?: Record<string, any>): Promise<T>;
|
|
39
|
-
/**
|
|
40
|
-
* 在页面全局执行 JavaScript 表达式并返回值
|
|
41
|
-
*/
|
|
42
26
|
evaluate<T = any>(expression: string): Promise<T | undefined>;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
bringToFront(): Promise<void>;
|
|
47
|
-
/**
|
|
48
|
-
* 导航到指定 URL
|
|
49
|
-
*/
|
|
27
|
+
bringToFront(options?: {
|
|
28
|
+
focusOsWindow?: boolean;
|
|
29
|
+
}): Promise<void>;
|
|
50
30
|
navigate(url: string): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
* 获取当前页面 URL
|
|
53
|
-
*/
|
|
54
31
|
getCurrentUrl(): Promise<string>;
|
|
55
|
-
/**
|
|
56
|
-
* 获取页面 document.body.innerText
|
|
57
|
-
*/
|
|
58
32
|
getBodyText(): Promise<string>;
|
|
59
|
-
/**
|
|
60
|
-
* 触发输入框聚焦并填入文本 (兼具 CDP 原生键入流与 React/Vue/Knockout 状态同步,100% 兼容各类复杂前端)
|
|
61
|
-
*/
|
|
62
33
|
fillInput(selector: string, value: string): Promise<boolean>;
|
|
63
|
-
/**
|
|
64
|
-
* 点击匹配的元素 (结合 CDP 原生鼠标事件与 DOM 派发)
|
|
65
|
-
*/
|
|
66
34
|
click(selector: string): Promise<boolean>;
|
|
67
|
-
/**
|
|
68
|
-
* 使用原生 CDP 鼠标事件真实点击指定屏幕坐标 (具有真实 isTrusted 属性,兼容各类复杂前端框架与自定义组件)
|
|
69
|
-
*/
|
|
70
35
|
clickPoint(x: number, y: number): Promise<void>;
|
|
71
|
-
/**
|
|
72
|
-
* 优雅断开 CDP 连接,保持被托管的浏览器进程继续运行
|
|
73
|
-
*/
|
|
74
36
|
disconnect(): void;
|
|
75
37
|
}
|
package/dist/core/cdp-engine.js
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 原生零依赖 Chrome DevTools Protocol (CDP) 控制引擎
|
|
3
|
-
* 基于 Node.js 内置 HTTP 与原生 WebSocket 构建,不依赖任何第三方包
|
|
4
|
-
* 兼容 Windows / macOS / Linux / WSL 各类操作系统
|
|
5
|
-
*/
|
|
6
1
|
import http from 'node:http';
|
|
7
2
|
import net from 'node:net';
|
|
3
|
+
import { bringOsBrowserWindowToFront, extractLoopbackCdpPort } from '../utils/window-focus.js';
|
|
8
4
|
export class CdpEngine {
|
|
9
5
|
ws = null;
|
|
10
6
|
messageId = 1;
|
|
11
7
|
responseHandlers = new Map();
|
|
8
|
+
eventHandlers = new Map();
|
|
12
9
|
targetWsUrl;
|
|
13
10
|
constructor(targetWsUrl) {
|
|
14
11
|
this.targetWsUrl = targetWsUrl;
|
|
15
12
|
}
|
|
16
|
-
/**
|
|
17
|
-
* 动态分配一个本地可用随机空闲端口
|
|
18
|
-
*/
|
|
19
13
|
static getFreePort() {
|
|
20
14
|
return new Promise((resolve, reject) => {
|
|
21
15
|
const server = net.createServer();
|
|
@@ -32,38 +26,96 @@ export class CdpEngine {
|
|
|
32
26
|
server.on('error', reject);
|
|
33
27
|
});
|
|
34
28
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
isConnected() {
|
|
30
|
+
return Boolean(this.ws && this.ws.readyState === 1);
|
|
31
|
+
}
|
|
32
|
+
getTargetWsUrl() {
|
|
33
|
+
return this.targetWsUrl;
|
|
34
|
+
}
|
|
35
|
+
on(method, handler) {
|
|
36
|
+
let set = this.eventHandlers.get(method);
|
|
37
|
+
if (!set) {
|
|
38
|
+
set = new Set();
|
|
39
|
+
this.eventHandlers.set(method, set);
|
|
40
|
+
}
|
|
41
|
+
set.add(handler);
|
|
42
|
+
}
|
|
43
|
+
off(method, handler) {
|
|
44
|
+
const set = this.eventHandlers.get(method);
|
|
45
|
+
if (!set)
|
|
46
|
+
return;
|
|
47
|
+
set.delete(handler);
|
|
48
|
+
if (set.size === 0) {
|
|
49
|
+
this.eventHandlers.delete(method);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
static async createTarget(port, url = 'about:blank') {
|
|
53
|
+
const endpoint = `http://127.0.0.1:${port}/json/new?${encodeURIComponent(url)}`;
|
|
54
|
+
const doRequest = (method) => new Promise((resolve, reject) => {
|
|
55
|
+
const req = http.request(endpoint, { method }, (res) => {
|
|
56
|
+
let buf = '';
|
|
57
|
+
res.on('data', (c) => (buf += c));
|
|
58
|
+
res.on('end', () => resolve({ status: res.statusCode || 0, body: buf }));
|
|
59
|
+
res.on('error', reject);
|
|
60
|
+
});
|
|
61
|
+
req.on('error', reject);
|
|
62
|
+
req.end();
|
|
63
|
+
});
|
|
64
|
+
for (const method of ['PUT', 'GET']) {
|
|
65
|
+
try {
|
|
66
|
+
const res = await doRequest(method);
|
|
67
|
+
if (res.status >= 200 && res.status < 300 && res.body) {
|
|
68
|
+
return JSON.parse(res.body);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch { }
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
static async closeTarget(port, targetId) {
|
|
76
|
+
if (!targetId)
|
|
77
|
+
return;
|
|
78
|
+
const endpoint = `http://127.0.0.1:${port}/json/close/${encodeURIComponent(targetId)}`;
|
|
79
|
+
await new Promise((resolve) => {
|
|
80
|
+
http
|
|
81
|
+
.get(endpoint, (res) => {
|
|
82
|
+
res.resume();
|
|
83
|
+
resolve();
|
|
84
|
+
})
|
|
85
|
+
.on('error', () => resolve());
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
static async waitForPageTarget(port, timeoutMs = 15000, preferredUrlSubstrings) {
|
|
39
89
|
const deadline = Date.now() + timeoutMs;
|
|
40
90
|
while (Date.now() < deadline) {
|
|
41
91
|
try {
|
|
42
92
|
const targets = await this.fetchJson(`http://127.0.0.1:${port}/json/list`);
|
|
43
93
|
if (Array.isArray(targets) && targets.length > 0) {
|
|
44
|
-
|
|
45
|
-
const page = targets.find((t) => t.type === 'page' &&
|
|
94
|
+
const validPages = targets.filter((t) => t.type === 'page' &&
|
|
46
95
|
t.webSocketDebuggerUrl &&
|
|
47
96
|
!t.url.startsWith('chrome-extension://') &&
|
|
48
97
|
!t.url.startsWith('edge-extension://'));
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
|
|
98
|
+
if (preferredUrlSubstrings && preferredUrlSubstrings.length > 0 && validPages.length > 0) {
|
|
99
|
+
for (const sub of preferredUrlSubstrings) {
|
|
100
|
+
const targetSub = sub.toLowerCase();
|
|
101
|
+
const preferred = validPages.find((t) => (t.url || '').toLowerCase().includes(targetSub));
|
|
102
|
+
if (preferred)
|
|
103
|
+
return preferred;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (validPages.length > 0)
|
|
107
|
+
return validPages[0];
|
|
52
108
|
const anyPage = targets.find((t) => t.type === 'page' && t.webSocketDebuggerUrl);
|
|
53
109
|
if (anyPage)
|
|
54
110
|
return anyPage;
|
|
55
111
|
}
|
|
56
112
|
}
|
|
57
113
|
catch {
|
|
58
|
-
// 浏览器尚未完全启动或端口未就绪,继续重试
|
|
59
114
|
}
|
|
60
115
|
await new Promise((r) => setTimeout(r, 250));
|
|
61
116
|
}
|
|
62
117
|
return null;
|
|
63
118
|
}
|
|
64
|
-
/**
|
|
65
|
-
* 原生 HTTP GET 获取 JSON 数据
|
|
66
|
-
*/
|
|
67
119
|
static fetchJson(url) {
|
|
68
120
|
return new Promise((resolve, reject) => {
|
|
69
121
|
http
|
|
@@ -82,13 +134,10 @@ export class CdpEngine {
|
|
|
82
134
|
.on('error', reject);
|
|
83
135
|
});
|
|
84
136
|
}
|
|
85
|
-
/**
|
|
86
|
-
* 连接到页面 WebSocket CDP 端口
|
|
87
|
-
*/
|
|
88
137
|
async connect(timeoutMs = 10000) {
|
|
89
138
|
const WebSocketImpl = globalThis.WebSocket;
|
|
90
139
|
if (!WebSocketImpl) {
|
|
91
|
-
throw new Error('当前 Node.js 运行环境缺少原生 WebSocket 支持 (建议 Node >=
|
|
140
|
+
throw new Error('当前 Node.js 运行环境缺少原生 WebSocket 支持 (建议 Node >= 22.5.0)');
|
|
92
141
|
}
|
|
93
142
|
return new Promise((resolve, reject) => {
|
|
94
143
|
const timer = setTimeout(() => {
|
|
@@ -118,15 +167,24 @@ export class CdpEngine {
|
|
|
118
167
|
handler(msg.result);
|
|
119
168
|
}
|
|
120
169
|
}
|
|
170
|
+
else if (!msg.id && msg.method && this.eventHandlers.has(msg.method)) {
|
|
171
|
+
const listeners = Array.from(this.eventHandlers.get(msg.method));
|
|
172
|
+
for (const fn of listeners) {
|
|
173
|
+
try {
|
|
174
|
+
fn(msg.params);
|
|
175
|
+
}
|
|
176
|
+
catch { }
|
|
177
|
+
}
|
|
178
|
+
}
|
|
121
179
|
}
|
|
122
180
|
catch { }
|
|
123
181
|
};
|
|
124
182
|
this.ws.onclose = () => {
|
|
125
|
-
// 清空等待响应的处理器
|
|
126
183
|
for (const handler of this.responseHandlers.values()) {
|
|
127
184
|
handler(null, new Error('CDP 连接已关闭'));
|
|
128
185
|
}
|
|
129
186
|
this.responseHandlers.clear();
|
|
187
|
+
this.eventHandlers.clear();
|
|
130
188
|
};
|
|
131
189
|
}
|
|
132
190
|
catch (e) {
|
|
@@ -135,9 +193,6 @@ export class CdpEngine {
|
|
|
135
193
|
}
|
|
136
194
|
});
|
|
137
195
|
}
|
|
138
|
-
/**
|
|
139
|
-
* 发送标准 CDP JSON-RPC 消息并等待响应
|
|
140
|
-
*/
|
|
141
196
|
send(method, params = {}) {
|
|
142
197
|
if (!this.ws || this.ws.readyState !== 1) {
|
|
143
198
|
return Promise.reject(new Error('CDP WebSocket 未处于连接状态'));
|
|
@@ -155,9 +210,6 @@ export class CdpEngine {
|
|
|
155
210
|
this.ws.send(JSON.stringify({ id, method, params }));
|
|
156
211
|
});
|
|
157
212
|
}
|
|
158
|
-
/**
|
|
159
|
-
* 在页面全局执行 JavaScript 表达式并返回值
|
|
160
|
-
*/
|
|
161
213
|
async evaluate(expression) {
|
|
162
214
|
try {
|
|
163
215
|
const res = await this.send('Runtime.evaluate', {
|
|
@@ -171,115 +223,110 @@ export class CdpEngine {
|
|
|
171
223
|
return undefined;
|
|
172
224
|
}
|
|
173
225
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
226
|
+
async bringToFront(options = {}) {
|
|
227
|
+
let titleHint = '';
|
|
228
|
+
try {
|
|
229
|
+
const info = await this.send('Target.getTargetInfo');
|
|
230
|
+
titleHint = String(info?.targetInfo?.title || '').trim();
|
|
231
|
+
const targetId = info?.targetInfo?.targetId;
|
|
232
|
+
const win = await this.send('Browser.getWindowForTarget', targetId ? { targetId } : {});
|
|
233
|
+
if (win?.windowId && win?.bounds?.windowState === 'minimized') {
|
|
234
|
+
await this.send('Browser.setWindowBounds', {
|
|
235
|
+
windowId: win.windowId,
|
|
236
|
+
bounds: { windowState: 'normal' },
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
catch { }
|
|
178
241
|
try {
|
|
179
242
|
await this.send('Page.bringToFront');
|
|
180
243
|
}
|
|
181
244
|
catch { }
|
|
245
|
+
if (options.focusOsWindow !== false) {
|
|
246
|
+
const port = extractLoopbackCdpPort(this.targetWsUrl);
|
|
247
|
+
if (port) {
|
|
248
|
+
await bringOsBrowserWindowToFront(port, titleHint);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
182
251
|
}
|
|
183
|
-
/**
|
|
184
|
-
* 导航到指定 URL
|
|
185
|
-
*/
|
|
186
252
|
async navigate(url) {
|
|
187
253
|
await this.send('Page.navigate', { url });
|
|
188
254
|
}
|
|
189
|
-
/**
|
|
190
|
-
* 获取当前页面 URL
|
|
191
|
-
*/
|
|
192
255
|
async getCurrentUrl() {
|
|
193
256
|
const url = await this.evaluate('window.location.href');
|
|
194
257
|
return url || '';
|
|
195
258
|
}
|
|
196
|
-
/**
|
|
197
|
-
* 获取页面 document.body.innerText
|
|
198
|
-
*/
|
|
199
259
|
async getBodyText() {
|
|
200
260
|
const text = await this.evaluate('document.body ? document.body.innerText : ""');
|
|
201
261
|
return text || '';
|
|
202
262
|
}
|
|
203
|
-
/**
|
|
204
|
-
* 触发输入框聚焦并填入文本 (兼具 CDP 原生键入流与 React/Vue/Knockout 状态同步,100% 兼容各类复杂前端)
|
|
205
|
-
*/
|
|
206
263
|
async fillInput(selector, value) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
if (
|
|
214
|
-
|
|
215
|
-
el.
|
|
216
|
-
el.
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
return true;
|
|
264
|
+
const focused = await this.evaluate(`(() => {
|
|
265
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
266
|
+
if (!el || el.disabled) return false;
|
|
267
|
+
const r = el.getBoundingClientRect();
|
|
268
|
+
const style = window.getComputedStyle(el);
|
|
269
|
+
if (style.display === 'none' || style.visibility === 'hidden') return false;
|
|
270
|
+
if (r.width === 0 && r.height === 0 && el.getClientRects().length === 0) return false;
|
|
271
|
+
el.scrollIntoView({ block: 'center' });
|
|
272
|
+
el.focus();
|
|
273
|
+
if (typeof el.select === 'function') {
|
|
274
|
+
el.select();
|
|
275
|
+
}
|
|
276
|
+
return true;
|
|
221
277
|
})()`);
|
|
222
278
|
if (!focused)
|
|
223
279
|
return false;
|
|
224
|
-
// 2. 优先通过 CDP Input.insertText 真实键入,生成具有真实 isTrusted 与原生 Keyboard/Input 流
|
|
225
280
|
try {
|
|
226
281
|
await this.send('Input.insertText', { text: value });
|
|
227
282
|
}
|
|
228
283
|
catch { }
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
if (
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
el.dispatchEvent(new Event('
|
|
246
|
-
|
|
247
|
-
return true;
|
|
284
|
+
await this.evaluate(`(() => {
|
|
285
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
286
|
+
if (!el) return false;
|
|
287
|
+
if (el.value !== ${JSON.stringify(value)}) {
|
|
288
|
+
try {
|
|
289
|
+
const nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set;
|
|
290
|
+
if (nativeSetter) {
|
|
291
|
+
nativeSetter.call(el, ${JSON.stringify(value)});
|
|
292
|
+
} else {
|
|
293
|
+
el.value = ${JSON.stringify(value)};
|
|
294
|
+
}
|
|
295
|
+
} catch {
|
|
296
|
+
el.value = ${JSON.stringify(value)};
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
300
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
301
|
+
return true;
|
|
248
302
|
})()`);
|
|
249
303
|
return true;
|
|
250
304
|
}
|
|
251
|
-
/**
|
|
252
|
-
* 点击匹配的元素 (结合 CDP 原生鼠标事件与 DOM 派发)
|
|
253
|
-
*/
|
|
254
305
|
async click(selector) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
el.
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
|
|
306
|
+
const point = await this.evaluate(`(() => {
|
|
307
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
308
|
+
if (!el || el.disabled) return null;
|
|
309
|
+
const style = window.getComputedStyle(el);
|
|
310
|
+
if (style.display === 'none' || style.visibility === 'hidden') return null;
|
|
311
|
+
el.scrollIntoView({ block: 'center' });
|
|
312
|
+
const r = el.getBoundingClientRect();
|
|
313
|
+
if (r.width <= 0 || r.height <= 0) return null;
|
|
314
|
+
return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
|
|
265
315
|
})()`);
|
|
266
316
|
if (point && typeof point.x === 'number' && typeof point.y === 'number') {
|
|
267
317
|
await this.clickPoint(point.x, point.y);
|
|
318
|
+
return true;
|
|
268
319
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
return true;
|
|
320
|
+
const clicked = await this.evaluate(`(() => {
|
|
321
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
322
|
+
if (!el || el.disabled) return false;
|
|
323
|
+
const style = window.getComputedStyle(el);
|
|
324
|
+
if (style.display === 'none' || style.visibility === 'hidden') return false;
|
|
325
|
+
el.click();
|
|
326
|
+
return true;
|
|
277
327
|
})()`);
|
|
278
|
-
return Boolean(
|
|
328
|
+
return Boolean(clicked);
|
|
279
329
|
}
|
|
280
|
-
/**
|
|
281
|
-
* 使用原生 CDP 鼠标事件真实点击指定屏幕坐标 (具有真实 isTrusted 属性,兼容各类复杂前端框架与自定义组件)
|
|
282
|
-
*/
|
|
283
330
|
async clickPoint(x, y) {
|
|
284
331
|
try {
|
|
285
332
|
await this.send('Input.dispatchMouseEvent', {
|
|
@@ -304,9 +351,6 @@ export class CdpEngine {
|
|
|
304
351
|
}
|
|
305
352
|
catch { }
|
|
306
353
|
}
|
|
307
|
-
/**
|
|
308
|
-
* 优雅断开 CDP 连接,保持被托管的浏览器进程继续运行
|
|
309
|
-
*/
|
|
310
354
|
disconnect() {
|
|
311
355
|
if (this.ws) {
|
|
312
356
|
try {
|
|
@@ -316,5 +360,6 @@ export class CdpEngine {
|
|
|
316
360
|
this.ws = null;
|
|
317
361
|
}
|
|
318
362
|
this.responseHandlers.clear();
|
|
363
|
+
this.eventHandlers.clear();
|
|
319
364
|
}
|
|
320
365
|
}
|
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 用户本地偏好配置仓库
|
|
3
|
-
* 存储路径: ~/.cursor-switch/config.json
|
|
4
|
-
* 用于记忆常用卡密、接口地址与个性化选项
|
|
5
|
-
*/
|
|
6
1
|
export interface AppConfig {
|
|
7
2
|
seat_card_code?: string;
|
|
8
3
|
token_card_code?: string;
|
|
4
|
+
extract_card_code?: string;
|
|
9
5
|
seat_api_base?: string;
|
|
10
6
|
auto_restart_cursor?: boolean;
|
|
11
7
|
last_update_check?: number;
|
|
12
8
|
latest_version?: string;
|
|
13
9
|
custom_executable_path?: string;
|
|
14
10
|
custom_data_dir?: string;
|
|
11
|
+
preferred_copy_format?: 'single' | 'triple';
|
|
15
12
|
}
|
|
16
13
|
export declare class ConfigStore {
|
|
17
14
|
private configDir;
|
|
18
15
|
private configPath;
|
|
19
|
-
constructor();
|
|
16
|
+
constructor(customPath?: string);
|
|
20
17
|
private ensureDir;
|
|
21
18
|
load(): AppConfig;
|
|
22
19
|
save(config: AppConfig): void;
|
|
@@ -24,6 +21,8 @@ export declare class ConfigStore {
|
|
|
24
21
|
setSeatCardCode(code: string): void;
|
|
25
22
|
getTokenCardCode(): string | null;
|
|
26
23
|
setTokenCardCode(code: string): void;
|
|
24
|
+
getExtractCardCode(): string | null;
|
|
25
|
+
setExtractCardCode(code: string): void;
|
|
27
26
|
getSeatApiBase(): string;
|
|
28
27
|
setSeatApiBase(url: string): void;
|
|
29
28
|
getUpdateCache(): {
|
|
@@ -35,4 +34,6 @@ export declare class ConfigStore {
|
|
|
35
34
|
setCustomExecutablePath(pathStr: string): void;
|
|
36
35
|
getCustomDataDir(): string | null;
|
|
37
36
|
setCustomDataDir(dirStr: string): void;
|
|
37
|
+
getPreferredCopyFormat(): 'single' | 'triple';
|
|
38
|
+
setPreferredCopyFormat(format: 'single' | 'triple'): void;
|
|
38
39
|
}
|