feat: CDP bridge batch support - cookies/tabs/cdp mixed commands, lazy attach, $N chain refs, file upload & screenshot verified
This commit is contained in:
@@ -10,6 +10,10 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||
handleCDP(msg, sender).then(sendResponse);
|
||||
return true;
|
||||
}
|
||||
if (msg.action === 'batch') {
|
||||
handleBatch(msg, sender).then(sendResponse);
|
||||
return true;
|
||||
}
|
||||
if (msg.action === 'tabs') {
|
||||
(async () => {
|
||||
try {
|
||||
@@ -44,6 +48,38 @@ async function handleCookies(msg, sender) {
|
||||
}
|
||||
}
|
||||
|
||||
async function handleBatch(msg, sender) {
|
||||
const R = [];
|
||||
let attached = null;
|
||||
const resolve$N = (params) => JSON.parse(JSON.stringify(params || {}).replace(/"\$(\d+)\.([^"]+)"/g,
|
||||
(_, i, path) => { let v = R[+i]; for (const k of path.split('.')) v = v[k]; return JSON.stringify(v); }));
|
||||
try {
|
||||
for (const c of msg.commands) {
|
||||
if (c.cmd === 'cookies') {
|
||||
R.push(await handleCookies(c, sender));
|
||||
} else if (c.cmd === 'tabs') {
|
||||
const tabs = await chrome.tabs.query({});
|
||||
R.push({ ok: true, data: tabs.map(t => ({ id: t.id, url: t.url, title: t.title, active: t.active, windowId: t.windowId })) });
|
||||
} else if (c.cmd === 'cdp') {
|
||||
const tabId = c.tabId || msg.tabId || sender.tab?.id;
|
||||
if (attached !== tabId) {
|
||||
if (attached) { await chrome.debugger.detach({ tabId: attached }); attached = null; }
|
||||
await chrome.debugger.attach({ tabId }, '1.3');
|
||||
attached = tabId;
|
||||
}
|
||||
R.push(await chrome.debugger.sendCommand({ tabId }, c.method, resolve$N(c.params)));
|
||||
} else {
|
||||
R.push({ ok: false, error: 'unknown cmd: ' + c.cmd });
|
||||
}
|
||||
}
|
||||
if (attached) await chrome.debugger.detach({ tabId: attached });
|
||||
return { ok: true, results: R };
|
||||
} catch (e) {
|
||||
if (attached) try { await chrome.debugger.detach({ tabId: attached }); } catch (_) {}
|
||||
return { ok: false, error: e.message, results: R };
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCDP(msg, sender) {
|
||||
const tabId = msg.tabId || sender.tab?.id;
|
||||
if (!tabId) return { ok: false, error: 'no tabId' };
|
||||
|
||||
Reference in New Issue
Block a user