page-recorder-mcp 1.9.0 → 1.9.1
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 +6 -1
- package/bridge/pack.js +137 -0
- package/bridge/server.js +35 -2
- package/bridge/updater.js +77 -16
- package/manifest.json +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -18,6 +18,10 @@ npx -y page-recorder-mcp@latest extension # 生成浏览器扩展,默认
|
|
|
18
18
|
|
|
19
19
|
从 1.5.0 或更早版本升上来时手动做最后一次,目录写你现在加载的那个:`npx -y page-recorder-mcp@latest extension <现有扩展目录>`,然后在扩展卡片上点「重新加载」,**重启 AI 工具**(旧的后台桥接退出后,备份和自动更新才生效),并**刷新已打开的页面**(否则这些页面录不到接口的返回内容)。
|
|
20
20
|
|
|
21
|
+
**装进 HubStudio 等指纹浏览器**:它们的「上传扩展」只收安装包,不能选文件夹。运行 `npx -y page-recorder-mcp@latest pack`,在当前目录生成 `page-recorder-<版本>.crx` 和 `.zip`(可以跟一个目录参数)。HubStudio 在环境列表进「扩展管理」→「团队扩展」→ 上传扩展,名称随便填,安装文件选 `.crx`;上传后默认所有环境都装,只给部分环境用就关掉「全局使用」再分配。HubStudio 环境默认开着「端口扫描保护」,会拦掉扩展连本机桥接(扩展弹窗一直显示连不上):在环境的「编辑」里找到「端口扫描保护」,保持开启,在下面「允许被扫描的本地网络端口」里填 `17321`(改过桥接端口就填改后的)。改完或者装扩展时环境已经开着的,都要重启环境,装好后刷新要录的页面。按打包扩展装的,不用开「开发者模式」。
|
|
22
|
+
|
|
23
|
+
crx 用 `~/.page-recorder/extension-key.pem` 签名(第一次打包时生成),扩展 ID 由它决定,打包时就显示出来,桥接也已放行,不用再运行 `trust`。以后打新版一定用同一个私钥:扩展 ID 不变,装上的还是同一个扩展;在别的电脑上打包就把这个文件复制过去,加 `--key <私钥文件>`。团队扩展装到别的电脑(比如同事的 Windows)的 HubStudio 里,那边照旧用原来的 MCP 配置,不用另外操作:桥接会在 HubStudio 的环境目录里找到它(客户端里改过缓存目录也找得到),核对代码和这一版一样就放行。扩展代码有改动的新版发出来后,要重新 `pack` 上传,桥接日志里会提示。还是连不上时(别的指纹浏览器、环境目录放在别处),按扩展弹窗里显示的 ID 运行 `npx page-recorder-mcp trust <扩展ID>`。只收 zip 的工具用 `.zip`:包里的 `manifest.json` 写了公钥,解开按已解压扩展加载时扩展 ID 也是这个;那边重新签名的话 ID 会变,同样按弹窗里的 ID 放行。指纹浏览器里装的扩展**不会自动更新**:有新版时重新运行 `pack`,上传新的 `.crx`。
|
|
24
|
+
|
|
21
25
|
**脚本不会因为升级丢失**:连上桥接后,脚本库自动同步到 `~/.page-recorder/library/`,每个浏览器资料一个目录,互不影响。备份和录制包一样脱敏:不含 AI Key 和密码框的内容(关了密码脱敏录的脚本恢复后要重新填密码),地址里的令牌、签名和响应里的敏感字段也不保留,起始地址带这类参数的脚本恢复后可能要重新录制。这个目录不在扩展文件夹和 npm 缓存里。卸载或换目录重装扩展后,打开「脚本库」页点「恢复到这个浏览器」就能找回;换了目录的按导入的录制包对待,部分需要页面签名的接口要用页面回放。录完还没连过桥接的脚本只在扩展里,卸载前先启动一次桥接。
|
|
22
26
|
|
|
23
27
|
## 录制
|
|
@@ -237,7 +241,7 @@ REST 接口(`/api/recordings/…/run`、`/call`)照旧返回完整的原始
|
|
|
237
241
|
|
|
238
242
|
**局域网里的另一台电脑读共享脚本**:在「脚本库 → 局域网」打开开关,桥接临时监听局域网端口(默认 17322,被占了往后找,`PR_LAN_PORT` 可改),弹窗显示这台电脑的地址和 16 位口令。另一台电脑在同一处填上地址和口令点「读取」,就能看到这边点了「共享」的脚本,点「接收」复制一份进自己的脚本库;之后两边各管各的,要更新就再接收一次(会覆盖那边的同一条)。请求和返回都用口令加密,局域网里抓包看不到内容,没有口令连脚本列表都拿不到,口令连错 10 次先拒绝一分钟;两边都只认局域网地址(192.168.x.x、10.x.x.x、172.16–31.x.x、Tailscale 的 100.64–127.x.x),不支持 IPv6。不知道对方是不是自己的电脑,所以接收来的脚本和别人给的录制包一样按导入的对待(部分需要页面签名的接口要用页面回放),起始地址、跳转和接口只能是 http(s) 地址。开关只在这次浏览器会话里有效:开开关的浏览器关掉(断开超过一分钟)端口就关,下次打开浏览器要重新打开;在任何一个浏览器里关掉就全关;桥接退出(AI 工具都关了)时端口也关。第一次打开时系统可能问是否允许 node 接收连接,选允许(Windows 选「专用网络」)。
|
|
239
243
|
|
|
240
|
-
**桥接只认 Page Recorder 扩展**:本机装的任何扩展都连得上 `127.0.0.1:17321`,连上就能收到 AI 发来的参数(包括密码)、读写脚本库备份,所以桥接要核对是谁:每个连上来的扩展(第一个也一样),桥接都要找到它的安装目录才放行,认过的记下来,下次不再核对。认的目录有两种:`extension` 子命令生成的目录、默认目录 `~/page-recorder-extension`
|
|
244
|
+
**桥接只认 Page Recorder 扩展**:本机装的任何扩展都连得上 `127.0.0.1:17321`,连上就能收到 AI 发来的参数(包括密码)、读写脚本库备份,所以桥接要核对是谁:每个连上来的扩展(第一个也一样),桥接都要找到它的安装目录才放行,认过的记下来,下次不再核对。认的目录有两种:`extension` 子命令生成的目录、默认目录 `~/page-recorder-extension` 这类桥接自己管的目录;或者浏览器资料里登记的已解压扩展目录,但里面的扩展文件要和当前版本逐个一样(照抄名字、改了代码的过不了)。按 crx 装的(`pack` 打的包传进 HubStudio)也一样核对:浏览器安装时会往 `manifest.json` 里写公钥、重新编码图标,所以 manifest 只许多出公钥(版本号不比),公钥算出来的 ID 要对得上,图标不比,代码文件逐个一样;打包的那台电脑在打包时就放行了这个 ID。指纹浏览器这类自定义资料目录找不到时,扩展弹窗的桥接状态会显示它的 ID,确认是自己装的就在终端运行 `npx page-recorder-mcp trust <扩展ID>` 放行(或者把资料目录写进环境变量 `PR_BROWSER_DATA_DIRS`)。启动参数里给了 `--extension-id`,或者设了 `--token`,就按那个来,不做这项核对。
|
|
241
245
|
|
|
242
246
|
反过来,扩展不核对连上的是不是你的桥接:本机有程序抢先占住 17321 端口,扩展就会连上它、执行它发来的回放和调用。这一点不防,因为能这么做的一般是以你的身份运行的程序,它本来就能直接读你的文件和浏览器 Cookie。`--token` 防的是别的程序冒充扩展连桥接,防不了冒充桥接(口令是扩展连接时带过去的)。多人共用一台电脑时,不用的时候在扩展「设置」里关掉桥接连接。
|
|
243
247
|
|
|
@@ -297,6 +301,7 @@ bridge/library.js 脚本库同步到本机目录,重装后恢复
|
|
|
297
301
|
bridge/shared.js 同一台电脑上几个环境之间共享脚本
|
|
298
302
|
bridge/lan.js 局域网里另一台电脑凭口令读共享脚本
|
|
299
303
|
bridge/updater.js 自动更新扩展目录和技能
|
|
304
|
+
bridge/pack.js 打包成 .crx / .zip,给指纹浏览器「上传扩展」用
|
|
300
305
|
```
|
|
301
306
|
|
|
302
307
|
## 已知限制
|
package/bridge/pack.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// 打包成 .crx / .zip:HubStudio 等指纹浏览器的「上传扩展」只收安装包,不能选已解压的目录。
|
|
2
|
+
// crx 用本机私钥签名(CRX3),扩展 ID 由公钥决定:以后打新版用同一个私钥,装上后还是同一个扩展,脚本不丢。
|
|
3
|
+
// 零依赖:zip 自己写(deflate 用 zlib),签名用 node:crypto
|
|
4
|
+
'use strict';
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const zlib = require('zlib');
|
|
8
|
+
const crypto = require('crypto');
|
|
9
|
+
const { extensionIdOfKey } = require('./updater');
|
|
10
|
+
|
|
11
|
+
const CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
|
|
12
|
+
let c = n;
|
|
13
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
14
|
+
return c >>> 0;
|
|
15
|
+
});
|
|
16
|
+
function crc32(buf) {
|
|
17
|
+
let c = 0xffffffff;
|
|
18
|
+
for (const b of buf) c = CRC_TABLE[(c ^ b) & 0xff] ^ (c >>> 8);
|
|
19
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
20
|
+
}
|
|
21
|
+
// 文件时间固定写 2000-01-01:同一版打出来的包一样
|
|
22
|
+
const DOS_TIME = 0;
|
|
23
|
+
const DOS_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
|
|
24
|
+
// entries: [{ name: 'popup/popup.js', data: Buffer }],manifest.json 在压缩包根目录
|
|
25
|
+
function zipFiles(entries) {
|
|
26
|
+
const locals = [];
|
|
27
|
+
const centrals = [];
|
|
28
|
+
let offset = 0;
|
|
29
|
+
for (const { name, data } of entries) {
|
|
30
|
+
const nameBuf = Buffer.from(name, 'utf8');
|
|
31
|
+
const deflated = zlib.deflateRawSync(data, { level: 9 });
|
|
32
|
+
const method = deflated.length < data.length ? 8 : 0; // 压不小的直接存
|
|
33
|
+
const body = method ? deflated : data;
|
|
34
|
+
const crc = crc32(data);
|
|
35
|
+
const local = Buffer.alloc(30);
|
|
36
|
+
local.writeUInt32LE(0x04034b50, 0);
|
|
37
|
+
local.writeUInt16LE(20, 4);
|
|
38
|
+
local.writeUInt16LE(0x0800, 6); // 文件名是 UTF-8
|
|
39
|
+
local.writeUInt16LE(method, 8);
|
|
40
|
+
local.writeUInt16LE(DOS_TIME, 10);
|
|
41
|
+
local.writeUInt16LE(DOS_DATE, 12);
|
|
42
|
+
local.writeUInt32LE(crc, 14);
|
|
43
|
+
local.writeUInt32LE(body.length, 18);
|
|
44
|
+
local.writeUInt32LE(data.length, 22);
|
|
45
|
+
local.writeUInt16LE(nameBuf.length, 26);
|
|
46
|
+
const central = Buffer.alloc(46);
|
|
47
|
+
central.writeUInt32LE(0x02014b50, 0);
|
|
48
|
+
central.writeUInt16LE(20, 4);
|
|
49
|
+
central.writeUInt16LE(20, 6);
|
|
50
|
+
central.writeUInt16LE(0x0800, 8);
|
|
51
|
+
central.writeUInt16LE(method, 10);
|
|
52
|
+
central.writeUInt16LE(DOS_TIME, 12);
|
|
53
|
+
central.writeUInt16LE(DOS_DATE, 14);
|
|
54
|
+
central.writeUInt32LE(crc, 16);
|
|
55
|
+
central.writeUInt32LE(body.length, 20);
|
|
56
|
+
central.writeUInt32LE(data.length, 24);
|
|
57
|
+
central.writeUInt16LE(nameBuf.length, 28);
|
|
58
|
+
central.writeUInt32LE(offset, 42);
|
|
59
|
+
locals.push(local, nameBuf, body);
|
|
60
|
+
centrals.push(central, nameBuf);
|
|
61
|
+
offset += local.length + nameBuf.length + body.length;
|
|
62
|
+
}
|
|
63
|
+
const dir = Buffer.concat(centrals);
|
|
64
|
+
const end = Buffer.alloc(22);
|
|
65
|
+
end.writeUInt32LE(0x06054b50, 0);
|
|
66
|
+
end.writeUInt16LE(entries.length, 8);
|
|
67
|
+
end.writeUInt16LE(entries.length, 10);
|
|
68
|
+
end.writeUInt32LE(dir.length, 12);
|
|
69
|
+
end.writeUInt32LE(offset, 16);
|
|
70
|
+
return Buffer.concat([...locals, dir, end]);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// protobuf 只用到长度定界的字段
|
|
74
|
+
function varint(n) {
|
|
75
|
+
const out = [];
|
|
76
|
+
while (n > 0x7f) { out.push((n & 0x7f) | 0x80); n = Math.floor(n / 128); }
|
|
77
|
+
out.push(n);
|
|
78
|
+
return Buffer.from(out);
|
|
79
|
+
}
|
|
80
|
+
const pbBytes = (field, buf) => Buffer.concat([varint(field * 8 + 2), varint(buf.length), buf]);
|
|
81
|
+
// CRX3:'Cr24' + 版本 3 + 头长度 + CrxFileHeader(公钥和签名、带 crx_id 的 SignedData)+ zip
|
|
82
|
+
function crx3(zip, privateKey) {
|
|
83
|
+
const pub = crypto.createPublicKey(privateKey).export({ type: 'spki', format: 'der' });
|
|
84
|
+
const signedData = pbBytes(1, crypto.createHash('sha256').update(pub).digest().subarray(0, 16));
|
|
85
|
+
const len = Buffer.alloc(4);
|
|
86
|
+
len.writeUInt32LE(signedData.length);
|
|
87
|
+
const signature = crypto.sign('sha256', Buffer.concat([Buffer.from('CRX3 SignedData\x00', 'latin1'), len, signedData, zip]), privateKey);
|
|
88
|
+
const header = Buffer.concat([pbBytes(2, Buffer.concat([pbBytes(1, pub), pbBytes(2, signature)])), pbBytes(10000, signedData)]);
|
|
89
|
+
const prefix = Buffer.alloc(12);
|
|
90
|
+
prefix.write('Cr24', 0, 'latin1');
|
|
91
|
+
prefix.writeUInt32LE(3, 4);
|
|
92
|
+
prefix.writeUInt32LE(header.length, 8);
|
|
93
|
+
return { crx: Buffer.concat([prefix, header, zip]), id: extensionIdOfKey(pub) };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 没有就生成一个 RSA 私钥(PKCS#8 PEM,Chrome「打包扩展程序」也认)。
|
|
97
|
+
// 先写临时文件再硬链接过去:两个进程同时生成时用先链接成的那个,读不到写了一半的
|
|
98
|
+
function loadOrCreateKey(file) {
|
|
99
|
+
try { return crypto.createPrivateKey(fs.readFileSync(file)); } catch (e) { if (e.code !== 'ENOENT') throw e; }
|
|
100
|
+
const { privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 });
|
|
101
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
102
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
103
|
+
fs.writeFileSync(tmp, privateKey.export({ type: 'pkcs8', format: 'pem' }), { mode: 0o600 });
|
|
104
|
+
try { fs.linkSync(tmp, file); } catch (e) { if (e.code !== 'EEXIST') throw e; } finally { fs.unlinkSync(tmp); }
|
|
105
|
+
return crypto.createPrivateKey(fs.readFileSync(file));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// manifest.json 里写上公钥(key):浏览器装 crx 时本来就会写进去;有的工具把包解开按已解压扩展加载,有它扩展 ID 也一样
|
|
109
|
+
function extensionEntries(root, files, pubDer) {
|
|
110
|
+
const walk = (rel) => {
|
|
111
|
+
const p = path.join(root, rel);
|
|
112
|
+
if (!fs.statSync(p).isDirectory()) return [rel];
|
|
113
|
+
return fs.readdirSync(p).filter((n) => !n.startsWith('.')).flatMap((n) => walk(path.join(rel, n))); // .DS_Store 这类不打进去
|
|
114
|
+
};
|
|
115
|
+
return files.flatMap(walk).map((rel) => rel.split(path.sep).join('/')).sort().map((name) => {
|
|
116
|
+
const data = fs.readFileSync(path.join(root, name));
|
|
117
|
+
if (name !== 'manifest.json') return { name, data };
|
|
118
|
+
const manifest = { ...JSON.parse(data.toString('utf8')), key: pubDer.toString('base64') };
|
|
119
|
+
return { name, data: Buffer.from(JSON.stringify(manifest, null, 2) + '\n') };
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 返回 { crx, zip, id }:两个包的路径和装上后的扩展 ID。
|
|
124
|
+
// 用户自己指定的私钥(createKey 为假)不存在时报错,不悄悄换一个新的(换了扩展 ID 就变了)
|
|
125
|
+
function packExtension({ root, files, outDir, keyFile, createKey, version }) {
|
|
126
|
+
const key = createKey ? loadOrCreateKey(keyFile) : crypto.createPrivateKey(fs.readFileSync(keyFile));
|
|
127
|
+
if (key.asymmetricKeyType !== 'rsa') throw new Error(`私钥要是 RSA 的(${keyFile} 是 ${key.asymmetricKeyType})`); // 签名只按 RSA 写
|
|
128
|
+
const zip = zipFiles(extensionEntries(root, files, crypto.createPublicKey(key).export({ type: 'spki', format: 'der' })));
|
|
129
|
+
const { crx, id } = crx3(zip, key);
|
|
130
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
131
|
+
const base = path.join(outDir, `page-recorder-${version}`);
|
|
132
|
+
fs.writeFileSync(`${base}.crx`, crx);
|
|
133
|
+
fs.writeFileSync(`${base}.zip`, zip);
|
|
134
|
+
return { crx: `${base}.crx`, zip: `${base}.zip`, id };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
module.exports = { packExtension, zipFiles, crx3, crc32 };
|
package/bridge/server.js
CHANGED
|
@@ -42,6 +42,8 @@ if (argv[0] === '--help' || argv[0] === '-h') {
|
|
|
42
42
|
page-recorder-mcp trust <扩展ID> 放行桥接没认的扩展(找不到安装目录时,比如装在指纹浏览器里的)
|
|
43
43
|
page-recorder-mcp extension [目录] 生成浏览器扩展(默认 ~/page-recorder-extension),
|
|
44
44
|
在 Chrome 的 chrome://extensions 里「加载已解压的扩展程序」选这个目录
|
|
45
|
+
page-recorder-mcp pack [目录] 打包成 .crx 和 .zip(默认放当前目录),给 HubStudio 等指纹浏览器「上传扩展」用;
|
|
46
|
+
crx 用 ~/.page-recorder/extension-key.pem 签名(--key <私钥文件> 换一个),扩展 ID 固定
|
|
45
47
|
|
|
46
48
|
选项(也可以用环境变量):
|
|
47
49
|
--port <端口> 监听端口,默认 17321 PR_BRIDGE_PORT
|
|
@@ -113,6 +115,37 @@ if (argv[0] === 'trust') {
|
|
|
113
115
|
process.stdout.write(`已放行扩展 ${id},扩展 30 秒内会自动连上桥接\n`);
|
|
114
116
|
process.exit(0);
|
|
115
117
|
}
|
|
118
|
+
if (argv[0] === 'pack') {
|
|
119
|
+
// HubStudio 等指纹浏览器「上传扩展」用的安装包。crx 的扩展 ID 由私钥定,这里打包的就是用户自己要装的,直接放行
|
|
120
|
+
const dirArg = argv.slice(1).find((a, i, rest) => !a.startsWith('-') && !['--key', '--port'].includes(rest[i - 1]));
|
|
121
|
+
const out = path.resolve(dirArg || process.cwd());
|
|
122
|
+
const keyFile = flag('key') ? path.resolve(flag('key')) : path.join(STATE_DIR, 'extension-key.pem');
|
|
123
|
+
if (!flag('key') && argv.some((a) => a === '--key' || a.startsWith('--key='))) {
|
|
124
|
+
process.stderr.write('--key 后面要写私钥文件的路径\n'); // 不能悄悄换成默认私钥:扩展 ID 会变
|
|
125
|
+
process.exit(1);
|
|
126
|
+
}
|
|
127
|
+
let r;
|
|
128
|
+
try {
|
|
129
|
+
r = require('./pack').packExtension({ root: ROOT, files: EXTENSION_FILES, outDir: out, keyFile, createKey: !flag('key'), version: VERSION });
|
|
130
|
+
} catch (e) {
|
|
131
|
+
process.stderr.write(`打包失败:${e.message}\n`);
|
|
132
|
+
process.exit(1);
|
|
133
|
+
}
|
|
134
|
+
const bridgePort = flag('port') || process.env.PR_BRIDGE_PORT || 17321;
|
|
135
|
+
let trusted = '桥接已放行';
|
|
136
|
+
try { writeTrusted([r.id]); } catch (e) { trusted = `放行失败(${e.message}),装好后运行 page-recorder-mcp trust ${r.id}`; }
|
|
137
|
+
process.stdout.write(`Page Recorder 扩展 ${VERSION} 已打包:
|
|
138
|
+
${r.crx}
|
|
139
|
+
HubStudio:环境列表 →「扩展管理」→「团队扩展」→ 上传扩展,安装文件选它。装上后扩展 ID 是 ${r.id},${trusted}。
|
|
140
|
+
环境的「端口扫描保护」下面要填允许端口 ${bridgePort},否则扩展连不上本机桥接;改完重启环境
|
|
141
|
+
${r.zip}
|
|
142
|
+
只收 zip 的地方用(那边重新签名的话扩展 ID 会变,连不上桥接时按扩展弹窗里显示的 ID 运行 page-recorder-mcp trust <ID>)
|
|
143
|
+
签名私钥:${keyFile}
|
|
144
|
+
以后打新版要用同一个私钥,扩展 ID 才不变、脚本不丢;在别的电脑上打包就把它复制过去,加 --key <私钥文件>。
|
|
145
|
+
指纹浏览器里装的扩展不会自动更新:有新版时重新运行这个命令,再上传新的 crx。
|
|
146
|
+
`);
|
|
147
|
+
process.exit(0);
|
|
148
|
+
}
|
|
116
149
|
|
|
117
150
|
const PORT = Number(flag('port') || process.env.PR_BRIDGE_PORT || 17321);
|
|
118
151
|
const HOST = flag('host') || process.env.PR_BRIDGE_HOST || '127.0.0.1';
|
|
@@ -968,7 +1001,7 @@ function trustExtension(id) {
|
|
|
968
1001
|
if (Date.now() - (rejectedIds.get(id) || 0) < ((rejectCount.get(id) || 0) <= 3 ? 3000 : 60000)) return false;
|
|
969
1002
|
const dir = await updater.extensionDir(id).catch(() => '');
|
|
970
1003
|
if (!dir) {
|
|
971
|
-
if (!rejectedIds.has(id)) log(`没认扩展 ${id}
|
|
1004
|
+
if (!rejectedIds.has(id)) log(`没认扩展 ${id}:没找到装着这一版 Page Recorder 的安装目录,确认不了是它。是你自己装的就运行 page-recorder-mcp trust ${id} 放行(装在指纹浏览器等自定义资料目录里的,也可以把资料目录写进 PR_BROWSER_DATA_DIRS)`);
|
|
972
1005
|
rejectedIds.set(id, Date.now());
|
|
973
1006
|
rejectCount.set(id, (rejectCount.get(id) || 0) + 1);
|
|
974
1007
|
return false;
|
|
@@ -2336,7 +2369,7 @@ server.on('upgrade', (req, socket, head) => {
|
|
|
2336
2369
|
if (!ok) {
|
|
2337
2370
|
// 握手后告诉扩展为什么不认(直接 403 的话扩展只知道连不上),然后关掉;什么请求都不转给它
|
|
2338
2371
|
// 关闭帧带 1008(违反策略);对方不回关闭也 2 秒后断开,不占连接名额
|
|
2339
|
-
socket.end(Buffer.concat([wsFrame(JSON.stringify({ type: 'rejected', extensionId: extId, reason: `桥接没认这个扩展(ID ${extId}
|
|
2372
|
+
socket.end(Buffer.concat([wsFrame(JSON.stringify({ type: 'rejected', extensionId: extId, reason: `桥接没认这个扩展(ID ${extId}):没找到装着这一版 Page Recorder 的安装目录,确认不了是它。是你自己装的,就在终端运行 npx page-recorder-mcp trust ${extId}` }), 1), wsFrame(Buffer.from([0x03, 0xf0]), 8)]));
|
|
2340
2373
|
setTimeout(() => socket.destroy(), 2000).unref();
|
|
2341
2374
|
return;
|
|
2342
2375
|
}
|
package/bridge/updater.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
6
|
const crypto = require('crypto');
|
|
7
|
+
const util = require('util');
|
|
7
8
|
|
|
8
9
|
const EXTENSION_FILES = ['manifest.json', 'background.js', 'content', 'inject', 'lib', 'popup', 'icons'];
|
|
9
10
|
const LOCK_STALE_MS = 60000;
|
|
@@ -16,13 +17,14 @@ function compareVersions(a, b) {
|
|
|
16
17
|
for (let i = 0; i < 3; i++) if ((x[i] || 0) !== (y[i] || 0)) return (x[i] || 0) < (y[i] || 0) ? -1 : 1;
|
|
17
18
|
return 0;
|
|
18
19
|
}
|
|
19
|
-
//
|
|
20
|
+
// 扩展 ID:SHA-256 的前 16 字节,每半字节映射到 a-p
|
|
21
|
+
const idOfHash = (bytes) => crypto.createHash('sha256').update(bytes).digest('hex').slice(0, 32).replace(/./g, (c) => String.fromCharCode(97 + parseInt(c, 16)));
|
|
22
|
+
// 已解压扩展按路径算(Windows 上路径是 UTF-16、盘符大写)
|
|
20
23
|
function unpackedExtensionId(p, platform = process.platform) {
|
|
21
|
-
|
|
22
|
-
? Buffer.from(p.replace(/^[a-z]:/, (x) => x.toUpperCase()), 'utf16le')
|
|
23
|
-
: Buffer.from(p, 'utf8');
|
|
24
|
-
return crypto.createHash('sha256').update(bytes).digest('hex').slice(0, 32).replace(/./g, (c) => String.fromCharCode(97 + parseInt(c, 16)));
|
|
24
|
+
return idOfHash(platform === 'win32' ? Buffer.from(p.replace(/^[a-z]:/, (x) => x.toUpperCase()), 'utf16le') : Buffer.from(p, 'utf8'));
|
|
25
25
|
}
|
|
26
|
+
// 打包的(crx)按公钥(DER)算
|
|
27
|
+
const extensionIdOfKey = (pubDer) => idOfHash(pubDer);
|
|
26
28
|
function extensionIdsOf(dir) {
|
|
27
29
|
const out = new Set([unpackedExtensionId(path.resolve(dir))]);
|
|
28
30
|
for (const real of [fs.realpathSync, fs.realpathSync.native]) {
|
|
@@ -48,12 +50,36 @@ function browserDataRoots(home, platform = process.platform, env = process.env)
|
|
|
48
50
|
const chrome = env.CHROME_CONFIG_HOME || config; // Chrome / Chromium 用它代替 XDG_CONFIG_HOME,下面照样接 google-chrome 这些
|
|
49
51
|
return extra.concat(['google-chrome', 'google-chrome-beta', 'google-chrome-unstable', 'chromium'].map((x) => join(chrome, x)), [['microsoft-edge'], ['BraveSoftware', 'Brave-Browser']].map((x) => join(config, ...x)));
|
|
50
52
|
}
|
|
51
|
-
//
|
|
53
|
+
// HubStudio:每个环境一个资料目录,放在它的缓存目录里(<缓存目录>/chromium_<环境>/Default)。缓存目录可以在客户端里改,
|
|
54
|
+
// 记在 hubstudio-client/ELECTRON_DB_LOCAL_STORAGE(SQLite)里的设置 JSON 中:不用 SQLite 也读得出,按文本找 "cachePath":"…"
|
|
55
|
+
const APP_DB_MAX_BYTES = 64 * 1024 * 1024;
|
|
56
|
+
async function hubstudioRoots(home, platform = process.platform, env = process.env) {
|
|
57
|
+
const p = platform === 'win32' ? path.win32 : path.posix;
|
|
58
|
+
let app;
|
|
59
|
+
if (platform === 'win32') app = p.join(env.APPDATA || p.join(home, 'AppData', 'Roaming'), 'hubstudio-client');
|
|
60
|
+
else if (platform === 'darwin') app = p.join(home, 'Library', 'Application Support', 'hubstudio-client');
|
|
61
|
+
else return [];
|
|
62
|
+
const out = new Set([p.join(app, 'sdk', 'cache')]);
|
|
63
|
+
// 刚改的设置可能还在 -wal 里没写回主库,一起找;记录跨了页、被拆开的找不到,靠 PR_BROWSER_DATA_DIRS 或 trust
|
|
64
|
+
for (const name of ['ELECTRON_DB_LOCAL_STORAGE', 'ELECTRON_DB_LOCAL_STORAGE-wal']) {
|
|
65
|
+
try {
|
|
66
|
+
const f = p.join(app, name);
|
|
67
|
+
if ((await fs.promises.stat(f)).size > APP_DB_MAX_BYTES) continue;
|
|
68
|
+
const text = (await fs.promises.readFile(f)).toString('utf8');
|
|
69
|
+
for (const m of text.matchAll(/"[cC]achePath":("(?:[^"\\\r\n]|\\.){1,1024}")/g)) {
|
|
70
|
+
try { const dir = JSON.parse(m[1]); if (p.isAbsolute(dir)) out.add(dir); } catch (e) { /* 截断的半截 */ }
|
|
71
|
+
}
|
|
72
|
+
} catch (e) { /* 没装 HubStudio、没有 -wal */ }
|
|
73
|
+
}
|
|
74
|
+
return [...out];
|
|
75
|
+
}
|
|
76
|
+
// 资料里记着的这些扩展装在哪:已解压的记绝对路径;打包装的(crx:商店、HubStudio 这类指纹浏览器装的)记相对 <资料>/Extensions 的路径。
|
|
77
|
+
// 异步一个个读,不卡住桥接;特别大的文件不读。
|
|
52
78
|
// 资料目录可能是 <根>/<资料>/Preferences(Chrome),也可能直接就是一个资料(<根>/Preferences),
|
|
53
79
|
// 或者指纹浏览器那样每个环境一个 --user-data-dir(<根>/<环境>/Default/Preferences):三种都找
|
|
54
80
|
const PREFS_MAX_BYTES = 32 * 1024 * 1024;
|
|
55
|
-
async function
|
|
56
|
-
const out = new
|
|
81
|
+
async function installedDirsOf(roots, ids) {
|
|
82
|
+
const out = new Map(); // 目录 → 是不是打包装的
|
|
57
83
|
for (const root of roots) {
|
|
58
84
|
let children = [];
|
|
59
85
|
try { children = (await fs.promises.readdir(root, { withFileTypes: true })).filter((e) => e.isDirectory()).map((e) => e.name); } catch (e) { continue; }
|
|
@@ -68,12 +94,18 @@ async function unpackedDirsOf(roots, ids) {
|
|
|
68
94
|
} catch (e) { continue; }
|
|
69
95
|
for (const id of ids) {
|
|
70
96
|
const p = settings && settings[id] && settings[id].path;
|
|
71
|
-
if (typeof p
|
|
97
|
+
if (typeof p !== 'string' || !p) continue;
|
|
98
|
+
if (path.isAbsolute(p)) out.set(p, false);
|
|
99
|
+
else if (!p.split(/[\\/]/).includes('..')) out.set(path.join(profile, 'Extensions', p), true);
|
|
72
100
|
}
|
|
73
101
|
}
|
|
74
102
|
}
|
|
75
103
|
}
|
|
76
|
-
return [...out];
|
|
104
|
+
return [...out].map(([dir, packed]) => ({ dir, packed }));
|
|
105
|
+
}
|
|
106
|
+
// 自动更新只动已解压的目录:打包装的由浏览器(或指纹浏览器客户端)自己管
|
|
107
|
+
async function unpackedDirsOf(roots, ids) {
|
|
108
|
+
return (await installedDirsOf(roots, ids)).filter((x) => !x.packed).map((x) => x.dir);
|
|
77
109
|
}
|
|
78
110
|
const sha = (x) => crypto.createHash('sha256').update(x).digest('hex');
|
|
79
111
|
function writeAtomic(file, data) {
|
|
@@ -152,6 +184,8 @@ function rollback(dir, saved, done) {
|
|
|
152
184
|
function createUpdater({ root, version, stateDir, home, log, browserRoots }) {
|
|
153
185
|
const installFailures = new Map();
|
|
154
186
|
const lookups = new Map(); // 扩展 ID → { n: 找了几次没找到, next: 下次最早什么时候再找 }(资料文件可能还没写盘,隔一分钟再找,最多 3 次)
|
|
187
|
+
const staleWarned = new Set(); // 提示过“装了但代码对不上”的扩展 ID:扩展每 30 秒重连一次,不反复提示
|
|
188
|
+
const allRoots = async () => browserRoots || [...browserDataRoots(home), ...await hubstudioRoots(home)];
|
|
155
189
|
const stateFile = path.join(stateDir, 'update.json');
|
|
156
190
|
const lockDir = path.join(stateDir, 'update.lock');
|
|
157
191
|
const skillSource = path.join(root, 'skills', 'page-recorder', 'SKILL.md');
|
|
@@ -313,7 +347,7 @@ function createUpdater({ root, version, stateDir, home, log, browserRoots }) {
|
|
|
313
347
|
const tried = (id) => lookups.get(id) || { n: 0, next: 0 };
|
|
314
348
|
const unknown = [...new Set(due.filter((b) => !dirOf(b)).map((b) => b.extensionId))].filter((id) => tried(id).n < 3 && Date.now() >= tried(id).next);
|
|
315
349
|
if (unknown.length) {
|
|
316
|
-
for (const d of await unpackedDirsOf(
|
|
350
|
+
for (const d of await unpackedDirsOf(await allRoots(), unknown)) {
|
|
317
351
|
if (dirs.includes(d) || !updatableDir(d) || !unknown.some((id) => extensionIdsOf(d).has(id))) continue;
|
|
318
352
|
dirs.push(d);
|
|
319
353
|
rememberExtensionDir(d); // 记下来,下次不用再找
|
|
@@ -370,7 +404,10 @@ function createUpdater({ root, version, stateDir, home, log, browserRoots }) {
|
|
|
370
404
|
// 连上来的扩展是不是 Page Recorder:按路径算出来的 ID 就是它的目录里,
|
|
371
405
|
// · 扩展文件和这个程序带的逐个一样(内容比对,别人照抄名字改了代码的过不了);
|
|
372
406
|
// · 或者是 `extension` 子命令生成、更新器记下的目录、默认目录(旧版本的文件,等连上后自动更新),manifest 名字对得上。
|
|
373
|
-
//
|
|
407
|
+
// 浏览器资料里登记的其他目录只认第一种。打包装的(`pack` 打的 crx 传进 HubStudio 这类)也是第一种,但浏览器安装时
|
|
408
|
+
// 会往 manifest 里写公钥(key)、重新编码图标:manifest 去掉 key 后要一样、key 算出的 ID 要就是它,图标不比(放不了代码),
|
|
409
|
+
// 别的文件照样逐个一样。版本号也不比:这种装法不会自动更新,只改了桥接的新版发出来后,装好的还认(代码变了就不认)。
|
|
410
|
+
// 找到返回目录,找不到返回空
|
|
374
411
|
let packageHashes = null;
|
|
375
412
|
const hashesOf = (dir) => {
|
|
376
413
|
const out = new Map();
|
|
@@ -381,23 +418,47 @@ function createUpdater({ root, version, stateDir, home, log, browserRoots }) {
|
|
|
381
418
|
}
|
|
382
419
|
return out;
|
|
383
420
|
};
|
|
384
|
-
const
|
|
421
|
+
const readManifest = (dir) => JSON.parse(fs.readFileSync(path.join(dir, 'manifest.json'), 'utf8'));
|
|
422
|
+
const samePackage = (dir, packed = false) => {
|
|
385
423
|
try {
|
|
386
424
|
packageHashes = packageHashes || hashesOf(root);
|
|
387
|
-
for (const [f, h] of packageHashes)
|
|
425
|
+
for (const [f, h] of packageHashes) {
|
|
426
|
+
if (packed && f === 'manifest.json') {
|
|
427
|
+
const { key, version: v1, ...installed } = readManifest(dir);
|
|
428
|
+
const { version: v2, ...ours } = readManifest(root);
|
|
429
|
+
if (!util.isDeepStrictEqual(installed, ours)) return false;
|
|
430
|
+
} else if (!(packed && f.endsWith('.png')) && sha(fs.readFileSync(path.join(dir, f))) !== h) return false;
|
|
431
|
+
}
|
|
388
432
|
return true;
|
|
389
433
|
} catch (e) { return false; }
|
|
390
434
|
};
|
|
435
|
+
const packedId = (dir) => {
|
|
436
|
+
try { return extensionIdOfKey(Buffer.from(String(readManifest(dir).key), 'base64')); } catch (e) { return ''; }
|
|
437
|
+
};
|
|
391
438
|
const namedOurs = (dir) => {
|
|
392
439
|
try { return String(JSON.parse(fs.readFileSync(path.join(dir, 'manifest.json'), 'utf8')).name || '').startsWith('Page Recorder'); } catch (e) { return false; }
|
|
393
440
|
};
|
|
394
441
|
async function extensionDir(id) {
|
|
395
442
|
const known = [...readState().extensionDirs, path.join(home, 'page-recorder-extension'), root];
|
|
396
443
|
for (const d of known) if (extensionIdsOf(d).has(id) && namedOurs(d)) return d;
|
|
397
|
-
|
|
444
|
+
// pack 打的 zip 解开按已解压扩展加载时,资料里记的是绝对路径,但 ID 按 manifest 里的公钥算:也按打包的规则核对。
|
|
445
|
+
// HubStudio 每个环境各装一份,没打开过的环境里可能还是旧版:都扫完没有对得上的才提示
|
|
446
|
+
let stale = '';
|
|
447
|
+
for (const { dir, packed } of await installedDirsOf(await allRoots(), [id])) {
|
|
448
|
+
const byKey = packedId(dir) === id;
|
|
449
|
+
if (byKey ? samePackage(dir, true) : !packed && extensionIdsOf(dir).has(id) && samePackage(dir)) {
|
|
450
|
+
staleWarned.delete(id);
|
|
451
|
+
return dir;
|
|
452
|
+
}
|
|
453
|
+
if (byKey && !stale && namedOurs(dir)) stale = dir;
|
|
454
|
+
}
|
|
455
|
+
if (stale && !staleWarned.has(id)) {
|
|
456
|
+
staleWarned.add(id);
|
|
457
|
+
log(`扩展 ${id} 装在 ${stale},但代码和这个桥接带的 ${version} 版不一样,没认:用这一版重新 pack、上传新的 crx,或者确认是自己装的就运行 page-recorder-mcp trust ${id}`);
|
|
458
|
+
}
|
|
398
459
|
return '';
|
|
399
460
|
}
|
|
400
461
|
return { rememberExtensionDir, updateExtensions, updateSkills, extensionDir };
|
|
401
462
|
}
|
|
402
463
|
|
|
403
|
-
module.exports = { createUpdater, compareVersions, unpackedExtensionId, copyTree, browserDataRoots, EXTENSION_FILES };
|
|
464
|
+
module.exports = { createUpdater, compareVersions, unpackedExtensionId, extensionIdOfKey, copyTree, browserDataRoots, hubstudioRoots, EXTENSION_FILES };
|
package/manifest.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "page-recorder-mcp",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.1",
|
|
4
4
|
"description": "Record clicks and the fetch/XHR APIs behind them in your logged-in Chrome (including cross-origin iframes), then let AI tools replay them or call the APIs directly through MCP / HTTP. Zero dependencies. 录制页面操作和接口(含跨域 iframe),让 Claude Code、Accio 等 AI 工具通过 MCP / HTTP 在你已登录的浏览器里回放,或直接调用接口。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mcp",
|
|
@@ -41,6 +41,6 @@
|
|
|
41
41
|
"scripts": {
|
|
42
42
|
"start": "node bridge/server.js",
|
|
43
43
|
"mcp": "node bridge/server.js --stdio",
|
|
44
|
-
"test": "node --test test/smoke.test.mjs test/result.test.mjs test/tools.test.mjs test/params.test.mjs test/security-and-bugs.test.mjs test/persistence.test.mjs test/merge.test.mjs test/dynamic.test.mjs test/shared.test.mjs test/lan.test.mjs"
|
|
44
|
+
"test": "node --test test/smoke.test.mjs test/result.test.mjs test/tools.test.mjs test/params.test.mjs test/security-and-bugs.test.mjs test/persistence.test.mjs test/merge.test.mjs test/dynamic.test.mjs test/shared.test.mjs test/lan.test.mjs test/pack.test.mjs"
|
|
45
45
|
}
|
|
46
46
|
}
|