browser-ref-mcp 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/README.md +174 -2
- package/README.zh-CN.md +157 -0
- package/dist/actions.js +381 -0
- package/dist/browserManager.js +410 -0
- package/dist/config.js +125 -0
- package/dist/errors.js +52 -0
- package/dist/index.js +68 -0
- package/dist/session.js +176 -0
- package/dist/snapshot.js +707 -0
- package/dist/tools.js +498 -0
- package/dist/util.js +113 -0
- package/package.json +53 -4
package/dist/session.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { mkdir, writeFile, rename } from "node:fs/promises";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { uid, withTimeout, log } from "./util.js";
|
|
5
|
+
/**
|
|
6
|
+
* 页面登记表。所有页面(含弹窗)都有 uuid,id 持久化以便跨重启恢复。
|
|
7
|
+
* 注册表/快照 ref 均通过 pageId 关联页面。
|
|
8
|
+
*/
|
|
9
|
+
export class SessionTracker {
|
|
10
|
+
pages = new Map();
|
|
11
|
+
meta = new Map();
|
|
12
|
+
activeId = null;
|
|
13
|
+
saveFile;
|
|
14
|
+
saveTimer = null;
|
|
15
|
+
saving = false;
|
|
16
|
+
dirty = false;
|
|
17
|
+
constructor(stateDir) {
|
|
18
|
+
this.saveFile = path.join(stateDir, "session.json");
|
|
19
|
+
}
|
|
20
|
+
adopt(page, opts) {
|
|
21
|
+
// 弹窗/新标签可能触发多次 adopt,先查重
|
|
22
|
+
for (const [id, p] of this.pages) {
|
|
23
|
+
if (p === page)
|
|
24
|
+
return id;
|
|
25
|
+
}
|
|
26
|
+
const id = uid();
|
|
27
|
+
this.pages.set(id, page);
|
|
28
|
+
this.meta.set(id, { url: page.url(), title: "" });
|
|
29
|
+
page.on("framenavigated", (frame) => {
|
|
30
|
+
if (frame === page.mainFrame()) {
|
|
31
|
+
const m = this.meta.get(id);
|
|
32
|
+
if (m) {
|
|
33
|
+
m.url = page.url();
|
|
34
|
+
this.markDirty();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
page.once("close", () => {
|
|
39
|
+
this.remove(id);
|
|
40
|
+
});
|
|
41
|
+
if (opts?.activate || this.activeId === null || !this.pages.has(this.activeId)) {
|
|
42
|
+
this.activeId = id;
|
|
43
|
+
}
|
|
44
|
+
this.markDirty();
|
|
45
|
+
return id;
|
|
46
|
+
}
|
|
47
|
+
remove(id) {
|
|
48
|
+
this.pages.delete(id);
|
|
49
|
+
this.meta.delete(id);
|
|
50
|
+
if (this.activeId === id) {
|
|
51
|
+
const first = this.pages.keys().next();
|
|
52
|
+
this.activeId = first.done ? null : first.value;
|
|
53
|
+
}
|
|
54
|
+
this.markDirty();
|
|
55
|
+
}
|
|
56
|
+
get(id) {
|
|
57
|
+
const p = this.pages.get(id);
|
|
58
|
+
return p && !p.isClosed() ? p : undefined;
|
|
59
|
+
}
|
|
60
|
+
getActive() {
|
|
61
|
+
if (this.activeId) {
|
|
62
|
+
const p = this.get(this.activeId);
|
|
63
|
+
if (p)
|
|
64
|
+
return p;
|
|
65
|
+
this.activeId = null; // 已死,自动换到第一个活页
|
|
66
|
+
}
|
|
67
|
+
const first = this.pages.keys().next();
|
|
68
|
+
if (first.done)
|
|
69
|
+
return undefined;
|
|
70
|
+
this.activeId = first.value;
|
|
71
|
+
return this.pages.get(first.value);
|
|
72
|
+
}
|
|
73
|
+
setActive(id) {
|
|
74
|
+
if (!this.get(id))
|
|
75
|
+
return false;
|
|
76
|
+
this.activeId = id;
|
|
77
|
+
this.markDirty();
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
listTabs() {
|
|
81
|
+
const out = [];
|
|
82
|
+
for (const [id, p] of this.pages) {
|
|
83
|
+
if (p.isClosed())
|
|
84
|
+
continue;
|
|
85
|
+
const m = this.meta.get(id);
|
|
86
|
+
out.push({
|
|
87
|
+
id,
|
|
88
|
+
url: p.url(),
|
|
89
|
+
title: m?.title ?? "",
|
|
90
|
+
active: id === this.activeId,
|
|
91
|
+
pageId: id,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
async refreshTitle(id) {
|
|
97
|
+
const p = this.pages.get(id);
|
|
98
|
+
if (!p || p.isClosed())
|
|
99
|
+
return;
|
|
100
|
+
try {
|
|
101
|
+
const t = await withTimeout(p.title(), 3000, "取标题");
|
|
102
|
+
this.meta.set(id, { url: p.url(), title: t });
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
/* 标题拿不到不影响主流程 */
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
markDirty() {
|
|
109
|
+
this.dirty = true;
|
|
110
|
+
if (this.saveTimer)
|
|
111
|
+
return;
|
|
112
|
+
this.saveTimer = setTimeout(() => void this.flush(), 600);
|
|
113
|
+
}
|
|
114
|
+
/** 原子写 session.json(tmp + rename),标题尽力而为 */
|
|
115
|
+
async flush() {
|
|
116
|
+
if (this.saveTimer) {
|
|
117
|
+
clearTimeout(this.saveTimer);
|
|
118
|
+
this.saveTimer = null;
|
|
119
|
+
}
|
|
120
|
+
if (!this.dirty && !this.forceSaveFlag)
|
|
121
|
+
return;
|
|
122
|
+
this.dirty = false;
|
|
123
|
+
this.forceSaveFlag = false;
|
|
124
|
+
this.saving = true;
|
|
125
|
+
try {
|
|
126
|
+
const tabs = [];
|
|
127
|
+
for (const [id, p] of this.pages) {
|
|
128
|
+
if (p.isClosed())
|
|
129
|
+
continue;
|
|
130
|
+
const m = this.meta.get(id);
|
|
131
|
+
let title = m?.title ?? "";
|
|
132
|
+
try {
|
|
133
|
+
if (!title)
|
|
134
|
+
title = await withTimeout(p.title(), 1500, "取标题");
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
title = "";
|
|
138
|
+
}
|
|
139
|
+
tabs.push({ id, url: p.url(), title });
|
|
140
|
+
}
|
|
141
|
+
const state = {
|
|
142
|
+
version: 1,
|
|
143
|
+
tabs,
|
|
144
|
+
activeId: this.activeId,
|
|
145
|
+
updatedAt: new Date().toISOString(),
|
|
146
|
+
};
|
|
147
|
+
await mkdir(path.dirname(this.saveFile), { recursive: true });
|
|
148
|
+
const tmp = this.saveFile + ".tmp";
|
|
149
|
+
await writeFile(tmp, JSON.stringify(state, null, 2), "utf8");
|
|
150
|
+
await rename(tmp, this.saveFile);
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
void log("warn", "session save failed", e);
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
this.saving = false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
forceSaveFlag = false;
|
|
160
|
+
/** 读出持久化的会话(用于重启恢复) */
|
|
161
|
+
readPersisted() {
|
|
162
|
+
try {
|
|
163
|
+
if (!existsSync(this.saveFile))
|
|
164
|
+
return null;
|
|
165
|
+
const raw = readFileSync(this.saveFile, "utf8");
|
|
166
|
+
const st = JSON.parse(raw);
|
|
167
|
+
if (st.version !== 1 || !Array.isArray(st.tabs))
|
|
168
|
+
return null;
|
|
169
|
+
return st;
|
|
170
|
+
}
|
|
171
|
+
catch (e) {
|
|
172
|
+
void log("warn", "session read failed", e);
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|