Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for browser profiles #113

Merged
merged 3 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Finicky/Finicky/AppDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public struct BrowserOpts: CustomStringConvertible {
public var openInBackground: Bool
public var bundleId: String?
public var appPath: String?
public var profile: String?

public var description: String {
if let bundleId = self.bundleId {
Expand All @@ -32,9 +33,10 @@ public struct BrowserOpts: CustomStringConvertible {
}
}

public init(name: String, appType: AppDescriptorType, openInBackground: Bool?) throws {
public init(name: String, appType: AppDescriptorType, openInBackground: Bool?, profile: String?) throws {
self.name = name
self.openInBackground = openInBackground ?? !NSApplication.shared.isActive
self.profile = profile

if appType == AppDescriptorType.bundleId {
bundleId = name
Expand Down
43 changes: 36 additions & 7 deletions Finicky/Finicky/Browsers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
import Foundation

enum Browser: String {
case Chrome = "com.google.Chrome"
case ChromeCanary = "com.google.Chrome.canary"
case Brave = "com.brave.Browser"
case BraveDev = "com.brave.Browser.dev"
case Safari = "com.apple.Safari"
case Chrome = "com.google.chrome"
case ChromeCanary = "com.google.chrome.canary"
case Brave = "com.brave.browser"
case BraveDev = "com.brave.browser.dev"
case Safari = "com.apple.safari"
case Firefox = "org.mozilla.firefox"
case FirefoxDeveloperEdition = "org.mozilla.firefoxdeveloperedition"
case Opera = "com.operasoftware.Opera"
case Opera = "com.operasoftware.opera"
}

public func getBrowserCommand(_ browserOpts: BrowserOpts, url: URL) -> [String] {
var command = ["open", url.absoluteString]
var command = ["open"]

// appPath takes priority over bundleId as it is always unique.
if let appPath = browserOpts.appPath {
Expand All @@ -26,5 +26,34 @@ public func getBrowserCommand(_ browserOpts: BrowserOpts, url: URL) -> [String]
command.append("-g")
}

if let profile = browserOpts.profile, let bundleId: String = browserOpts.bundleId {
if let profileOption: [String] = getProfileOption(bundleId: bundleId, profile: profile) {
command.append("-n")
command.append("--args")
command.append(contentsOf: profileOption)
}
}

command.append(url.absoluteString)

return command
}

private func getProfileOption(bundleId: String, profile: String) -> [String]? {
var profileOption: [String]? {
switch bundleId.lowercased() {
case Browser.Brave.rawValue: return ["--profile-directory=\(profile)"]
case Browser.BraveDev.rawValue: return ["--profile-directory=\(profile)"]
case Browser.Chrome.rawValue: return ["--profile-directory=\(profile)"]

// Disabling Firefox support due to unreliable performance
// Link: https://github.com/johnste/finicky/pull/113#issuecomment-672180597
//
// case Browser.Firefox.rawValue: return ["-P", profile]
// case Browser.FirefoxDeveloperEdition.rawValue: return ["-P", profile]

default: return nil
}
}
return profileOption
}
3 changes: 2 additions & 1 deletion Finicky/Finicky/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,15 @@ open class FinickyConfig {
let appType = AppDescriptorType(rawValue: dict["appType"] as! String)
let openInBackground: Bool? = dict["openInBackground"] as? Bool
let browserName = dict["name"] as! String
let browserProfile: String? = dict["profile"] as? String

if browserName == "" {
return nil
}

do {
// Default to opening the application in the bg if Finicky is not activated.
let browser = try BrowserOpts(name: browserName, appType: appType!, openInBackground: openInBackground)
let browser = try BrowserOpts(name: browserName, appType: appType!, openInBackground: openInBackground, profile:browserProfile)
return browser
} catch _ as BrowserError {
showNotification(title: "Couldn't find browser \"\(browserName)\"")
Expand Down
1 change: 1 addition & 0 deletions config-api/src/processUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const appDescriptorSchema = {
validate.value("none"),
]).isRequired,
openInBackground: validate.boolean,
profile: validate.string,
};

export function processUrl(
Expand Down
4 changes: 3 additions & 1 deletion config-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface BrowserObject {
name: string;
appType?: "appName" | "bundleId" | "appPath" | "none";
openInBackground?: boolean;
profile?: string;
}

/**
Expand Down Expand Up @@ -191,7 +192,8 @@ const browserSchema = validate.oneOf([
validate.shape({
name: validate.string.isRequired,
appType: validate.oneOf(["appName", "appPath", "bundleId"]),
openInBackground: validate.boolean
openInBackground: validate.boolean,
profile: validate.string,
}),
validate.function("options"),
validate.value(null)
Expand Down