From 5c14b30360680972cf7b97ec1beec927637baad5 Mon Sep 17 00:00:00 2001 From: Matthew Curtis Date: Thu, 23 Jan 2020 13:57:12 -0600 Subject: [PATCH 1/3] Expands scope of openBrowser tab control Adjust openChrome.applescript to allow manipulation of other Chromium-based browsers (defaulting to Chrome). Requires list of compatible browsers to try in openBrowser.js --- packages/react-dev-utils/openBrowser.js | 44 +++++-- .../react-dev-utils/openChrome.applescript | 115 ++++++++++-------- 2 files changed, 95 insertions(+), 64 deletions(-) diff --git a/packages/react-dev-utils/openBrowser.js b/packages/react-dev-utils/openBrowser.js index 22d3c44d852..fd8debc2746 100644 --- a/packages/react-dev-utils/openBrowser.js +++ b/packages/react-dev-utils/openBrowser.js @@ -73,18 +73,38 @@ function startBrowserProcess(browser, url, args) { process.platform === 'darwin' && (typeof browser !== 'string' || 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: __dirname, - stdio: 'ignore', - }); - return true; - } catch (err) { - // Ignore errors. + if (shouldTryOpenChromiumWithAppleScript) { + // Will use the first open browser found from list + const supportedChromiumBrowsers = [ + 'Google Chrome Canary', + 'Google Chrome', + 'Microsoft Edge', + 'Brave Browser', + 'Vivaldi', + 'Chromium', + 'Safari', + ]; + + for (let chromiumBrowser of supportedChromiumBrowsers) { + try { + // Try our best to reuse existing tab + // on OSX Chromium-based browser with AppleScript + execSync('ps cax | grep "' + chromiumBrowser + '"'); + execSync( + 'osascript openChrome.applescript "' + + encodeURI(url) + + '" "' + + chromiumBrowser + + '"', + { + cwd: __dirname, + stdio: 'ignore', + } + ); + return true; + } catch (err) { + // Ignore errors. + } } } diff --git a/packages/react-dev-utils/openChrome.applescript b/packages/react-dev-utils/openChrome.applescript index 3b1b5a29d3f..c1efba15985 100644 --- a/packages/react-dev-utils/openChrome.applescript +++ b/packages/react-dev-utils/openChrome.applescript @@ -8,47 +8,56 @@ LICENSE file in the root directory of this source tree. property targetTab: null property targetTabIndex: -1 property targetWindow: null +property theProgram: "Google Chrome" on run argv set theURL to item 1 of argv - 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 - if (count every window) = 0 then - make new window - end if + using terms from application "Google Chrome" + tell application theProgram - -- 1: Looking for tab running debugger - -- then, Reload debugging tab if found - -- then return - set found to my lookupTabWithUrl(theURL) - if found then - set targetWindow's active tab index to targetTabIndex - tell targetTab to reload - tell targetWindow to activate - set index of targetWindow to 1 - return - end if + if (count every window) = 0 then + make new window + end if + + -- 1: Looking for tab running debugger + -- then, Reload debugging tab if found + -- then return + set found to my lookupTabWithUrl(theURL) + if found then + set targetWindow's active tab index to targetTabIndex + tell targetTab to reload + tell targetWindow to activate + set index of targetWindow to 1 + return + end if - -- 2: Looking for Empty tab - -- In case debugging tab was not found - -- We try to find an empty tab instead - set found to my lookupTabWithUrl("chrome://newtab/") - if found then - set targetWindow's active tab index to targetTabIndex - set URL of targetTab to theURL - tell targetWindow to activate - return - end if + -- 2: Looking for Empty tab + -- In case debugging tab was not found + -- We try to find an empty tab instead + set found to my lookupTabWithUrl("chrome://newtab/") + if found then + set targetWindow's active tab index to targetTabIndex + set URL of targetTab to theURL + tell targetWindow to activate + return + end if - -- 3: Create new tab - -- both debugging and empty tab were not found - -- make a new tab with url - tell window 1 - activate - make new tab with properties {URL:theURL} + -- 3: Create new tab + -- both debugging and empty tab were not found + -- make a new tab with url + tell window 1 + activate + make new tab with properties {URL:theURL} + end tell end tell - end tell + end using terms from end run -- Function: @@ -56,28 +65,30 @@ end run -- 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 From 4b977f2350f9bbb6983a6e0ade20cc69c15c802b Mon Sep 17 00:00:00 2001 From: Matthew Curtis Date: Thu, 23 Jan 2020 14:05:27 -0600 Subject: [PATCH 2/3] Fix typo --- packages/react-dev-utils/openBrowser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-dev-utils/openBrowser.js b/packages/react-dev-utils/openBrowser.js index fd8debc2746..babaed1b303 100644 --- a/packages/react-dev-utils/openBrowser.js +++ b/packages/react-dev-utils/openBrowser.js @@ -69,7 +69,7 @@ function startBrowserProcess(browser, url, args) { // requested a different browser, we can try opening // Chrome with AppleScript. This lets us reuse an // existing tab when possible instead of creating a new one. - const shouldTryOpenChromeWithAppleScript = + const shouldTryOpenChromiumWithAppleScript = process.platform === 'darwin' && (typeof browser !== 'string' || browser === OSX_CHROME); From 64d40cc2d82e86d58d922fbf4c909835281ed6ab Mon Sep 17 00:00:00 2001 From: Matthew Curtis Date: Thu, 23 Jan 2020 14:16:50 -0600 Subject: [PATCH 3/3] Remove Safari --- packages/react-dev-utils/openBrowser.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/react-dev-utils/openBrowser.js b/packages/react-dev-utils/openBrowser.js index babaed1b303..73db076f903 100644 --- a/packages/react-dev-utils/openBrowser.js +++ b/packages/react-dev-utils/openBrowser.js @@ -82,7 +82,6 @@ function startBrowserProcess(browser, url, args) { 'Brave Browser', 'Vivaldi', 'Chromium', - 'Safari', ]; for (let chromiumBrowser of supportedChromiumBrowsers) {