File size: 6,767 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | import { readdir } from 'fs/promises'
import { homedir } from 'os'
import { join } from 'path'
import { isFsInaccessible } from '../errors.js'
export const CHROME_EXTENSION_URL = 'https://claude.ai/chrome'
// Production extension ID
const PROD_EXTENSION_ID = 'fcoeoabgfenejglbffodgkkbkcdhcgfn'
// Dev extension IDs (for internal use)
const DEV_EXTENSION_ID = 'dihbgbndebgnbjfmelmegjepbnkhlgni'
const ANT_EXTENSION_ID = 'dngcpimnedloihjnnfngkgjoidhnaolf'
function getExtensionIds(): string[] {
return process.env.USER_TYPE === 'ant'
? [PROD_EXTENSION_ID, DEV_EXTENSION_ID, ANT_EXTENSION_ID]
: [PROD_EXTENSION_ID]
}
// Must match ChromiumBrowser from common.ts
export type ChromiumBrowser =
| 'chrome'
| 'brave'
| 'arc'
| 'chromium'
| 'edge'
| 'vivaldi'
| 'opera'
export type BrowserPath = {
browser: ChromiumBrowser
path: string
}
type Logger = (message: string) => void
// Browser detection order - must match BROWSER_DETECTION_ORDER from common.ts
const BROWSER_DETECTION_ORDER: ChromiumBrowser[] = [
'chrome',
'brave',
'arc',
'edge',
'chromium',
'vivaldi',
'opera',
]
type BrowserDataConfig = {
macos: string[]
linux: string[]
windows: { path: string[]; useRoaming?: boolean }
}
// Must match CHROMIUM_BROWSERS dataPath from common.ts
const CHROMIUM_BROWSERS: Record<ChromiumBrowser, BrowserDataConfig> = {
chrome: {
macos: ['Library', 'Application Support', 'Google', 'Chrome'],
linux: ['.config', 'google-chrome'],
windows: { path: ['Google', 'Chrome', 'User Data'] },
},
brave: {
macos: ['Library', 'Application Support', 'BraveSoftware', 'Brave-Browser'],
linux: ['.config', 'BraveSoftware', 'Brave-Browser'],
windows: { path: ['BraveSoftware', 'Brave-Browser', 'User Data'] },
},
arc: {
macos: ['Library', 'Application Support', 'Arc', 'User Data'],
linux: [],
windows: { path: ['Arc', 'User Data'] },
},
chromium: {
macos: ['Library', 'Application Support', 'Chromium'],
linux: ['.config', 'chromium'],
windows: { path: ['Chromium', 'User Data'] },
},
edge: {
macos: ['Library', 'Application Support', 'Microsoft Edge'],
linux: ['.config', 'microsoft-edge'],
windows: { path: ['Microsoft', 'Edge', 'User Data'] },
},
vivaldi: {
macos: ['Library', 'Application Support', 'Vivaldi'],
linux: ['.config', 'vivaldi'],
windows: { path: ['Vivaldi', 'User Data'] },
},
opera: {
macos: ['Library', 'Application Support', 'com.operasoftware.Opera'],
linux: ['.config', 'opera'],
windows: { path: ['Opera Software', 'Opera Stable'], useRoaming: true },
},
}
/**
* Get all browser data paths to check for extension installation.
* Portable version that uses process.platform directly.
*/
export function getAllBrowserDataPathsPortable(): BrowserPath[] {
const home = homedir()
const paths: BrowserPath[] = []
for (const browserId of BROWSER_DETECTION_ORDER) {
const config = CHROMIUM_BROWSERS[browserId]
let dataPath: string[] | undefined
switch (process.platform) {
case 'darwin':
dataPath = config.macos
break
case 'linux':
dataPath = config.linux
break
case 'win32': {
if (config.windows.path.length > 0) {
const appDataBase = config.windows.useRoaming
? join(home, 'AppData', 'Roaming')
: join(home, 'AppData', 'Local')
paths.push({
browser: browserId,
path: join(appDataBase, ...config.windows.path),
})
}
continue
}
}
if (dataPath && dataPath.length > 0) {
paths.push({
browser: browserId,
path: join(home, ...dataPath),
})
}
}
return paths
}
/**
* Detects if the Claude in Chrome extension is installed by checking the Extensions
* directory across all supported Chromium-based browsers and their profiles.
*
* This is a portable version that can be used by both TUI and VS Code extension.
*
* @param browserPaths - Array of browser data paths to check (from getAllBrowserDataPaths)
* @param log - Optional logging callback for debug messages
* @returns Object with isInstalled boolean and the browser where the extension was found
*/
export async function detectExtensionInstallationPortable(
browserPaths: BrowserPath[],
log?: Logger,
): Promise<{
isInstalled: boolean
browser: ChromiumBrowser | null
}> {
if (browserPaths.length === 0) {
log?.(`[Claude in Chrome] No browser paths to check`)
return { isInstalled: false, browser: null }
}
const extensionIds = getExtensionIds()
// Check each browser for the extension
for (const { browser, path: browserBasePath } of browserPaths) {
let browserProfileEntries = []
try {
browserProfileEntries = await readdir(browserBasePath, {
withFileTypes: true,
})
} catch (e) {
// Browser not installed or path doesn't exist, continue to next browser
if (isFsInaccessible(e)) continue
throw e
}
const profileDirs = browserProfileEntries
.filter(entry => entry.isDirectory())
.filter(
entry => entry.name === 'Default' || entry.name.startsWith('Profile '),
)
.map(entry => entry.name)
if (profileDirs.length > 0) {
log?.(
`[Claude in Chrome] Found ${browser} profiles: ${profileDirs.join(', ')}`,
)
}
// Check each profile for any of the extension IDs
for (const profile of profileDirs) {
for (const extensionId of extensionIds) {
const extensionPath = join(
browserBasePath,
profile,
'Extensions',
extensionId,
)
try {
await readdir(extensionPath)
log?.(
`[Claude in Chrome] Extension ${extensionId} found in ${browser} ${profile}`,
)
return { isInstalled: true, browser }
} catch {
// Extension not found in this profile, continue checking
}
}
}
}
log?.(`[Claude in Chrome] Extension not found in any browser`)
return { isInstalled: false, browser: null }
}
/**
* Simple wrapper that returns just the boolean result
*/
export async function isChromeExtensionInstalledPortable(
browserPaths: BrowserPath[],
log?: Logger,
): Promise<boolean> {
const result = await detectExtensionInstallationPortable(browserPaths, log)
return result.isInstalled
}
/**
* Convenience function that gets browser paths automatically.
* Use this when you don't need to provide custom browser paths.
*/
export function isChromeExtensionInstalled(log?: Logger): Promise<boolean> {
const browserPaths = getAllBrowserDataPathsPortable()
return isChromeExtensionInstalledPortable(browserPaths, log)
}
|