chrome-launcher icon indicating copy to clipboard operation
chrome-launcher copied to clipboard

Reuse an existing window

Open aleclarson opened this issue 1 year ago • 1 comments

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

aleclarson avatar Feb 24 '23 16:02 aleclarson

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);

aleclarson avatar Feb 24 '23 17:02 aleclarson