豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 1746ed9

Browse files
authored
fix: detect X server display on Linux (#1027)
in headful mode on Linux we should try detecting X server if DISPLAY is not set.
1 parent 0106865 commit 1746ed9

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/browser.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
import {execSync} from 'node:child_process';
78
import fs from 'node:fs';
89
import os from 'node:os';
910
import 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+
151170
export 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,

tests/browser.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import {describe, it} from 'node:test';
1111

1212
import {executablePath} from 'puppeteer';
1313

14-
import {ensureBrowserConnected, launch} from '../src/browser.js';
14+
import {detectDisplay, ensureBrowserConnected, launch} from '../src/browser.js';
1515

1616
describe('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()}`);

0 commit comments

Comments
 (0)