Skip to content

Commit 895d8bd

Browse files
committed
[cdp] update chobitsu, implement Target.closeTarget
1 parent 92f1700 commit 895d8bd

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

frontend/src/CDP.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ class CDPServer {
153153
import type Protocol from "devtools-protocol";
154154
import { browser } from "./main";
155155
import type { Tab } from "./Tab";
156+
157+
function createTargetInfo(tab: Tab): Protocol.Target.TargetInfo {
158+
return {
159+
browserContextId: "0",
160+
targetId: String(tab.id),
161+
type: "page",
162+
title: tab.title || "New Tab",
163+
url: tab.url.href,
164+
attached: true,
165+
canAccessOpener: false,
166+
};
167+
}
168+
156169
const Scopes = {
157170
Browser: {
158171
getVersion(): Protocol.Browser.GetVersionResponse {
@@ -189,15 +202,7 @@ const Scopes = {
189202
{
190203
sessionId: sessionid,
191204
waitingForDebugger: false,
192-
targetInfo: {
193-
browserContextId: "0",
194-
targetId: String(tab.id),
195-
type: "page",
196-
title: "test",
197-
url: "https://google.com",
198-
attached: false,
199-
canAccessOpener: false,
200-
},
205+
targetInfo: createTargetInfo(tab),
201206
}
202207
);
203208

@@ -206,9 +211,21 @@ const Scopes = {
206211
};
207212
},
208213

209-
async setAutoAttach(params: Protocol.Target.SetAutoAttachRequest) {
210-
console.log("Target.setAutoAttach", params);
214+
async closeTarget(params: Protocol.Target.CloseTargetRequest) {
215+
let tab = browser.tabs.find((x) => x.id === Number(params.targetId));
216+
if (!tab) {
217+
throw new Error(`Target ${params.targetId} not found`);
218+
}
219+
browser.destroyTab(tab);
220+
server.emit<Protocol.Target.TargetDestroyedEvent>(
221+
"Target.targetDestroyed",
222+
{
223+
targetId: String(tab.id),
224+
}
225+
);
226+
},
211227

228+
async setAutoAttach(params: Protocol.Target.SetAutoAttachRequest) {
212229
return {};
213230
},
214231
getTargetInfo(

pnpm-lock.yaml

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)