glidercli 0.4.19 → 0.4.31
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 +18 -6
- package/bin/glider.js +378 -61
- package/docs/setup.json +6 -2
- package/docs/tab-identity.scorched.json +2042 -0
- package/lib/bwindow.js +8 -6
- package/lib/cookie-jar.js +233 -0
- package/lib/paths.js +25 -0
- package/lib/tab-catalog.js +67 -10
- package/lib/tab-map.js +282 -0
- package/lib/tab-tui.js +179 -0
- package/package.json +3 -3
package/lib/bwindow.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* bwindow.js -
|
|
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
|
|
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
|
-
*
|
|
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,
|
package/lib/tab-catalog.js
CHANGED
|
@@ -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
|
}
|
|
@@ -107,15 +171,7 @@ function buildCatalog({ chromeTabs, chromeWindows, cdpPages, focusedWindowId, li
|
|
|
107
171
|
const ok = listTabsOk === true;
|
|
108
172
|
for (const p of enriched) {
|
|
109
173
|
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
|
-
};
|
|
174
|
+
const row = cdpRow(p);
|
|
119
175
|
if (ok && p.tabId && !chromeIds.has(p.tabId)) {
|
|
120
176
|
stale.push({ ...row, reason: 'tab_gone' });
|
|
121
177
|
} else {
|
|
@@ -130,7 +186,8 @@ function buildCatalog({ chromeTabs, chromeWindows, cdpPages, focusedWindowId, li
|
|
|
130
186
|
attached,
|
|
131
187
|
stale,
|
|
132
188
|
reapSessionIds: stale.map((s) => s.sessionId).filter(Boolean),
|
|
133
|
-
iframes: part.iframes,
|
|
189
|
+
iframes: (part.iframes || []).map((p) => cdpRow(p)),
|
|
190
|
+
other: (part.other || []).map((p) => cdpRow(p)),
|
|
134
191
|
pages: part.pages,
|
|
135
192
|
};
|
|
136
193
|
}
|
package/lib/tab-map.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { stateDir } = require('./paths.js');
|
|
6
|
+
|
|
7
|
+
function mapPath() {
|
|
8
|
+
return path.join(stateDir(), 'tab-map.json');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function rowHandle(t, handle) {
|
|
12
|
+
return {
|
|
13
|
+
handle: String(handle),
|
|
14
|
+
kind: t.kind || 'tab',
|
|
15
|
+
tabId: t.tabId == null ? 0 : t.tabId,
|
|
16
|
+
sessionId: t.sessionId || '',
|
|
17
|
+
targetId: t.targetId || '',
|
|
18
|
+
windowId: t.windowId == null ? 0 : t.windowId,
|
|
19
|
+
index: t.index == null ? 0 : t.index,
|
|
20
|
+
title: t.title || '',
|
|
21
|
+
url: t.url || '',
|
|
22
|
+
attached: !!t.attached,
|
|
23
|
+
active: !!t.active,
|
|
24
|
+
discarded: !!t.discarded,
|
|
25
|
+
highlighted: !!t.highlighted,
|
|
26
|
+
pinned: !!t.pinned,
|
|
27
|
+
load: t.load || '',
|
|
28
|
+
incognito: !!t.incognito,
|
|
29
|
+
current: !!t.current,
|
|
30
|
+
audible: !!t.audible,
|
|
31
|
+
muted: !!t.muted,
|
|
32
|
+
autoDiscardable: t.autoDiscardable !== false,
|
|
33
|
+
groupId: t.groupId == null ? '' : t.groupId,
|
|
34
|
+
openerTabId: t.openerTabId == null ? '' : t.openerTabId,
|
|
35
|
+
pendingUrl: t.pendingUrl || '',
|
|
36
|
+
favIconUrl: t.favIconUrl || '',
|
|
37
|
+
frozen: !!t.frozen,
|
|
38
|
+
lastAccessed: t.lastAccessed == null ? '' : t.lastAccessed,
|
|
39
|
+
width: t.width == null ? '' : t.width,
|
|
40
|
+
height: t.height == null ? '' : t.height,
|
|
41
|
+
chromeSessionId: t.chromeSessionId || '',
|
|
42
|
+
successorTabId: t.successorTabId == null ? '' : t.successorTabId,
|
|
43
|
+
splitViewId: t.splitViewId == null ? '' : t.splitViewId,
|
|
44
|
+
selected: !!t.selected,
|
|
45
|
+
mutedReason: t.mutedReason || '',
|
|
46
|
+
mutedExtensionId: t.mutedExtensionId || '',
|
|
47
|
+
targetType: t.targetType || t.type || '',
|
|
48
|
+
subtype: t.subtype || '',
|
|
49
|
+
openerId: t.openerId || '',
|
|
50
|
+
openerFrameId: t.openerFrameId || '',
|
|
51
|
+
parentFrameId: t.parentFrameId || '',
|
|
52
|
+
canAccessOpener: !!t.canAccessOpener,
|
|
53
|
+
cdpAttached: t.cdpAttached,
|
|
54
|
+
browserContextId: t.browserContextId || '',
|
|
55
|
+
cookieStoreId: t.cookieStoreId == null ? '' : String(t.cookieStoreId),
|
|
56
|
+
parent_sessionId: t.parent_sessionId || '',
|
|
57
|
+
parent_targetId: t.parent_targetId || '',
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function buildMap(catalog) {
|
|
62
|
+
const handles = {};
|
|
63
|
+
let n = 0;
|
|
64
|
+
for (const w of catalog.windows || []) {
|
|
65
|
+
for (const t of w.tabs || []) {
|
|
66
|
+
n += 1;
|
|
67
|
+
handles[String(n)] = rowHandle({
|
|
68
|
+
...t,
|
|
69
|
+
kind: 'tab',
|
|
70
|
+
windowId: t.windowId || w.windowId,
|
|
71
|
+
current: !!w.current,
|
|
72
|
+
incognito: t.incognito || w.incognito,
|
|
73
|
+
}, n);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
(catalog.attached || []).forEach((t, i) => {
|
|
77
|
+
const h = `A${i + 1}`;
|
|
78
|
+
handles[h] = rowHandle({ ...t, kind: 'attached' }, h);
|
|
79
|
+
});
|
|
80
|
+
(catalog.stale || []).forEach((t, i) => {
|
|
81
|
+
const h = `S${i + 1}`;
|
|
82
|
+
handles[h] = rowHandle({ ...t, kind: 'stale' }, h);
|
|
83
|
+
});
|
|
84
|
+
(catalog.iframes || []).forEach((t, i) => {
|
|
85
|
+
const h = `I${i + 1}`;
|
|
86
|
+
handles[h] = rowHandle({ ...t, kind: 'iframe', attached: true }, h);
|
|
87
|
+
});
|
|
88
|
+
(catalog.other || []).forEach((t, i) => {
|
|
89
|
+
const h = `O${i + 1}`;
|
|
90
|
+
handles[h] = rowHandle({ ...t, kind: 'other', attached: true }, h);
|
|
91
|
+
});
|
|
92
|
+
return { handles };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function writeMap(catalog) {
|
|
96
|
+
const payload = { ...buildMap(catalog), writtenAt: Date.now() };
|
|
97
|
+
fs.writeFileSync(mapPath(), `${JSON.stringify(payload, null, 2)}\n`);
|
|
98
|
+
return payload;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function readMap() {
|
|
102
|
+
const p = mapPath();
|
|
103
|
+
if (!fs.existsSync(p)) return { handles: {} };
|
|
104
|
+
try {
|
|
105
|
+
const data = JSON.parse(fs.readFileSync(p, 'utf8'));
|
|
106
|
+
return data && data.handles ? data : { handles: {} };
|
|
107
|
+
} catch {
|
|
108
|
+
return { handles: {} };
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function resolveRef(opts) {
|
|
113
|
+
return resolveChain(opts);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function resolveChain({
|
|
117
|
+
current, incognito, windowId, index, handle, tabId, targetId, sessionId, browserContextId, iframe,
|
|
118
|
+
active, attached, discarded, highlighted, pinned, status, title, focusedWindowId, openerTabId, groupId,
|
|
119
|
+
cookieStoreId, map,
|
|
120
|
+
} = {}) {
|
|
121
|
+
const src = map && map.handles ? map : readMap();
|
|
122
|
+
let rows = Object.values(src.handles || {});
|
|
123
|
+
const via = [];
|
|
124
|
+
const fail = (err) => ({ row: null, via: via.join('>'), error: err });
|
|
125
|
+
if (current) {
|
|
126
|
+
rows = rows.filter((r) => r.current);
|
|
127
|
+
via.push('current');
|
|
128
|
+
if (!rows.length) return fail('current');
|
|
129
|
+
}
|
|
130
|
+
if (incognito) {
|
|
131
|
+
rows = rows.filter((r) => r.incognito);
|
|
132
|
+
via.push('incognito');
|
|
133
|
+
if (!rows.length) return fail('incognito');
|
|
134
|
+
}
|
|
135
|
+
if (windowId != null && windowId !== '') {
|
|
136
|
+
const id = Number(windowId);
|
|
137
|
+
rows = rows.filter((r) => Number(r.windowId) === id);
|
|
138
|
+
via.push('window');
|
|
139
|
+
if (!rows.length) return fail('window');
|
|
140
|
+
}
|
|
141
|
+
if (focusedWindowId != null && focusedWindowId !== '') {
|
|
142
|
+
const id = Number(focusedWindowId);
|
|
143
|
+
rows = rows.filter((r) => Number(r.windowId) === id);
|
|
144
|
+
via.push('focusedWindowId');
|
|
145
|
+
if (!rows.length) return fail('focusedWindowId');
|
|
146
|
+
}
|
|
147
|
+
if (active) {
|
|
148
|
+
rows = rows.filter((r) => r.active);
|
|
149
|
+
via.push('active');
|
|
150
|
+
if (!rows.length) return fail('active');
|
|
151
|
+
}
|
|
152
|
+
if (attached) {
|
|
153
|
+
rows = rows.filter((r) => r.attached);
|
|
154
|
+
via.push('attached');
|
|
155
|
+
if (!rows.length) return fail('attached');
|
|
156
|
+
}
|
|
157
|
+
if (discarded) {
|
|
158
|
+
rows = rows.filter((r) => r.discarded);
|
|
159
|
+
via.push('discarded');
|
|
160
|
+
if (!rows.length) return fail('discarded');
|
|
161
|
+
}
|
|
162
|
+
if (highlighted) {
|
|
163
|
+
rows = rows.filter((r) => r.highlighted);
|
|
164
|
+
via.push('highlighted');
|
|
165
|
+
if (!rows.length) return fail('highlighted');
|
|
166
|
+
}
|
|
167
|
+
if (pinned) {
|
|
168
|
+
rows = rows.filter((r) => r.pinned);
|
|
169
|
+
via.push('pinned');
|
|
170
|
+
if (!rows.length) return fail('pinned');
|
|
171
|
+
}
|
|
172
|
+
if (status != null && status !== '') {
|
|
173
|
+
const want = String(status).toLowerCase();
|
|
174
|
+
rows = rows.filter((r) => String(r.load || '').toLowerCase() === want);
|
|
175
|
+
via.push('status');
|
|
176
|
+
if (!rows.length) return fail('status');
|
|
177
|
+
}
|
|
178
|
+
if (title != null && title !== '') {
|
|
179
|
+
const needle = String(title).toLowerCase();
|
|
180
|
+
rows = rows.filter((r) => String(r.title || '').toLowerCase().includes(needle));
|
|
181
|
+
via.push('title');
|
|
182
|
+
if (!rows.length) return fail('title');
|
|
183
|
+
}
|
|
184
|
+
if (openerTabId != null && openerTabId !== '') {
|
|
185
|
+
const id = Number(openerTabId);
|
|
186
|
+
rows = rows.filter((r) => Number(r.openerTabId) === id);
|
|
187
|
+
via.push('openerTabId');
|
|
188
|
+
if (!rows.length) return fail('openerTabId');
|
|
189
|
+
}
|
|
190
|
+
if (groupId != null && groupId !== '') {
|
|
191
|
+
const id = String(groupId);
|
|
192
|
+
rows = rows.filter((r) => String(r.groupId) === id);
|
|
193
|
+
via.push('groupId');
|
|
194
|
+
if (!rows.length) return fail('groupId');
|
|
195
|
+
}
|
|
196
|
+
if (cookieStoreId != null && cookieStoreId !== '') {
|
|
197
|
+
const id = String(cookieStoreId);
|
|
198
|
+
rows = rows.filter((r) => String(r.cookieStoreId || '') === id);
|
|
199
|
+
via.push('cookieStoreId');
|
|
200
|
+
if (!rows.length) return fail('cookieStoreId');
|
|
201
|
+
}
|
|
202
|
+
if (index != null && index !== '') {
|
|
203
|
+
const n = Number(index);
|
|
204
|
+
rows = rows.filter((r) => Number(r.index) === n);
|
|
205
|
+
via.push('index');
|
|
206
|
+
if (!rows.length) return fail('index');
|
|
207
|
+
}
|
|
208
|
+
if (iframe) {
|
|
209
|
+
rows = rows.filter((r) => r.kind === 'iframe');
|
|
210
|
+
via.push('iframe');
|
|
211
|
+
if (!rows.length) return fail('iframe');
|
|
212
|
+
}
|
|
213
|
+
if (handle != null && handle !== '') {
|
|
214
|
+
const hit = src.handles[String(handle)];
|
|
215
|
+
if (!hit) return fail('handle');
|
|
216
|
+
if (rows.length && !rows.some((r) => r.handle === hit.handle)) {
|
|
217
|
+
via.push('handle');
|
|
218
|
+
return fail('handle_not_in_window');
|
|
219
|
+
}
|
|
220
|
+
rows = [hit];
|
|
221
|
+
via.push('handle');
|
|
222
|
+
}
|
|
223
|
+
if (tabId != null && tabId !== '') {
|
|
224
|
+
const id = Number(tabId);
|
|
225
|
+
const hit = rows.find((r) => Number(r.tabId) === id);
|
|
226
|
+
if (!hit) {
|
|
227
|
+
via.push('tab');
|
|
228
|
+
return fail('tab');
|
|
229
|
+
}
|
|
230
|
+
rows = [hit];
|
|
231
|
+
via.push('tab');
|
|
232
|
+
}
|
|
233
|
+
if (targetId) {
|
|
234
|
+
const hit = rows.find((r) => r.targetId === targetId);
|
|
235
|
+
if (!hit) {
|
|
236
|
+
via.push('target');
|
|
237
|
+
return fail('target');
|
|
238
|
+
}
|
|
239
|
+
rows = [hit];
|
|
240
|
+
via.push('target');
|
|
241
|
+
}
|
|
242
|
+
if (sessionId) {
|
|
243
|
+
const hit = rows.find((r) => r.sessionId === sessionId);
|
|
244
|
+
if (!hit) {
|
|
245
|
+
via.push('session');
|
|
246
|
+
return fail('session');
|
|
247
|
+
}
|
|
248
|
+
rows = [hit];
|
|
249
|
+
via.push('session');
|
|
250
|
+
}
|
|
251
|
+
if (browserContextId) {
|
|
252
|
+
const hit = rows.find((r) => r.browserContextId === browserContextId);
|
|
253
|
+
if (!hit) {
|
|
254
|
+
via.push('context');
|
|
255
|
+
return fail('context');
|
|
256
|
+
}
|
|
257
|
+
rows = [hit];
|
|
258
|
+
via.push('context');
|
|
259
|
+
}
|
|
260
|
+
if (rows.length === 1) return { row: rows[0], via: via.join('>') || 'unique' };
|
|
261
|
+
const coarse = via.includes('window') || via.includes('focusedWindowId') || via.includes('current') || via.includes('incognito');
|
|
262
|
+
if (rows.length > 1 && coarse && !handle && (tabId == null || tabId === '') && !sessionId && !targetId && (index == null || index === '')) {
|
|
263
|
+
const active = rows.find((r) => r.active && r.sessionId && r.kind !== 'iframe');
|
|
264
|
+
if (active) return { row: active, via: via.concat('active').join('>') };
|
|
265
|
+
}
|
|
266
|
+
if (rows.length > 1) return { row: null, via: via.join('>'), error: 'ambiguous' };
|
|
267
|
+
return { row: null, via: via.join('>'), error: 'empty' };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function lookslikeHandle(token) {
|
|
271
|
+
return typeof token === 'string' && /^(?:\d+|[ASI]\d+)$/i.test(token);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
module.exports = {
|
|
275
|
+
mapPath,
|
|
276
|
+
buildMap,
|
|
277
|
+
writeMap,
|
|
278
|
+
readMap,
|
|
279
|
+
resolveRef,
|
|
280
|
+
resolveChain,
|
|
281
|
+
lookslikeHandle,
|
|
282
|
+
};
|