browser tools are even better now
This commit is contained in:
@@ -2,17 +2,19 @@
|
||||
|
||||
import { spawn, execSync } from "node:child_process";
|
||||
import puppeteer from "puppeteer-core";
|
||||
import { homedir, platform } from "node:os";
|
||||
import { existsSync } from "node:fs";
|
||||
|
||||
const useProfile = process.argv[2] === "--profile";
|
||||
|
||||
if (process.argv[2] && process.argv[2] !== "--profile") {
|
||||
console.log("Usage: browser-start.js [--profile]");
|
||||
console.log("\nOptions:");
|
||||
console.log(" --profile Copy your default Chrome profile (cookies, logins)");
|
||||
console.log(" --profile Copy your default browser profile (cookies, logins)");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const SCRAPING_DIR = `${process.env.HOME}/.cache/browser-tools`;
|
||||
const SCRAPING_DIR = `${homedir()}/.cache/browser-tools`;
|
||||
|
||||
// Check if already running on :9222
|
||||
try {
|
||||
@@ -21,10 +23,66 @@ try {
|
||||
defaultViewport: null,
|
||||
});
|
||||
await browser.disconnect();
|
||||
console.log("✓ Chrome already running on :9222");
|
||||
console.log("✓ Browser already running on :9222");
|
||||
process.exit(0);
|
||||
} catch {}
|
||||
|
||||
// Detect platform and find browser binary
|
||||
const isMac = platform() === "darwin";
|
||||
const isLinux = platform() === "linux";
|
||||
|
||||
function findBrowser() {
|
||||
// Try to find browser using `which` command
|
||||
const candidates = isMac
|
||||
? ["Helium", "Google Chrome", "Chromium", "Microsoft Edge"]
|
||||
: ["helium", "chromium", "google-chrome", "google-chrome-stable", "chrome", "brave", "edge"];
|
||||
|
||||
for (const name of candidates) {
|
||||
try {
|
||||
if (isMac) {
|
||||
// On macOS, check /Applications
|
||||
const appPath = `/Applications/${name}.app/Contents/MacOS/${name}`;
|
||||
if (existsSync(appPath)) {
|
||||
return appPath;
|
||||
}
|
||||
} else {
|
||||
// On Linux, use `which`
|
||||
const result = execSync(`which ${name} 2>/dev/null`, { encoding: "utf-8" }).trim();
|
||||
if (result) return result;
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function findProfilePath() {
|
||||
const home = homedir();
|
||||
if (isMac) {
|
||||
return [
|
||||
`${home}/Library/Application Support/Helium`,
|
||||
`${home}/Library/Application Support/Google/Chrome`,
|
||||
`${home}/Library/Application Support/Google/Chrome Canary`,
|
||||
`${home}/Library/Application Support/Chromium`,
|
||||
`${home}/Library/Application Support/Microsoft Edge`,
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
`${home}/.config/helium`,
|
||||
`${home}/.config/chromium`,
|
||||
`${home}/.config/google-chrome`,
|
||||
`${home}/.config/google-chrome-stable`,
|
||||
`${home}/.config/BraveSoftware/Brave-Browser`,
|
||||
`${home}/.config/microsoft-edge`,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
const browserPath = findBrowser();
|
||||
if (!browserPath) {
|
||||
console.error("✗ Could not find a Chromium-based browser (tried: helium, chromium, google-chrome, edge, brave)");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Setup profile directory
|
||||
execSync(`mkdir -p "${SCRAPING_DIR}"`, { stdio: "ignore" });
|
||||
|
||||
@@ -35,24 +93,42 @@ try {
|
||||
|
||||
if (useProfile) {
|
||||
console.log("Syncing profile...");
|
||||
execSync(
|
||||
`rsync -a --delete \
|
||||
--exclude='SingletonLock' \
|
||||
--exclude='SingletonSocket' \
|
||||
--exclude='SingletonCookie' \
|
||||
--exclude='*/Sessions/*' \
|
||||
--exclude='*/Current Session' \
|
||||
--exclude='*/Current Tabs' \
|
||||
--exclude='*/Last Session' \
|
||||
--exclude='*/Last Tabs' \
|
||||
"${process.env.HOME}/Library/Application Support/Google/Chrome/" "${SCRAPING_DIR}/"`,
|
||||
{ stdio: "pipe" },
|
||||
);
|
||||
const profilePaths = findProfilePath();
|
||||
let synced = false;
|
||||
|
||||
for (const sourcePath of profilePaths) {
|
||||
if (existsSync(sourcePath)) {
|
||||
try {
|
||||
execSync(
|
||||
`rsync -a --delete \
|
||||
--exclude='SingletonLock' \
|
||||
--exclude='SingletonSocket' \
|
||||
--exclude='SingletonCookie' \
|
||||
--exclude='*/Sessions/*' \
|
||||
--exclude='*/Current Session' \
|
||||
--exclude='*/Current Tabs' \
|
||||
--exclude='*/Last Session' \
|
||||
--exclude='*/Last Tabs' \
|
||||
"${sourcePath}/" "${SCRAPING_DIR}/"`,
|
||||
{ stdio: "pipe" },
|
||||
);
|
||||
console.log(`✓ Synced profile from ${sourcePath}`);
|
||||
synced = true;
|
||||
break;
|
||||
} catch (e) {
|
||||
// Try next path
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!synced) {
|
||||
console.log("⚠ No existing browser profile found, starting fresh");
|
||||
}
|
||||
}
|
||||
|
||||
// Start Chrome with flags to force new instance
|
||||
// Start browser with remote debugging
|
||||
spawn(
|
||||
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
||||
browserPath,
|
||||
[
|
||||
"--remote-debugging-port=9222",
|
||||
`--user-data-dir=${SCRAPING_DIR}`,
|
||||
@@ -62,7 +138,7 @@ spawn(
|
||||
{ detached: true, stdio: "ignore" },
|
||||
).unref();
|
||||
|
||||
// Wait for Chrome to be ready
|
||||
// Wait for browser to be ready
|
||||
let connected = false;
|
||||
for (let i = 0; i < 30; i++) {
|
||||
try {
|
||||
@@ -79,8 +155,8 @@ for (let i = 0; i < 30; i++) {
|
||||
}
|
||||
|
||||
if (!connected) {
|
||||
console.error("✗ Failed to connect to Chrome");
|
||||
console.error("✗ Failed to connect to browser");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`✓ Chrome started on :9222${useProfile ? " with your profile" : ""}`);
|
||||
console.log(`✓ Browser started on :9222${useProfile ? " with your profile" : ""}`);
|
||||
|
||||
Reference in New Issue
Block a user