forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserAutomationTarget.swift
More file actions
308 lines (286 loc) · 11.8 KB
/
Copy pathBrowserAutomationTarget.swift
File metadata and controls
308 lines (286 loc) · 11.8 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import AppKit
import Foundation
struct BrowserAutomationTarget: Equatable, Hashable, Identifiable, Sendable {
static let extensionId = "mmlmfjhmonkocbjadbfplnigmagldckm"
static let chromeWebStoreURL =
"https://chromewebstore.google.com/detail/playwright-mcp-bridge/\(extensionId)"
let name: String
let bundleIdentifier: String
let appPath: String
let profileDirectoryRelativePath: String
let installURL: URL?
let supportsChromeWebStore: Bool
var id: String { bundleIdentifier }
var appURL: URL { URL(fileURLWithPath: appPath) }
func profileRoot(homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser) -> URL {
homeDirectory.appendingPathComponent(profileDirectoryRelativePath)
}
func extensionStatusURL() -> URL? {
URL(string: "chrome-extension://\(Self.extensionId)/status.html")
}
func extensionInstallURL() -> URL? {
supportsChromeWebStore ? URL(string: Self.chromeWebStoreURL) : installURL
}
func extensionSetupURL() -> URL? {
extensionInstallURL()
}
}
enum BrowserAutomationTargetStore {
private static let selectedBundleIdentifierKey = "playwrightBrowserBundleIdentifier"
private static let userSelectedBundleIdentifierKey = "playwrightBrowserBundleIdentifierUserSelected"
private static let extensionTokenKey = "playwrightExtensionToken"
static var selectedBundleIdentifier: String? {
get {
guard UserDefaults.standard.bool(forKey: userSelectedBundleIdentifierKey) else { return nil }
let value = UserDefaults.standard.string(forKey: selectedBundleIdentifierKey) ?? ""
return value.isEmpty ? nil : value
}
set {
UserDefaults.standard.set(newValue ?? "", forKey: selectedBundleIdentifierKey)
UserDefaults.standard.set(newValue != nil, forKey: userSelectedBundleIdentifierKey)
}
}
static func select(_ target: BrowserAutomationTarget) {
if selectedBundleIdentifier != target.bundleIdentifier {
UserDefaults.standard.removeObject(forKey: extensionTokenKey)
}
selectedBundleIdentifier = target.bundleIdentifier
}
}
enum BrowserAutomationTargetResolver {
static let knownTargets: [BrowserAutomationTarget] = [
BrowserAutomationTarget(
name: "ChatGPT Atlas",
bundleIdentifier: "com.openai.atlas",
appPath: "/Applications/ChatGPT Atlas.app",
profileDirectoryRelativePath: "Library/Application Support/com.openai.atlas/browser-data/host",
installURL: URL(string: "https://chatgpt.com/atlas"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Google Chrome",
bundleIdentifier: "com.google.Chrome",
appPath: "/Applications/Google Chrome.app",
profileDirectoryRelativePath: "Library/Application Support/Google/Chrome",
installURL: URL(string: "https://www.google.com/chrome/"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Google Chrome Beta",
bundleIdentifier: "com.google.Chrome.beta",
appPath: "/Applications/Google Chrome Beta.app",
profileDirectoryRelativePath: "Library/Application Support/Google/Chrome Beta",
installURL: URL(string: "https://www.google.com/chrome/beta/"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Google Chrome Canary",
bundleIdentifier: "com.google.Chrome.canary",
appPath: "/Applications/Google Chrome Canary.app",
profileDirectoryRelativePath: "Library/Application Support/Google/Chrome Canary",
installURL: URL(string: "https://www.google.com/chrome/canary/"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Brave Browser",
bundleIdentifier: "com.brave.Browser",
appPath: "/Applications/Brave Browser.app",
profileDirectoryRelativePath: "Library/Application Support/BraveSoftware/Brave-Browser",
installURL: URL(string: "https://brave.com/download/"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Brave Browser Beta",
bundleIdentifier: "com.brave.Browser.beta",
appPath: "/Applications/Brave Browser Beta.app",
profileDirectoryRelativePath: "Library/Application Support/BraveSoftware/Brave-Browser-Beta",
installURL: URL(string: "https://brave.com/download-beta/"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Brave Browser Nightly",
bundleIdentifier: "com.brave.Browser.nightly",
appPath: "/Applications/Brave Browser Nightly.app",
profileDirectoryRelativePath: "Library/Application Support/BraveSoftware/Brave-Browser-Nightly",
installURL: URL(string: "https://brave.com/download-nightly/"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Microsoft Edge",
bundleIdentifier: "com.microsoft.edgemac",
appPath: "/Applications/Microsoft Edge.app",
profileDirectoryRelativePath: "Library/Application Support/Microsoft Edge",
installURL: URL(string: "https://www.microsoft.com/edge/download"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Microsoft Edge Beta",
bundleIdentifier: "com.microsoft.edgemac.Beta",
appPath: "/Applications/Microsoft Edge Beta.app",
profileDirectoryRelativePath: "Library/Application Support/Microsoft Edge Beta",
installURL: URL(string: "https://www.microsoft.com/edge/download/insider"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Microsoft Edge Dev",
bundleIdentifier: "com.microsoft.edgemac.Dev",
appPath: "/Applications/Microsoft Edge Dev.app",
profileDirectoryRelativePath: "Library/Application Support/Microsoft Edge Dev",
installURL: URL(string: "https://www.microsoft.com/edge/download/insider"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Microsoft Edge Canary",
bundleIdentifier: "com.microsoft.edgemac.Canary",
appPath: "/Applications/Microsoft Edge Canary.app",
profileDirectoryRelativePath: "Library/Application Support/Microsoft Edge Canary",
installURL: URL(string: "https://www.microsoft.com/edge/download/insider"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Arc",
bundleIdentifier: "company.thebrowser.Browser",
appPath: "/Applications/Arc.app",
profileDirectoryRelativePath: "Library/Application Support/Arc/User Data",
installURL: URL(string: "https://arc.net/"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Opera",
bundleIdentifier: "com.operasoftware.Opera",
appPath: "/Applications/Opera.app",
profileDirectoryRelativePath: "Library/Application Support/com.operasoftware.Opera",
installURL: URL(string: "https://www.opera.com/download"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Opera GX",
bundleIdentifier: "com.operasoftware.OperaGX",
appPath: "/Applications/Opera GX.app",
profileDirectoryRelativePath: "Library/Application Support/com.operasoftware.OperaGX",
installURL: URL(string: "https://www.opera.com/gx"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Chromium",
bundleIdentifier: "org.chromium.Chromium",
appPath: "/Applications/Chromium.app",
profileDirectoryRelativePath: "Library/Application Support/Chromium",
installURL: URL(string: "https://www.chromium.org/getting-involved/download-chromium/"),
supportsChromeWebStore: true
),
BrowserAutomationTarget(
name: "Vivaldi",
bundleIdentifier: "com.vivaldi.Vivaldi",
appPath: "/Applications/Vivaldi.app",
profileDirectoryRelativePath: "Library/Application Support/Vivaldi",
installURL: URL(string: "https://vivaldi.com/download/"),
supportsChromeWebStore: true
),
]
static func installedTargets(
fileManager: FileManager = .default,
workspace: NSWorkspace = .shared
) -> [BrowserAutomationTarget] {
knownTargets.filter { target in
fileManager.fileExists(atPath: target.appPath)
|| workspace.urlForApplication(withBundleIdentifier: target.bundleIdentifier) != nil
}
}
static func target(
for bundleIdentifier: String?,
fileManager: FileManager = .default,
workspace: NSWorkspace = .shared
) -> BrowserAutomationTarget? {
guard let bundleIdentifier else { return nil }
return installedTargets(fileManager: fileManager, workspace: workspace)
.first { $0.bundleIdentifier == bundleIdentifier }
}
static func defaultTarget(
for url: URL = URL(string: "https://chatgpt.com/")!,
fileManager: FileManager = .default,
workspace: NSWorkspace = .shared
) -> BrowserAutomationTarget? {
guard let appURL = workspace.urlForApplication(toOpen: url) else { return nil }
let installed = installedTargets(fileManager: fileManager, workspace: workspace)
return installed.first { $0.appURL.standardizedFileURL == appURL.standardizedFileURL }
?? installed.first {
workspace.urlForApplication(withBundleIdentifier: $0.bundleIdentifier)?
.standardizedFileURL == appURL.standardizedFileURL
}
}
static func preferredTarget(
for url: URL = URL(string: "https://chatgpt.com/")!,
fileManager: FileManager = .default,
workspace: NSWorkspace = .shared
) -> BrowserAutomationTarget? {
if let selected = target(
for: BrowserAutomationTargetStore.selectedBundleIdentifier,
fileManager: fileManager,
workspace: workspace)
{
return selected
}
if let target = defaultTarget(for: url, fileManager: fileManager, workspace: workspace) {
return target
}
let installed = installedTargets(fileManager: fileManager, workspace: workspace)
if let target = installed.first {
return target
}
return knownTargets.first
}
static func isInstalled(
_ target: BrowserAutomationTarget,
fileManager: FileManager = .default,
workspace: NSWorkspace = .shared
) -> Bool {
fileManager.fileExists(atPath: target.appPath)
|| workspace.urlForApplication(withBundleIdentifier: target.bundleIdentifier) != nil
}
static func isExtensionInstalled(
in target: BrowserAutomationTarget,
homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser,
fileManager: FileManager = .default
) -> Bool {
let root = target.profileRoot(homeDirectory: homeDirectory)
guard
let profiles = try? fileManager.contentsOfDirectory(
at: root, includingPropertiesForKeys: nil)
else { return false }
for profile in profiles {
let extDir = profile.appendingPathComponent("Extensions/\(BrowserAutomationTarget.extensionId)")
if fileManager.fileExists(atPath: extDir.path) {
return true
}
}
return false
}
static func open(_ url: URL, in target: BrowserAutomationTarget, workspace: NSWorkspace = .shared) {
let configuration = NSWorkspace.OpenConfiguration()
configuration.activates = true
if let appURL = workspace.urlForApplication(withBundleIdentifier: target.bundleIdentifier) {
workspace.open([url], withApplicationAt: appURL, configuration: configuration) { _, error in
if let error {
log("BrowserAutomationTargetResolver: Failed opening \(url) in \(target.name): \(error)")
_ = MainActor.assumeIsolated {
NSWorkspace.shared.open(url)
}
}
}
return
}
if FileManager.default.fileExists(atPath: target.appPath) {
workspace.open([url], withApplicationAt: target.appURL, configuration: configuration) { _, error in
if let error {
log("BrowserAutomationTargetResolver: Failed opening \(url) at \(target.appPath): \(error)")
_ = MainActor.assumeIsolated {
NSWorkspace.shared.open(url)
}
}
}
return
}
workspace.open(url)
}
}