glidercli 0.4.19 → 0.4.32

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/lib/bserve.js CHANGED
@@ -149,7 +149,8 @@ async function buildLiveCatalog() {
149
149
  focusedWindowId,
150
150
  listTabsOk,
151
151
  });
152
- if (listTabsOk) reapFromMap(connectedTargets, catalog.reapSessionIds);
152
+ // catalog.listTabsOk is false on untrusted empty chrome.tabs (no mass-reap)
153
+ if (catalog.listTabsOk) reapFromMap(connectedTargets, catalog.reapSessionIds);
153
154
  return catalog;
154
155
  }
155
156
 
package/lib/bwindow.js CHANGED
@@ -1,16 +1,18 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * bwindow.js - Multi-window/tab management for Glider
4
- *
3
+ * bwindow.js - CDP window control plane (create/close/focus). Not tab identity SSOT.
4
+ *
5
+ * Identity + listing: `glider tabs` / `glider window list` → tab-catalog + $GLIDER/state/tab-map.json.
6
+ *
5
7
  * Commands:
6
8
  * glider window new [url] Create new browser window
7
9
  * glider window close <targetId> Close specific tab/window
8
10
  * glider window closeall Close all tabs created by Glider
9
- * glider window list List all windows/tabs
11
+ * glider window list Same catalog/TUI as glider tabs (bin/glider.js)
10
12
  * glider window focus <targetId> Bring tab to foreground
11
- *
12
- * The key insight: tabs created with newWindow:true CAN be closed via Target.closeTarget
13
- * Tabs created in the main window CANNOT be closed (Chrome security)
13
+ *
14
+ * Tabs created with newWindow:true CAN be closed via Target.closeTarget.
15
+ * Tabs created in the main window CANNOT be closed (Chrome security).
14
16
  */
15
17
 
16
18
  const WebSocket = require('ws');
