Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse an existing window #292

Open
aleclarson opened this issue Feb 24, 2023 · 1 comment
Open

Reuse an existing window #292

aleclarson opened this issue Feb 24, 2023 · 1 comment

Comments

@aleclarson
Copy link

I want to open a new tab in an existing window of a Launcher instance. Any guidance here would be awesome :)

@aleclarson
Copy link
Author

aleclarson commented Feb 24, 2023

Hmm, I guess devtools-protocol package is the way?

(Disclaimer: ChatGPT wrote this)

const CDP = require('devtools-protocol');
const WebSocket = require('ws');

// Replace with the process ID of the Chrome instance you want to target
const processId = 1234;

// Obtain the WebSocket URL for the instance
const resp = await fetch(`http://localhost:${port}/json/list`);
const targets = await resp.json();
const target = targets.find(t => t.pid === processId);
const webSocketUrl = target.webSocketDebuggerUrl;

// Connect to the instance using the WebSocket URL
const socket = new WebSocket(webSocketUrl);
const client = new CDP(socket);

// Send a message to open a new tab
const {targetId} = await client.send('Target.createTarget', {url: 'https://www.example.com/'});

// Wait for a response and print the ID of the newly opened tab
const {frameId} = await client.waitForEvent('Page.frameAttached');
console.log(frameId);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant