Skip to content

Commit 327111c

Browse files
authored
fix: Check default browser first before supported Chromium Browsers (#2)
I was running into an issue where OpenBrowser would open Canary even though my default browser is regular Chrome. This is because I have Canary open too and due to the order of the supportedChromiumBrowsers, openBrowser would default to Canary. This change makes the default browser the first browser checked before proceeding as it did before.
1 parent c43f4f4 commit 327111c

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

index.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ function executeNodeScript(scriptPath, url) {
6464
return true;
6565
}
6666

67+
function getDefaultChromiumBrowser() {
68+
try {
69+
if (process.platform !== 'darwin') return null;
70+
71+
const defaultBrowser = execSync(
72+
'defaults read com.apple.LaunchServices/com.apple.launchservices.secure LSHandlers | grep -A 3 "https" | grep "LSHandlerRoleAll" | awk -F\\" \'{print $2}\''
73+
).toString().trim();
74+
75+
const bundleIdToName = {
76+
'com.google.chrome.canary': 'Google Chrome Canary',
77+
'com.google.chrome.dev': 'Google Chrome Dev',
78+
'com.google.chrome.beta': 'Google Chrome Beta',
79+
'com.google.chrome': 'Google Chrome',
80+
'com.microsoft.edgemac': 'Microsoft Edge',
81+
'com.brave.Browser': 'Brave Browser',
82+
'com.vivaldi.Vivaldi': 'Vivaldi',
83+
'org.chromium.Chromium': 'Chromium'
84+
};
85+
86+
return bundleIdToName[defaultBrowser] || null;
87+
} catch (err) {
88+
return null;
89+
}
90+
}
91+
6792
function startBrowserProcess(browser, url, args) {
6893
// If we're on OS X, the user hasn't specifically
6994
// requested a different browser, we can try opening
@@ -74,8 +99,11 @@ function startBrowserProcess(browser, url, args) {
7499
(typeof browser !== 'string' || browser === OSX_CHROME);
75100

76101
if (shouldTryOpenChromiumWithAppleScript) {
102+
const defaultBrowser = getDefaultChromiumBrowser();
103+
77104
// Will use the first open browser found from list
78105
const supportedChromiumBrowsers = [
106+
defaultBrowser,
79107
'Google Chrome Canary',
80108
'Google Chrome Dev',
81109
'Google Chrome Beta',
@@ -84,7 +112,7 @@ function startBrowserProcess(browser, url, args) {
84112
'Brave Browser',
85113
'Vivaldi',
86114
'Chromium',
87-
];
115+
].filter(Boolean); ;
88116

89117
for (let chromiumBrowser of supportedChromiumBrowsers) {
90118
try {
@@ -152,4 +180,4 @@ function openBrowser(url) {
152180
}
153181
}
154182

155-
module.exports = openBrowser;
183+
module.exports = openBrowser;

0 commit comments

Comments
 (0)