@@ -0,0 +1,233 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const { cookiesDirRoot, cookiesDomainsFile, warchScanRoots } = require('./paths.js');
6
+
7
+ function stripAnsi(s) {
8
+ return String(s).replace(/\x1b\[[0-9;]*m/g, '');
9
+ }
10
+
11
+ function parseHeaderLine(raw) {
12
+ if (raw == null || raw === '') return '';
13
+ for (const line of String(raw).split(/\r?\n/)) {
14
+ let clean = stripAnsi(line).trimStart();
15
+ if (clean.startsWith('Cookie:')) clean = clean.slice('Cookie:'.length).trimStart();
16
+ if (clean && clean.includes('=')) return clean;
17
+ }
18
+ return '';
19
+ }
20
+
21
+ function domainDir(domain) {
22
+ return path.join(cookiesDirRoot(), domain);
23
+ }
24
+
25
+ function ensureJarRoot() {
26
+ fs.mkdirSync(cookiesDirRoot(), { recursive: true });
27
+ }
28
+
29
+ function loadDomainsFile() {
30
+ ensureJarRoot();
31
+ const p = cookiesDomainsFile();
32
+ if (!fs.existsSync(p)) return null;
33
+ return JSON.parse(fs.readFileSync(p, 'utf8'));
34
+ }
35
+
36
+ function saveDomains(domains) {
37
+ ensureJarRoot();
38
+ const sorted = [...new Set(domains)].sort();
39
+ fs.writeFileSync(cookiesDomainsFile(), `${JSON.stringify({ domains: sorted }, null, 2)}\n`);
40
+ return sorted;
41
+ }
42
+
43
+ function listDomainNames() {
44
+ const data = loadDomainsFile();
45
+ if (data && Array.isArray(data.domains)) return data.domains;
46
+ rebuildDomains();
47
+ const again = loadDomainsFile();
48
+ return again && Array.isArray(again.domains) ? again.domains : [];
49
+ }
50
+
51
+ function walkGliderJson(root) {
52
+ const out = [];
53
+ if (!root || !fs.existsSync(root)) return out;
54
+ const stack = [root];
55
+ while (stack.length) {
56
+ const dir = stack.pop();
57
+ let entries;
58
+ try {
59
+ entries = fs.readdirSync(dir, { withFileTypes: true });
60
+ } catch {
61
+ continue;
62
+ }
63
+ for (const ent of entries) {
64
+ const full = path.join(dir, ent.name);
65
+ if (ent.isDirectory()) stack.push(full);
66
+ else if (ent.isFile() && ent.name === 'glider.json') out.push(full);
67
+ }
68
+ }
69
+ return out.sort();
70
+ }
71
+
72
+ function rebuildDomains() {
73
+ ensureJarRoot();
74
+ const domains = [];
75
+ for (const root of warchScanRoots()) {
76
+ for (const gj of walkGliderJson(root)) {
77
+ try {
78
+ const data = JSON.parse(fs.readFileSync(gj, 'utf8'));
79
+ const jar = data.cookie_jar || {};
80
+ if (jar.enabled && data.host) domains.push(data.host);
81
+ } catch {
82
+ /* skip */
83
+ }
84
+ }
85
+ }
86
+ let existing = [];
87
+ try {
88
+ const cur = loadDomainsFile();
89
+ if (cur && Array.isArray(cur.domains)) existing = cur.domains;
90
+ } catch {
91
+ /* fresh */
92
+ }
93
+ for (const d of existing) {
94
+ if (!domains.includes(d)) domains.push(d);
95
+ }
96
+ const sorted = saveDomains(domains);
97
+ return { path: cookiesDomainsFile(), count: sorted.length };
98
+ }
99
+
100
+ function writeMeta(domain, headerLine) {
101
+ const count = headerLine.split(';').filter((p) => p.trim()).length;
102
+ const now = Math.floor(Date.now() / 1000);
103
+ const meta = {
104
+ domain,
105
+ synced_at: now,
106
+ synced_iso: new Date(now * 1000).toISOString().replace(/\.\d{3}Z$/, 'Z'),
107
+ cookie_count: count,
108
+ stale: false,
109
+ };
110
+ fs.writeFileSync(path.join(domainDir(domain), 'meta.json'), `${JSON.stringify(meta, null, 2)}\n`);
111
+ return count;
112
+ }
113
+
114
+ function markStale(domain) {
115
+ const metaPath = path.join(domainDir(domain), 'meta.json');
116
+ if (!fs.existsSync(metaPath)) return;
117
+ const m = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
118
+ m.stale = true;
119
+ m.stale_since = Math.floor(Date.now() / 1000);
120
+ fs.writeFileSync(metaPath, `${JSON.stringify(m, null, 2)}\n`);
121
+ }
122
+
123
+ async function syncOne(domain, fetchLive) {
124
+ fs.mkdirSync(domainDir(domain), { recursive: true });
125
+ let hdr = '';
126
+ if (fetchLive) {
127
+ try {
128
+ hdr = parseHeaderLine(await fetchLive(domain));
129
+ } catch {
130
+ hdr = '';
131
+ }
132
+ }
133
+ if (!hdr) {
134
+ markStale(domain);
135
+ const err = new Error(`${domain}: no cookies in response (extension down or not logged in)`);
136
+ err.code = 'SYNC_EMPTY';
137
+ throw err;
138
+ }
139
+ fs.writeFileSync(path.join(domainDir(domain), 'header.txt'), hdr);
140
+ const count = writeMeta(domain, hdr);
141
+ return { domain, count };
142
+ }
143
+
144
+ async function syncAll(fetchLive) {
145
+ const names = listDomainNames();
146
+ let ok = 0;
147
+ let fail = 0;
148
+ for (const d of names) {
149
+ try {
150
+ await syncOne(d, fetchLive);
151
+ ok += 1;
152
+ } catch {
153
+ fail += 1;
154
+ }
155
+ }
156
+ return { ok, fail };
157
+ }
158
+
159
+ async function readDomain(domain, fetchLive, warn) {
160
+ if (fetchLive) {
161
+ try {
162
+ const hdr = parseHeaderLine(await fetchLive(domain));
163
+ if (hdr) return hdr;
164
+ } catch {
165
+ /* fall through */
166
+ }
167
+ }
168
+ const jar = path.join(domainDir(domain), 'header.txt');
169
+ if (fs.existsSync(jar)) {
170
+ const metaPath = path.join(domainDir(domain), 'meta.json');
171
+ if (fs.existsSync(metaPath) && typeof warn === 'function') {
172
+ const m = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
173
+ const age = Math.floor(Date.now() / 1000) - m.synced_at;
174
+ warn(`using jar (${age}s old)`);
175
+ }
176
+ return fs.readFileSync(jar, 'utf8').trim();
177
+ }
178
+ const err = new Error(`no live cookies and no jar for ${domain}`);
179
+ err.code = 'READ_MISS';
180
+ throw err;
181
+ }
182
+
183
+ function ageDomain(domain) {
184
+ const metaPath = path.join(domainDir(domain), 'meta.json');
185
+ if (!fs.existsSync(metaPath)) return '';
186
+ const m = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
187
+ return String(Math.floor(Date.now() / 1000) - m.synced_at);
188
+ }
189
+
190
+ function listJars() {
191
+ listDomainNames();
192
+ const rows = [];
193
+ for (const d of listDomainNames()) {
194
+ const metaPath = path.join(domainDir(d), 'meta.json');
195
+ if (fs.existsSync(metaPath)) {
196
+ const m = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
197
+ const age = Math.floor(Date.now() / 1000) - m.synced_at;
198
+ rows.push({ domain: d, age: String(age), stale: m.stale ? 'yes' : 'no' });
199
+ } else {
200
+ rows.push({ domain: d, age: 'never', stale: '-' });
201
+ }
202
+ }
203
+ return rows;
204
+ }
205
+
206
+ function addDomain(domain) {
207
+ const names = listDomainNames();
208
+ if (names.includes(domain)) return { added: false, domain };
209
+ names.push(domain);
210
+ saveDomains(names);
211
+ return { added: true, domain };
212
+ }
213
+
214
+ function rmDomain(domain) {
215
+ const names = listDomainNames();
216
+ if (!names.includes(domain)) return { removed: false, domain };
217
+ saveDomains(names.filter((d) => d !== domain));
218
+ return { removed: true, domain };
219
+ }
220
+
221
+ module.exports = {
222
+ parseHeaderLine,
223
+ rebuildDomains,
224
+ syncOne,
225
+ syncAll,
226
+ readDomain,
227
+ ageDomain,
228
+ listJars,
229
+ addDomain,
230
+ rmDomain,
231
+ listDomainNames,
232
+ domainDir,
233
+ };
package/lib/paths.js CHANGED
@@ -55,6 +55,28 @@ function warchRoot() {
55
55
  return path.join(registryBase(), 'warch');
56
56
  }
57
57
 
58
+ /** Persistent cookie snapshots: $GLIDER/cookies. */
59
+ function cookiesDirRoot() {
60
+ return path.join(gliderHome(), 'cookies');
61
+ }
62
+
63
+ function cookiesDomainsFile() {
64
+ return path.join(cookiesDirRoot(), 'domains.json');
65
+ }
66
+
67
+ /** Warch trees scanned for cookie_jar.enabled ($GLIDER/warch plus optional GLIDER_EXTRA_WARCH_ROOT). */
68
+ function warchScanRoots() {
69
+ const roots = [warchRoot()];
70
+ const extra = process.env.GLIDER_EXTRA_WARCH_ROOT;
71
+ if (extra) {
72
+ for (const raw of extra.split(path.delimiter)) {
73
+ const p = raw.trim();
74
+ if (p) roots.push(expandUser(p));
75
+ }
76
+ }
77
+ return [...new Set(roots)];
78
+ }
79
+
58
80
  function domainsTemplatePath() {
59
81
  return path.join(packageRoot(), 'config', 'domains.template.json');
60
82
  }
@@ -91,6 +113,9 @@ module.exports = {
91
113
  registryMode,
92
114
  configPath,
93
115
  warchRoot,
116
+ cookiesDirRoot,
117
+ cookiesDomainsFile,
118
+ warchScanRoots,
94
119
  domainsTemplatePath,
95
120
  expandUser,
96
121
  warchDirForHost,
@@ -22,12 +22,17 @@ function windowIncognito(chromeWindows, wid, tabsInWindow) {
22
22
  return { incognito: false, incognitoSource: '' };
23
23
  }
24
24
 
25
+ function ti(page) {
26
+ return (page && page.targetInfo && typeof page.targetInfo === 'object') ? page.targetInfo : {};
27
+ }
28
+
25
29
  function chromeRow(tab, page, win) {
26
30
  const fromTab = !!tab.incognito;
27
31
  const incognito = win.incognito || fromTab;
28
32
  const incognitoSource = win.incognito
29
33
  ? win.incognitoSource
30
34
  : (fromTab ? 'chrome.tabs' : '');
35
+ const info = ti(page);
31
36
  return {
32
37
  tabId: tab.id,
33
38
  windowId: tab.windowId,
@@ -37,12 +42,61 @@ function chromeRow(tab, page, win) {
37
42
  active: !!tab.active,
38
43
  discarded: !!tab.discarded,
39
44
  highlighted: !!tab.highlighted,
45
+ pinned: !!tab.pinned,
46
+ load: tab.status || tab.load || '',
40
47
  incognito,
41
48
  incognitoSource,
42
49
  attached: Boolean(page && page.sessionId),
50
+ audible: !!tab.audible,
51
+ muted: !!(tab.muted || (tab.mutedInfo && tab.mutedInfo.muted)),
52
+ mutedReason: tab.mutedReason || (tab.mutedInfo && tab.mutedInfo.reason) || '',
53
+ mutedExtensionId: tab.mutedExtensionId || (tab.mutedInfo && tab.mutedInfo.extensionId) || '',
54
+ autoDiscardable: tab.autoDiscardable !== false,
55
+ groupId: tab.groupId == null ? '' : tab.groupId,
56
+ openerTabId: tab.openerTabId == null ? '' : tab.openerTabId,
57
+ pendingUrl: tab.pendingUrl || '',
58
+ favIconUrl: tab.favIconUrl || '',
59
+ frozen: !!tab.frozen,
60
+ lastAccessed: tab.lastAccessed == null ? '' : tab.lastAccessed,
61
+ width: tab.width == null ? '' : tab.width,
62
+ height: tab.height == null ? '' : tab.height,
63
+ chromeSessionId: tab.chromeSessionId || tab.sessionIdChrome || '',
64
+ successorTabId: tab.successorTabId == null ? '' : tab.successorTabId,
65
+ splitViewId: tab.splitViewId == null ? '' : tab.splitViewId,
66
+ selected: !!tab.selected,
43
67
  sessionId: page && page.sessionId ? page.sessionId : '',
44
68
  targetId: page && page.targetId ? page.targetId : '',
45
69
  browserContextId: contextIdOf(page),
70
+ targetType: (page && (page.type || info.type)) || '',
71
+ subtype: info.subtype || '',
72
+ openerId: info.openerId || (page && page.openerId) || '',
73
+ openerFrameId: info.openerFrameId || (page && page.parentFrameId) || '',
74
+ canAccessOpener: info.canAccessOpener,
75
+ parent_sessionId: (page && page.parent_sessionId) || '',
76
+ parent_targetId: (page && (page.parent_targetId || page.parentFrameId)) || '',
77
+ parentFrameId: (page && page.parentFrameId) || '',
78
+ cdpAttached: info.attached,
79
+ };
80
+ }
81
+
82
+ function cdpRow(p, extra) {
83
+ const info = ti(p);
84
+ return {
85
+ tabId: p.tabId || 0,
86
+ sessionId: p.sessionId || '',
87
+ targetId: p.targetId || '',
88
+ title: p.title || '',
89
+ url: p.url || '',
90
+ type: p.type || info.type || '',
91
+ targetType: p.type || info.type || '',
92
+ subtype: info.subtype || '',
93
+ browserContextId: contextIdOf(p),
94
+ openerId: info.openerId || '',
95
+ openerFrameId: info.openerFrameId || p.parentFrameId || '',
96
+ parent_sessionId: p.parent_sessionId || '',
97
+ parent_targetId: p.parent_targetId || p.parentFrameId || '',
98
+ parentFrameId: p.parentFrameId || '',
99
+ ...extra,
46
100
  };
47
101
  }
48
102
 
@@ -80,11 +134,21 @@ function buildCatalog({ chromeTabs, chromeWindows, cdpPages, focusedWindowId, li
80
134
  wid,
81
135
  tabs.filter((t) => t.windowId === wid),
82
136
  );
137
+ const meta = (chromeWindows || []).find((w) => (w.windowId ?? w.id) === wid) || {};
83
138
  windowMap.set(wid, {
84
139
  windowId: wid,
85
140
  current: focusedWindowId != null && wid === focusedWindowId,
141
+ focused: !!meta.focused,
86
142
  incognito: win.incognito,
87
143
  incognitoSource: win.incognitoSource,
144
+ type: meta.type || '',
145
+ state: meta.state || '',
146
+ alwaysOnTop: !!meta.alwaysOnTop,
147
+ width: meta.width == null ? '' : meta.width,
148
+ height: meta.height == null ? '' : meta.height,
149
+ left: meta.left == null ? '' : meta.left,
150
+ top: meta.top == null ? '' : meta.top,
151
+ chromeSessionId: meta.chromeSessionId || meta.sessionId || '',
88
152
  tabs: [],
89
153
  });
90
154
  }
@@ -105,18 +169,13 @@ function buildCatalog({ chromeTabs, chromeWindows, cdpPages, focusedWindowId, li
105
169
  const attached = [];
106
170
  const stale = [];
107
171
  const ok = listTabsOk === true;
172
+ // listTabs "ok" + zero chrome.tabs while CDP pages exist is untrusted (MV3 SW
173
+ // empty query). Do not mark tab_gone / reap - that wipes /targets after status.
174
+ const chromeEmptyUntrusted = ok && tabs.length === 0 && enriched.length > 0;
108
175
  for (const p of enriched) {
109
176
  if (matched.has(p.sessionId)) continue;
110
- const row = {
111
- tabId: p.tabId || 0,
112
- sessionId: p.sessionId,
113
- targetId: p.targetId,
114
- title: p.title || '',
115
- url: p.url || '',
116
- type: p.type,
117
- browserContextId: contextIdOf(p),
118
- };
119
- if (ok && p.tabId && !chromeIds.has(p.tabId)) {
177
+ const row = cdpRow(p);
178
+ if (ok && !chromeEmptyUntrusted && p.tabId && !chromeIds.has(p.tabId)) {
120
179
  stale.push({ ...row, reason: 'tab_gone' });
121
180
  } else {
122
181
  attached.push(row);
@@ -125,12 +184,13 @@ function buildCatalog({ chromeTabs, chromeWindows, cdpPages, focusedWindowId, li
125
184
 
126
185
  return {
127
186
  focusedWindowId: focusedWindowId || 0,
128
- listTabsOk: ok,
187
+ listTabsOk: ok && !chromeEmptyUntrusted,
129
188
  windows,
130
189
  attached,
131
190
  stale,
132
191
  reapSessionIds: stale.map((s) => s.sessionId).filter(Boolean),
133
- iframes: part.iframes,
192
+ iframes: (part.iframes || []).map((p) => cdpRow(p)),
193
+ other: (part.other || []).map((p) => cdpRow(p)),
134
194
  pages: part.pages,
135
195
  };
136
196
  }