diff --git a/extension-playwright/src/background.ts b/extension-playwright/src/background.ts index e304c93..92dabbf 100644 --- a/extension-playwright/src/background.ts +++ b/extension-playwright/src/background.ts @@ -1,18 +1,3 @@ -/** - * Copyright (c) Microsoft Corporation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ import { RelayConnection, debugLog } from './relayConnection'; diff --git a/extension-playwright/src/relayConnection.ts b/extension-playwright/src/relayConnection.ts index dc6f55e..5a34894 100644 --- a/extension-playwright/src/relayConnection.ts +++ b/extension-playwright/src/relayConnection.ts @@ -277,13 +277,38 @@ export class RelayConnection { if (message.method === 'forwardCDPCommand') { const { sessionId, method, params } = message.params; + if (method === 'Target.createTarget') { + const url = params?.url || 'about:blank'; + debugLog('Creating new tab with URL:', url); + + const tab = await chrome.tabs.create({ url, active: false }); + if (!tab.id) { + throw new Error('Failed to create tab'); + } + + debugLog('Created tab:', tab.id, 'waiting for it to load...'); + + // Wait a bit for tab to initialize + await new Promise(resolve => setTimeout(resolve, 100)); + + // Attach to the new tab + const targetInfo = await this.attachTab(tab.id); + + return { targetId: targetInfo.targetId }; + } + if (method === 'Target.closeTarget' && params?.targetId) { + debugLog('Closing target:', params.targetId); + for (const [tabId, tab] of this._attachedTabs) { if (tab.targetId === params.targetId) { + debugLog('Found tab to close:', tabId); await chrome.tabs.remove(tabId); return { success: true }; } } + + debugLog('Target not found:', params.targetId); throw new Error(`Target not found: ${params.targetId}`); } diff --git a/playwriter/scripts/extension-new-page.ts b/playwriter/scripts/extension-new-page.ts index 69b5833..ccdc568 100644 --- a/playwriter/scripts/extension-new-page.ts +++ b/playwriter/scripts/extension-new-page.ts @@ -12,12 +12,16 @@ async function main() { for (const context of contexts) { const pages = context.pages() console.log(`Context has ${pages.length} page(s):`) -// Create a new page + // Create a new page const newPage = await context.newPage() // Evaluate a sum (e.g., 2 + 3) and log the result const sumResult = await newPage.evaluate(() => 2 + 3) console.log(`Evaluated sum 2 + 3 = ${sumResult}`) + // Sleep 1 second + await new Promise((resolve) => setTimeout(resolve, 1000)) + // Close the page + await newPage.close() } } diff --git a/playwriter/src/extension/cdp-relay.ts b/playwriter/src/extension/cdp-relay.ts index d7dfa87..0ca47ed 100644 --- a/playwriter/src/extension/cdp-relay.ts +++ b/playwriter/src/extension/cdp-relay.ts @@ -195,8 +195,18 @@ export async function startRelayServer({ port = 9988 }: { port?: number } = {}) } } + case 'Target.createTarget': { + return await sendToExtension({ + method: 'forwardCDPCommand', + params: { method, params } + }) + } + case 'Target.closeTarget': { - break + return await sendToExtension({ + method: 'forwardCDPCommand', + params: { method, params } + }) } }