Skip to content

Commit

Permalink
feat: reuse opening tab in chromium browsers when start dev server (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh95 authored Oct 18, 2022
1 parent 9cc808e commit 1a2e7a8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 34 deletions.
55 changes: 32 additions & 23 deletions packages/vite/bin/openChrome.applescript
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ https://github.com/facebookincubator/create-react-app/blob/master/LICENSE
property targetTab: null
property targetTabIndex: -1
property targetWindow: null
property theProgram: "Google Chrome"

on run argv
set theURL to item 1 of argv

with timeout of 2 seconds
tell application "Chrome"
-- Allow requested program to be optional,
-- default to Google Chrome
if (count of argv) > 1 then
set theProgram to item 2 of argv
end if

using terms from application "Google Chrome"
tell application theProgram

if (count every window) = 0 then
make new window
Expand Down Expand Up @@ -51,36 +58,38 @@ on run argv
make new tab with properties {URL:theURL}
end tell
end tell
end timeout
end using terms from
end run

-- Function:
-- Lookup tab with given url
-- if found, store tab, index, and window in properties
-- (properties were declared on top of file)
on lookupTabWithUrl(lookupUrl)
tell application "Chrome"
-- Find a tab with the given url
set found to false
set theTabIndex to -1
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
if (theTab's URL as string) contains lookupUrl then
-- assign tab, tab index, and window to properties
set targetTab to theTab
set targetTabIndex to theTabIndex
set targetWindow to theWindow
set found to true
using terms from application "Google Chrome"
tell application theProgram
-- Find a tab with the given url
set found to false
set theTabIndex to -1
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
if (theTab's URL as string) contains lookupUrl then
-- assign tab, tab index, and window to properties
set targetTab to theTab
set targetTabIndex to theTabIndex
set targetWindow to theWindow
set found to true
exit repeat
end if
end repeat

if found then
exit repeat
end if
end repeat

if found then
exit repeat
end if
end repeat
end tell
end tell
end using terms from
return found
end lookupTabWithUrl
41 changes: 30 additions & 11 deletions packages/vite/src/node/server/openBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,36 @@ function startBrowserProcess(browser: string | undefined, url: string) {
process.platform === 'darwin' && (browser === '' || browser === OSX_CHROME)

if (shouldTryOpenChromeWithAppleScript) {
try {
// Try our best to reuse existing tab
// on OS X Google Chrome with AppleScript
execSync('ps cax | grep "Google Chrome"')
execSync('osascript openChrome.applescript "' + encodeURI(url) + '"', {
cwd: join(VITE_PACKAGE_DIR, 'bin'),
stdio: 'ignore'
})
return true
} catch (err) {
// Ignore errors
// Will use the first open browser found from list
const supportedChromiumBrowsers = [
'Google Chrome Canary',
'Google Chrome Dev',
'Google Chrome Beta',
'Google Chrome',
'Microsoft Edge',
'Brave Browser',
'Vivaldi',
'Chromium'
]

for (const chromiumBrowser of supportedChromiumBrowsers) {
try {
// Try our best to reuse existing tab
// on OS X Google Chrome with AppleScript
execSync(`ps cax | grep "${chromiumBrowser}"`)
execSync(
`osascript openChrome.applescript "${encodeURI(
url
)}" "${chromiumBrowser}"`,
{
cwd: join(VITE_PACKAGE_DIR, 'bin'),
stdio: 'ignore'
}
)
return true
} catch (err) {
// Ignore errors
}
}
}

Expand Down

0 comments on commit 1a2e7a8

Please sign in to comment.