From 5141f5586b9b71d39030248569ba5d1735899d2d Mon Sep 17 00:00:00 2001 From: 0xrushi <> Date: Tue, 7 Jan 2025 22:21:19 -0500 Subject: [PATCH 1/2] list profiles --- chrome-cli/App.h | 2 +- chrome-cli/App.m | 80 +++++++++++++++++++++++++++++++++++++++++++++++ chrome-cli/main.m | 2 ++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/chrome-cli/App.h b/chrome-cli/App.h index 4a41c78..26b756e 100644 --- a/chrome-cli/App.h +++ b/chrome-cli/App.h @@ -57,5 +57,5 @@ typedef enum { - (void)printSourceFromTab:(Arguments *)args; - (void)printChromeVersion:(Arguments *)args; - (void)printVersion:(Arguments *)args; - +- (void)listProfiles:(Arguments *)args; @end diff --git a/chrome-cli/App.m b/chrome-cli/App.m index 0ee78c4..c7c4579 100644 --- a/chrome-cli/App.m +++ b/chrome-cli/App.m @@ -28,6 +28,74 @@ - (id)initWithBundleIdentifier:(NSString *)bundleIdentifier outputFormat:(Output return self; } +- (NSArray *)getProfiles { + NSString *homeDir = NSHomeDirectory(); + NSString *profilesPath = [homeDir stringByAppendingPathComponent:@"Library/Application Support/Google/Chrome"]; + + NSError *error = nil; + NSFileManager *fileManager = [NSFileManager defaultManager]; + + NSArray *contents = [fileManager contentsOfDirectoryAtPath:profilesPath error:&error]; + if (error) { + return @[]; + } + + NSMutableArray *profiles = [NSMutableArray array]; + + for (NSString *item in contents) { + if ([item isEqualToString:@"Default"] || [item hasPrefix:@"Profile"]) { + NSString *prefsPath = [profilesPath stringByAppendingPathComponent: + [item stringByAppendingPathComponent:@"Preferences"]]; + + if ([fileManager fileExistsAtPath:prefsPath]) { + [profiles addObject:item]; + } + } + } + + return profiles; +} +- (NSString *)getProfileForWindowTitle:(NSString *)windowTitle fromProfiles:(NSArray *)profiles { + // Extract the email/profile name from window title (format: "Page Title - email@gmail.com - Google Chrome") + NSArray *parts = [windowTitle componentsSeparatedByString:@" - "]; + if (parts.count >= 2) { + NSString *possibleEmail = parts[parts.count - 2]; + + // For each profile directory + for (NSString *profile in profiles) { + NSString *prefsPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Google/Chrome"] + stringByAppendingPathComponent:[profile stringByAppendingPathComponent:@"Preferences"]]; + + if ([[NSFileManager defaultManager] fileExistsAtPath:prefsPath]) { + NSData *prefsData = [NSData dataWithContentsOfFile:prefsPath]; + if (prefsData) { + NSError *error = nil; + NSDictionary *prefs = [NSJSONSerialization JSONObjectWithData:prefsData + options:0 + error:&error]; + if (!error) { + // Check account_info array for email matching + NSArray *accountInfo = prefs[@"account_info"]; + if ([accountInfo isKindOfClass:[NSArray class]] && accountInfo.count > 0) { + NSDictionary *firstAccount = accountInfo[0]; + NSString *email = firstAccount[@"email"]; + if ([email isEqualToString:possibleEmail]) { + return profile; + } + } + } + } + } + } + } + + // If no match found and Default exists, use Default + if ([profiles containsObject:@"Default"]) { + return @"Default"; + } + + return @"Unknown"; +} - (chromeApplication *)chrome { chromeApplication *chrome = [SBApplication applicationWithBundleIdentifier:self->bundleIdentifier]; @@ -762,4 +830,16 @@ - (NSJSONWritingOptions)jsonWritingOptions { return NSJSONWritingPrettyPrinted; } +- (void)listProfiles:(Arguments *)args { + NSArray *profiles = [self getProfiles]; + + if (self->outputFormat == kOutputFormatJSON) { + [self printJSON:@{@"profiles": profiles}]; + } else { + for (NSString *profile in profiles) { + printf("Profile Directory: %s\n", profile.UTF8String); + } + } +} + @end diff --git a/chrome-cli/main.m b/chrome-cli/main.m index 02e1ce5..c70ccad 100644 --- a/chrome-cli/main.m +++ b/chrome-cli/main.m @@ -84,6 +84,8 @@ int main(int argc, const char * argv[]) [argonaut add:@"execute " target:app action:@selector(executeJavascriptInActiveTab:) description:@"Execute javascript in active tab"]; [argonaut add:@"execute -t " target:app action:@selector(executeJavascriptInTab:) description:@"Execute javascript in specific tab"]; + + [argonaut add:@"list profiles" target:app action:@selector(listProfiles:) description:@"List Chrome profiles"]; [argonaut add:@"chrome version" target:app action:@selector(printChromeVersion:) description:@"Print Chrome version"]; [argonaut add:@"version" target:app action:@selector(printVersion:) description:@"Print application version"]; From 205e023c9c99c043b2a112186acd6f51945cf54f Mon Sep 17 00:00:00 2001 From: 0xrushi <> Date: Tue, 7 Jan 2025 22:23:09 -0500 Subject: [PATCH 2/2] remove unused --- chrome-cli/App.m | 41 ----------------------------------------- 1 file changed, 41 deletions(-) diff --git a/chrome-cli/App.m b/chrome-cli/App.m index c7c4579..ec25df0 100644 --- a/chrome-cli/App.m +++ b/chrome-cli/App.m @@ -55,47 +55,6 @@ - (NSArray *)getProfiles { return profiles; } -- (NSString *)getProfileForWindowTitle:(NSString *)windowTitle fromProfiles:(NSArray *)profiles { - // Extract the email/profile name from window title (format: "Page Title - email@gmail.com - Google Chrome") - NSArray *parts = [windowTitle componentsSeparatedByString:@" - "]; - if (parts.count >= 2) { - NSString *possibleEmail = parts[parts.count - 2]; - - // For each profile directory - for (NSString *profile in profiles) { - NSString *prefsPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Google/Chrome"] - stringByAppendingPathComponent:[profile stringByAppendingPathComponent:@"Preferences"]]; - - if ([[NSFileManager defaultManager] fileExistsAtPath:prefsPath]) { - NSData *prefsData = [NSData dataWithContentsOfFile:prefsPath]; - if (prefsData) { - NSError *error = nil; - NSDictionary *prefs = [NSJSONSerialization JSONObjectWithData:prefsData - options:0 - error:&error]; - if (!error) { - // Check account_info array for email matching - NSArray *accountInfo = prefs[@"account_info"]; - if ([accountInfo isKindOfClass:[NSArray class]] && accountInfo.count > 0) { - NSDictionary *firstAccount = accountInfo[0]; - NSString *email = firstAccount[@"email"]; - if ([email isEqualToString:possibleEmail]) { - return profile; - } - } - } - } - } - } - } - - // If no match found and Default exists, use Default - if ([profiles containsObject:@"Default"]) { - return @"Default"; - } - - return @"Unknown"; -} - (chromeApplication *)chrome { chromeApplication *chrome = [SBApplication applicationWithBundleIdentifier:self->bundleIdentifier];