Identify browsers installed in the user directory

This commit is contained in:
被遗忘的记忆
2024-07-02 20:07:41 +08:00
committed by GitHub
parent 5545902480
commit 8693a75efd
+41 -38
View File
@@ -105,38 +105,29 @@ class YouProvider {
detectBrowser() { detectBrowser() {
const platform = os.platform(); const platform = os.platform();
let chromePath = null; let browsers = {
let edgePath = null; 'chrome': null,
'edge': null
};
if (platform === 'win32') { if (platform === 'win32') {
// Windows 系统 browsers.chrome = this.findWindowsBrowser('Chrome');
chromePath = this.findWindowsBrowser('Chrome'); browsers.edge = this.findWindowsBrowser('Edge');
edgePath = this.findWindowsBrowser('Edge');
} else if (platform === 'darwin') { } else if (platform === 'darwin') {
// macOS 系统 browsers.chrome = this.findMacOSBrowser('Google Chrome');
chromePath = this.findMacOSBrowser('Google Chrome'); browsers.edge = this.findMacOSBrowser('Microsoft Edge');
edgePath = this.findMacOSBrowser('Microsoft Edge');
} else if (platform === 'linux') { } else if (platform === 'linux') {
// Linux 系统 browsers.chrome = this.findLinuxBrowser('google-chrome');
chromePath = this.findLinuxBrowser('google-chrome'); browsers.edge = this.findLinuxBrowser('microsoft-edge');
edgePath = this.findLinuxBrowser('microsoft-edge');
} }
const preferredBrowser = this.preferredBrowser === 'auto' || this.preferredBrowser === undefined
? Object.keys(browsers).find(browser => browsers[browser])
: this.preferredBrowser;
if (this.preferredBrowser === 'chrome' && chromePath) { if (browsers[preferredBrowser]) {
console.log('使用Chrome浏览器'); console.log(`使用${preferredBrowser === 'chrome' ? 'Chrome' : 'Edge'}浏览器`);
return chromePath; return browsers[preferredBrowser];
} else if (this.preferredBrowser === 'edge' && edgePath) {
console.log('使用Edge浏览器');
return edgePath;
} else if (this.preferredBrowser === 'auto' || this.preferredBrowser === undefined) {
if (chromePath) {
console.log('使用Chrome浏览器');
return chromePath;
} else if (edgePath) {
console.log('使用Edge浏览器');
return edgePath;
}
} }
console.error('未找到Chrome或Edge浏览器,请确保已安装其中之一'); console.error('未找到Chrome或Edge浏览器,请确保已安装其中之一');
@@ -144,6 +135,12 @@ class YouProvider {
} }
findWindowsBrowser(browserName) { findWindowsBrowser(browserName) {
const regKeys = {
'Chrome': ['chrome.exe', 'Google\\Chrome'],
'Edge': ['msedge.exe', 'Microsoft\\Edge']
};
const [exeName, folderName] = regKeys[browserName];
const regQuery = (key) => { const regQuery = (key) => {
try { try {
return execSync(`reg query "${key}" /ve`).toString().trim().split('\r\n').pop().split(' ').pop(); return execSync(`reg query "${key}" /ve`).toString().trim().split('\r\n').pop().split(' ').pop();
@@ -152,27 +149,33 @@ class YouProvider {
} }
}; };
let browserPath = null; let browserPath = regQuery(`HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\${exeName}`) ||
if (browserName === 'Chrome') { regQuery(`HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\${exeName}`);
browserPath = regQuery('HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe');
} else if (browserName === 'Edge') {
browserPath = regQuery('HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\msedge.exe');
}
if (browserPath && fs.existsSync(browserPath)) { if (browserPath && fs.existsSync(browserPath)) {
return browserPath; return browserPath;
} }
// 如果注册表查询失败,尝试常见安装路径
const commonPaths = [ const commonPaths = [
`C:\\Program Files\\${browserName}\\Application\\${browserName.toLowerCase()}.exe`, `C:\\Program Files\\${browserName}\\Application\\${exeName}`,
`C:\\Program Files (x86)\\${browserName}\\Application\\${browserName.toLowerCase()}.exe`, `C:\\Program Files (x86)\\${browserName}\\Application\\${exeName}`,
`${process.env.LOCALAPPDATA}\\${browserName}\\Application\\${browserName.toLowerCase()}.exe`, `${process.env.LOCALAPPDATA}\\${browserName}\\Application\\${exeName}`,
`${process.env.USERPROFILE}\\AppData\\Local\\${browserName}\\Application\\${exeName}`,
]; ];
for (const path of commonPaths) { const foundPath = commonPaths.find(path => fs.existsSync(path));
if (fs.existsSync(path)) { if (foundPath) {
return path; return foundPath;
}
const userAppDataPath = process.env.LOCALAPPDATA || `${process.env.USERPROFILE}\\AppData\\Local`;
const appDataPath = path.join(userAppDataPath, folderName, 'Application');
if (fs.existsSync(appDataPath)) {
const files = fs.readdirSync(appDataPath);
const exePath = files.find(file => file.toLowerCase() === exeName.toLowerCase());
if (exePath) {
return path.join(appDataPath, exePath);
} }
} }