dsh-annotator 0.6.0 → 0.6.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 +1 -1
- package/lib/session.js +53 -7
- package/lib/shared/spaces.js +44 -5
- package/package.json +1 -1
package/README.md
CHANGED
package/lib/session.js
CHANGED
|
@@ -59,6 +59,7 @@ import {
|
|
|
59
59
|
filterTabsBySpace,
|
|
60
60
|
nextSpaceName,
|
|
61
61
|
normalizeSpaceId,
|
|
62
|
+
pickDefaultContextId,
|
|
62
63
|
sanitizeSpaceName,
|
|
63
64
|
} from './shared/spaces.js';
|
|
64
65
|
|
|
@@ -307,7 +308,12 @@ export class VisualPicker {
|
|
|
307
308
|
spaceName:
|
|
308
309
|
this.spaceList().find((space) => space.id === normalizeSpaceId(this.activeSpace))?.name ?? DEFAULT_SPACE_NAME,
|
|
309
310
|
// 只列当前空间的标签页:另一个空间开着的页面不该出现在这一格的标签条里。
|
|
310
|
-
tabs: filterTabsBySpace(
|
|
311
|
+
tabs: filterTabsBySpace(
|
|
312
|
+
this.state.tabs,
|
|
313
|
+
this.activeSpace,
|
|
314
|
+
this.defaultContextId,
|
|
315
|
+
this.spaces.map((space) => space.id),
|
|
316
|
+
).map((tab) => ({
|
|
311
317
|
...tab,
|
|
312
318
|
active: tab.targetId === this.activeTargetId,
|
|
313
319
|
// false = 这一页里没有标注层(host 会继续重试),面板据此标一个警示点,
|
|
@@ -676,6 +682,10 @@ export class VisualPicker {
|
|
|
676
682
|
await this.setMode(decideAutoPick('launch', this), { trigger: 'auto' });
|
|
677
683
|
await this._seedTabs();
|
|
678
684
|
await this._syncSpaces();
|
|
685
|
+
// 启动那一瞬的目标会被浏览器换掉(还是 about:blank,targetId 却换了)。
|
|
686
|
+
// 返回 ready 之前先按真实情况对一次,别让第一个 snapshot 报出 0 或 2 个
|
|
687
|
+
// 标签——面板第一次渲染就建在错的数据上,用户会看到一条空的标签条。
|
|
688
|
+
await this.refreshTabs({ force: true }).catch(() => {});
|
|
679
689
|
// ego:新手引导窗口在这时候已经弹出来了,先关掉,再把调试页开进独立空间。
|
|
680
690
|
if (browserId === 'ego') {
|
|
681
691
|
await this._closeEgoOnboarding();
|
|
@@ -813,6 +823,9 @@ export class VisualPicker {
|
|
|
813
823
|
this.state.activeTitle = preferred.title ?? '';
|
|
814
824
|
}
|
|
815
825
|
await this._seedTabs();
|
|
826
|
+
// 接回来的窗口里可能留着上一轮已经关掉的标签(事件发生在断开期间),
|
|
827
|
+
// 同样按真实情况对一遍再报 ready。
|
|
828
|
+
await this.refreshTabs({ force: true }).catch(() => {});
|
|
816
829
|
return true;
|
|
817
830
|
} catch {
|
|
818
831
|
if (this.cdp === cdp) this.cdp = null;
|
|
@@ -845,9 +858,12 @@ export class VisualPicker {
|
|
|
845
858
|
async _syncSpaces() {
|
|
846
859
|
if (!this.cdp) return;
|
|
847
860
|
let contextIds = [];
|
|
861
|
+
let authoritative = '';
|
|
848
862
|
try {
|
|
849
863
|
const reply = await this.cdp.send('Target.getBrowserContexts');
|
|
850
864
|
contextIds = reply?.browserContextIds ?? [];
|
|
865
|
+
// 新版 CDP 在这里连带给出默认空间的 context id;老版本没有这个字段。
|
|
866
|
+
authoritative = normalizeSpaceId(reply?.defaultBrowserContextId);
|
|
851
867
|
} catch {
|
|
852
868
|
return;
|
|
853
869
|
}
|
|
@@ -863,17 +879,29 @@ export class VisualPicker {
|
|
|
863
879
|
// 「空间」,再新建一个空的「agent 空间」,页面就找不回来了。
|
|
864
880
|
this._loadSpaces();
|
|
865
881
|
const live = new Set(contextIds.filter(Boolean));
|
|
882
|
+
// 默认空间的 context 绝不能当空间登记:一旦登记进 spaces,它带的页面就会
|
|
883
|
+
// 被「只显示当前空间」这条规则从默认空间的标签条里全部筛掉。
|
|
884
|
+
if (authoritative) live.delete(authoritative);
|
|
866
885
|
this.spaces = this.spaces.filter((space) => live.has(space.id));
|
|
867
886
|
for (const id of live) {
|
|
868
887
|
if (this.spaces.some((space) => space.id === id)) continue;
|
|
869
888
|
this.spaces.push({ id, name: nextSpaceName(this.spaces.map((space) => space.name)) });
|
|
870
889
|
}
|
|
871
|
-
// 默认空间的 context id
|
|
872
|
-
//
|
|
873
|
-
//
|
|
874
|
-
|
|
875
|
-
const
|
|
876
|
-
|
|
890
|
+
// 默认空间的 context id:优先听浏览器自己说(defaultBrowserContextId),
|
|
891
|
+
// 其次看用户眼前这个页面落在哪个 context。老办法「第一个不属于已知空间的
|
|
892
|
+
// target」不稳——真实 profile 里扩展、内部页、另一个窗口的页面各带一个
|
|
893
|
+
// context,认错就会把真正的页面从标签条里筛掉(见 filterTabsBySpace)。
|
|
894
|
+
const known = this.spaces.map((space) => space.id);
|
|
895
|
+
const contextOf = (info) => normalizeSpaceId(info?.browserContextId);
|
|
896
|
+
const activeInfo = targets.find((info) => info.targetId === this.activeTargetId);
|
|
897
|
+
const preferred = pickDefaultContextId({
|
|
898
|
+
authoritative,
|
|
899
|
+
activeContext: contextOf(activeInfo),
|
|
900
|
+
pageContexts: targets.filter((info) => info.type === 'page').map(contextOf),
|
|
901
|
+
targetContexts: targets.map(contextOf),
|
|
902
|
+
knownContexts: known,
|
|
903
|
+
});
|
|
904
|
+
if (preferred && preferred !== this.defaultContextId) this.defaultContextId = preferred;
|
|
877
905
|
this._saveSpaces();
|
|
878
906
|
}
|
|
879
907
|
|
|
@@ -972,6 +1000,9 @@ export class VisualPicker {
|
|
|
972
1000
|
this.state.activeTitle = preferred.title ?? '';
|
|
973
1001
|
}
|
|
974
1002
|
await this._seedTabs();
|
|
1003
|
+
// 接回来的窗口里可能留着上一轮已经关掉的标签(事件发生在断开期间),
|
|
1004
|
+
// 同样按真实情况对一遍再报 ready。
|
|
1005
|
+
await this.refreshTabs({ force: true }).catch(() => {});
|
|
975
1006
|
return true;
|
|
976
1007
|
} catch {
|
|
977
1008
|
return false;
|
|
@@ -1286,6 +1317,21 @@ export class VisualPicker {
|
|
|
1286
1317
|
const fingerprint = (tabs) =>
|
|
1287
1318
|
JSON.stringify((tabs ?? []).map((tab) => [tab.targetId, tab.url, tab.title, tab.contextId]));
|
|
1288
1319
|
const before = fingerprint(this.state.tabs);
|
|
1320
|
+
// 以 getTargets 为准对一遍,除了补新的,还要删掉已经不存在的:启动那一瞬
|
|
1321
|
+
// 的目标常常在开「发现」之前就被销毁,事件永远不会来,只增不删就会留一个
|
|
1322
|
+
// 「点不开的幽灵标签」——面板显示它还在,浏览器里其实早没了。
|
|
1323
|
+
const alive = new Set(targetInfos.map((info) => info.targetId));
|
|
1324
|
+
const gone = (this.state.tabs ?? []).filter((tab) => !alive.has(tab.targetId)).map((tab) => tab.targetId);
|
|
1325
|
+
if (gone.length) {
|
|
1326
|
+
this.state.tabs = (this.state.tabs ?? []).filter((tab) => alive.has(tab.targetId));
|
|
1327
|
+
for (const targetId of gone) {
|
|
1328
|
+
this.attached.delete(targetId);
|
|
1329
|
+
this.overlayOk.delete(targetId);
|
|
1330
|
+
this.preloadDone.delete(targetId);
|
|
1331
|
+
this.attachTriedAt.delete(targetId);
|
|
1332
|
+
if (this.activeTargetId === targetId) this.activeTargetId = null;
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1289
1335
|
for (const info of targetInfos) this._upsertTab(info, { emit: false });
|
|
1290
1336
|
if (before !== fingerprint(this.state.tabs)) this._emit();
|
|
1291
1337
|
// 新标签页原本只有 Target.targetCreated 一条命:事件漏了、或那一次 attach /
|
package/lib/shared/spaces.js
CHANGED
|
@@ -40,15 +40,54 @@ export function normalizeSpaceId(value) {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
43
|
+
* 标签属于哪个空间。
|
|
44
|
+
*
|
|
45
|
+
* 判断口径:**凡是认不出来的 context,都算默认空间**。默认空间的 context id 是
|
|
46
|
+
* 猜出来的——CDP 只保证 `createBrowserContext` 建的那些出现在
|
|
47
|
+
* `Target.getBrowserContexts` 里,默认空间的那个 id 得从某个 target 上读,而
|
|
48
|
+
* 一个真实 profile 里同时躺着好几个 context(扩展、内部页、不同窗口的页各不
|
|
49
|
+
* 相同)。按 context 严格相等去过滤,猜错一次就会把用户眼前这一页从标签条里
|
|
50
|
+
* 摘掉:页面还活着、HUD 还能点,面板却说「没有标签」。所以反向认定更稳:
|
|
51
|
+
* 只有**明确属于某个已知空间**的标签才被排除在默认空间之外。
|
|
45
52
|
*/
|
|
46
|
-
export function filterTabsBySpace(tabs, spaceId, defaultContextId) {
|
|
53
|
+
export function filterTabsBySpace(tabs, spaceId, defaultContextId, knownSpaceIds = []) {
|
|
47
54
|
const key = normalizeSpaceId(spaceId);
|
|
48
55
|
const fallback = normalizeSpaceId(defaultContextId);
|
|
56
|
+
const known = new Set((knownSpaceIds ?? []).map((value) => normalizeSpaceId(value)).filter(Boolean));
|
|
49
57
|
return (tabs ?? []).filter((tab) => {
|
|
50
58
|
const context = normalizeSpaceId(tab?.contextId);
|
|
51
|
-
const
|
|
52
|
-
return key === '' ?
|
|
59
|
+
const isDefault = !context || context === fallback || !known.has(context);
|
|
60
|
+
return key === '' ? isDefault : context === key;
|
|
53
61
|
});
|
|
54
62
|
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 默认空间的 context id 该取哪一个。
|
|
66
|
+
*
|
|
67
|
+
* 优先级从「谁说算数」到「只能猜」:
|
|
68
|
+
* 1. `authoritative`——新版 CDP 在 `Target.getBrowserContexts` 里直接给
|
|
69
|
+
* `defaultBrowserContextId`,这是浏览器自己说的,最可信;
|
|
70
|
+
* 2. `activeContext`——用户眼前正在看的那一页所在的 context;
|
|
71
|
+
* 3. `pageContexts`——任意一个页面 target 的 context;
|
|
72
|
+
* 4. `targetContexts` 里第一个不属于已知空间的 context(老版本 CDP 的兜底)。
|
|
73
|
+
*
|
|
74
|
+
* 顺序反了就会出现「插件把扩展的 context 当成默认空间」,面板标签条随后一条
|
|
75
|
+
* 都显示不出来(见 `filterTabsBySpace` 的说明)。
|
|
76
|
+
*/
|
|
77
|
+
export function pickDefaultContextId({
|
|
78
|
+
authoritative,
|
|
79
|
+
activeContext,
|
|
80
|
+
pageContexts,
|
|
81
|
+
targetContexts,
|
|
82
|
+
knownContexts,
|
|
83
|
+
} = {}) {
|
|
84
|
+
const known = new Set((knownContexts ?? []).map((value) => normalizeSpaceId(value)).filter(Boolean));
|
|
85
|
+
const authoritativeId = normalizeSpaceId(authoritative);
|
|
86
|
+
if (authoritativeId) return authoritativeId;
|
|
87
|
+
const active = normalizeSpaceId(activeContext);
|
|
88
|
+
if (active && !known.has(active)) return active;
|
|
89
|
+
const page = (pageContexts ?? []).map((value) => normalizeSpaceId(value)).find((id) => id && !known.has(id));
|
|
90
|
+
if (page) return page;
|
|
91
|
+
const anyTarget = (targetContexts ?? []).map((value) => normalizeSpaceId(value)).find((id) => id && !known.has(id));
|
|
92
|
+
return anyTarget ?? '';
|
|
93
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-annotator",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Visual element picker for DSH: highlight, annotate, comment and screenshot elements of a real Chrome/Edge page, then hand the selector + comment + shot to the agent.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|