Improve browser path recognition

This commit is contained in:
被遗忘的记忆
2024-07-02 15:42:32 +08:00
committed by GitHub
parent 8ee2509cce
commit 5545902480
+75 -21
View File
@@ -105,40 +105,35 @@ class YouProvider {
detectBrowser() { detectBrowser() {
const platform = os.platform(); const platform = os.platform();
let chromePath; let chromePath = null;
let edgePath; let edgePath = null;
if (platform === 'win32') { if (platform === 'win32') {
chromePath = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'; // Windows 系统
edgePath = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe'; chromePath = this.findWindowsBrowser('Chrome');
edgePath = this.findWindowsBrowser('Edge');
} else if (platform === 'darwin') { } else if (platform === 'darwin') {
chromePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; // macOS 系统
edgePath = '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge'; chromePath = this.findMacOSBrowser('Google Chrome');
edgePath = this.findMacOSBrowser('Microsoft Edge');
} else if (platform === 'linux') { } else if (platform === 'linux') {
try { // Linux 系统
chromePath = execSync('which google-chrome').toString().trim(); chromePath = this.findLinuxBrowser('google-chrome');
} catch (error) { edgePath = this.findLinuxBrowser('microsoft-edge');
chromePath = null;
}
try {
edgePath = execSync('which microsoft-edge').toString().trim();
} catch (error) {
edgePath = null;
}
} }
// 根据preferredBrowser的值来决定使用哪个浏览器
if (this.preferredBrowser === 'chrome' && fs.existsSync(chromePath)) { if (this.preferredBrowser === 'chrome' && chromePath) {
console.log('使用Chrome浏览器'); console.log('使用Chrome浏览器');
return chromePath; return chromePath;
} else if (this.preferredBrowser === 'edge' && fs.existsSync(edgePath)) { } else if (this.preferredBrowser === 'edge' && edgePath) {
console.log('使用Edge浏览器'); console.log('使用Edge浏览器');
return edgePath; return edgePath;
} else if (this.preferredBrowser === 'auto' || this.preferredBrowser === undefined) { } else if (this.preferredBrowser === 'auto' || this.preferredBrowser === undefined) {
if (fs.existsSync(chromePath)) { if (chromePath) {
console.log('使用Chrome浏览器'); console.log('使用Chrome浏览器');
return chromePath; return chromePath;
} else if (fs.existsSync(edgePath)) { } else if (edgePath) {
console.log('使用Edge浏览器'); console.log('使用Edge浏览器');
return edgePath; return edgePath;
} }
@@ -148,6 +143,65 @@ class YouProvider {
process.exit(1); process.exit(1);
} }
findWindowsBrowser(browserName) {
const regQuery = (key) => {
try {
return execSync(`reg query "${key}" /ve`).toString().trim().split('\r\n').pop().split(' ').pop();
} catch (error) {
return null;
}
};
let browserPath = null;
if (browserName === 'Chrome') {
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)) {
return browserPath;
}
// 如果注册表查询失败,尝试常见安装路径
const commonPaths = [
`C:\\Program Files\\${browserName}\\Application\\${browserName.toLowerCase()}.exe`,
`C:\\Program Files (x86)\\${browserName}\\Application\\${browserName.toLowerCase()}.exe`,
`${process.env.LOCALAPPDATA}\\${browserName}\\Application\\${browserName.toLowerCase()}.exe`,
];
for (const path of commonPaths) {
if (fs.existsSync(path)) {
return path;
}
}
return null;
}
findMacOSBrowser(browserName) {
const paths = [
`/Applications/${browserName}.app/Contents/MacOS/${browserName}`,
`${os.homedir()}/Applications/${browserName}.app/Contents/MacOS/${browserName}`,
];
for (const path of paths) {
if (fs.existsSync(path)) {
return path;
}
}
return null;
}
findLinuxBrowser(browserName) {
try {
return execSync(`which ${browserName}`).toString().trim();
} catch (error) {
return null;
}
}
async getCompletion(username, messages, stream = false, proxyModel, useCustomMode = false) { async getCompletion(username, messages, stream = false, proxyModel, useCustomMode = false) {
const session = this.sessions[username]; const session = this.sessions[username];
if (!session || !session.valid) { if (!session || !session.valid) {