-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathdetect-browser.js
More file actions
72 lines (62 loc) · 2.5 KB
/
Copy pathdetect-browser.js
File metadata and controls
72 lines (62 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
// Detects the best available Chromium-based browser on the system.
// Returns a Playwright channel name ('msedge', 'chrome', 'chromium').
// Used by the Playwright MCP launcher and the axe-core audit script.
const { execFileSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
function exists(filePath) {
try { return fs.existsSync(filePath); } catch { return false; }
}
function whichExists(cmd) {
try {
// This helper only runs in the Linux branch below. Pass the fixed utility and
// candidate name separately so a future candidate cannot become shell syntax.
execFileSync('which', [cmd], { stdio: 'ignore', shell: false });
return true;
} catch {
return false;
}
}
function detectBrowser() {
const platform = os.platform();
if (platform === 'win32') {
const localAppData = process.env.LOCALAPPDATA || '';
const programFiles = process.env.PROGRAMFILES || '';
const programFilesX86 = process.env['PROGRAMFILES(X86)'] || '';
// Edge — pre-installed on all Windows 10/11 machines
const edgePaths = [
path.join(programFilesX86, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
path.join(programFiles, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
path.join(localAppData, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
];
for (const p of edgePaths) {
if (exists(p)) return 'msedge';
}
// Chrome
const chromePaths = [
path.join(localAppData, 'Google', 'Chrome', 'Application', 'chrome.exe'),
path.join(programFiles, 'Google', 'Chrome', 'Application', 'chrome.exe'),
path.join(programFilesX86, 'Google', 'Chrome', 'Application', 'chrome.exe'),
];
for (const p of chromePaths) {
if (exists(p)) return 'chrome';
}
} else if (platform === 'darwin') {
// macOS
if (exists('/Applications/Google Chrome.app')) return 'chrome';
if (exists('/Applications/Microsoft Edge.app')) return 'msedge';
} else {
// Linux
if (whichExists('google-chrome')) return 'chrome';
if (whichExists('google-chrome-stable')) return 'chrome';
if (whichExists('microsoft-edge')) return 'msedge';
if (whichExists('microsoft-edge-stable')) return 'msedge';
if (whichExists('chromium-browser')) return 'chromium';
if (whichExists('chromium')) return 'chromium';
}
// Fallback: Playwright's bundled Chromium (requires `npx playwright install chromium`)
return 'chromium';
}
module.exports = { detectBrowser };