This repository was archived by the owner on Apr 16, 2026. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed
Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7+ import { execSync } from 'node:child_process' ;
78import fs from 'node:fs' ;
89import os from 'node:os' ;
910import path from 'node:path' ;
@@ -148,6 +149,24 @@ interface McpLaunchOptions {
148149 enableExtensions ?: boolean ;
149150}
150151
152+ export function detectDisplay ( ) : void {
153+ // Only detect display on Linux/UNIX.
154+ if ( os . platform ( ) === 'win32' || os . platform ( ) === 'darwin' ) {
155+ return ;
156+ }
157+ if ( ! process . env [ 'DISPLAY' ] ) {
158+ try {
159+ const result = execSync (
160+ `ps -u $(id -u) -o pid= | xargs -I{} cat /proc/{}/environ 2>/dev/null | tr '\\0' '\\n' | grep -m1 '^DISPLAY=' | cut -d= -f2` ,
161+ ) ;
162+ const display = result . toString ( 'utf8' ) . trim ( ) ;
163+ process . env [ 'DISPLAY' ] = display ;
164+ } catch {
165+ // no-op
166+ }
167+ }
168+ }
169+
151170export async function launch ( options : McpLaunchOptions ) : Promise < Browser > {
152171 const { channel, executablePath, headless, isolated} = options ;
153172 const profileDirName =
@@ -189,6 +208,10 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
189208 : 'chrome' ;
190209 }
191210
211+ if ( ! headless ) {
212+ detectDisplay ( ) ;
213+ }
214+
192215 try {
193216 const browser = await puppeteer . launch ( {
194217 channel : puppeteerChannel ,
Original file line number Diff line number Diff line change @@ -11,9 +11,13 @@ import {describe, it} from 'node:test';
1111
1212import { executablePath } from 'puppeteer' ;
1313
14- import { ensureBrowserConnected , launch } from '../src/browser.js' ;
14+ import { detectDisplay , ensureBrowserConnected , launch } from '../src/browser.js' ;
1515
1616describe ( 'browser' , ( ) => {
17+ it ( 'detects display does not crash' , ( ) => {
18+ detectDisplay ( ) ;
19+ } ) ;
20+
1721 it ( 'cannot launch multiple times with the same profile' , async ( ) => {
1822 const tmpDir = os . tmpdir ( ) ;
1923 const folderPath = path . join ( tmpDir , `temp-folder-${ crypto . randomUUID ( ) } ` ) ;
You can’t perform that action at this time.
0 commit comments