Skip to content

Instantly share code, notes, and snippets.

@glocore
Last active October 30, 2022 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glocore/009c3ad7a3a85359d0dfb2917e8ea158 to your computer and use it in GitHub Desktop.
Save glocore/009c3ad7a3a85359d0dfb2917e8ea158 to your computer and use it in GitHub Desktop.
Node script to open a URL in an existing tab in a Chromium browser, similar to create-react-app's dev server.
// Reference: https://github.com/facebook/create-react-app/blob/main/packages/react-dev-utils/openBrowser.js
import { execSync } from "child_process";
import open from "open";
import defaultBrowserId from "default-browser-id";
import path from "path";
async function startBrowserProcess(url: string) {
const shouldTryOpenChromiumWithAppleScript = process.platform === "darwin";
if (shouldTryOpenChromiumWithAppleScript) {
const supportedChromiumBrowsers: Record<string, string> = {
"com.google.chrome": "Google Chrome",
"com.google.chrome.beta": "Google Chrome Beta",
"com.google.chrome.canary": "Google Chrome Canary",
"com.microsoft.edgemac": "Microsoft Edge",
"com.brave.browser": "Brave Browser",
"org.chromium.chromium": "Chromium",
};
const defaultBrowserBundleId = (await defaultBrowserId()) as string;
const chromiumBrowser =
supportedChromiumBrowsers[defaultBrowserBundleId.toLowerCase()];
const baseUrl = new URL(url).origin;
if (typeof chromiumBrowser === "string") {
try {
// Try our best to reuse existing tab
// on OSX Chromium-based browser with AppleScript
execSync('ps cax | grep "' + chromiumBrowser + '"');
const appleScriptPath = path.join(
__dirname,
"openChrome.applescript"
);
execSync(
`osascript ${appleScriptPath} "${baseUrl}" "${encodeURI(
url
)}" "${chromiumBrowser}"`,
{
cwd: __dirname,
stdio: "ignore",
}
);
return true;
} catch (err) {
// Ignore errors.
}
}
}
// Fallback to open
// (It will always open new tab)
try {
open(url).catch(() => {}); // Prevent `unhandledRejection` error.
return true;
} catch (err) {
return false;
}
}
export function openBrowser(url: string) {
return startBrowserProcess(url);
}
(*
Reference: https://github.com/facebook/create-react-app/blob/main/packages/react-dev-utils/openChrome.applescript
*)
property targetTab : null
property targetTabIndex : -1
property targetWindow : null
property theProgram : "Google Chrome"
on run argv
set baseURL to item 1 of argv
set fullURL to item 2 of argv
-- Allow requested program to be optional,
-- default to Google Chrome
if (count of argv) > 2 then
set theProgram to item 3 of argv
end if
using terms from application "Google Chrome"
tell application theProgram
if (count every window) = 0 then
make new window
end if
-- 1: Looking for tab running current baseURL
-- then, set tab URL to fullURL
-- then return
set found to my lookupTabWithUrl(baseURL)
log (found)
if found then
set targetWindow's active tab index to targetTabIndex
set URL of targetTab to fullURL
tell targetWindow to activate
set index of targetWindow to 1
return
end if
-- 2: Looking for Empty tab
-- In case baseURL 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 fullURL
tell targetWindow to activate
return
end if
-- 3: Create new tab
-- both baseURL and empty tab were not found
-- make a new tab with url
tell window 1
activate
make new tab with properties {URL:fullURL}
end tell
end tell
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)
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
end tell
end using terms from
return found
end lookupTabWithUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment