-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathViewController.swift
More file actions
413 lines (385 loc) · 13.6 KB
/
Copy pathViewController.swift
File metadata and controls
413 lines (385 loc) · 13.6 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import WebKit
import SwiftUI
#if os(iOS)
typealias PlatformViewController = UIViewController
#elseif os(macOS)
import SafariServices.SFSafariApplication
typealias PlatformViewController = NSViewController
#endif
private let logger = USLogger(#fileID)
class ViewController: PlatformViewController, WKNavigationDelegate, WKScriptMessageHandlerWithReply {
@IBOutlet var webView: WKWebView!
enum ExtensionState: String {
case unknow = "unknow"
case on = "enabled"
case off = "disabled"
case error = "error"
}
var extensionState: ExtensionState = .unknow
#if os(iOS)
var documentPickerHandler: ((URL) -> Void)?
#elseif os(macOS)
var timerExtensionStateCheck: Timer?
#endif
// https://developer.apple.com/documentation/uikit/uiviewcontroller/1621454-loadview
override func loadView() {
// https://developer.apple.com/documentation/webkit/wkwebviewconfiguration
let configuration = WKWebViewConfiguration()
// https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler
configuration.setURLSchemeHandler(USchemeHandler(), forURLScheme: AppWebViewUrlScheme)
// https://developer.apple.com/documentation/webkit/wkusercontentcontroller
configuration.userContentController.addScriptMessageHandler(self, contentWorld: .page, name: "controller")
// https://developer.apple.com/documentation/webkit/wkwebview
self.webView = WKWebView(frame: .zero, configuration: configuration)
#if os(iOS)
self.webView.isOpaque = false
// https://developer.apple.com/documentation/uikit/appearance_customization/supporting_dark_mode_in_your_interface#2993897
self.webView.backgroundColor = UIColor(named: "USBackgroundColor")
// self.webView.backgroundColor = .clear // DEBUG
// self.webView.scrollView.isScrollEnabled = false
#elseif os(macOS)
self.webView.frame = NSRect(x: 0, y: 0, width: 600, height: 600)
self.webView.setValue(false, forKey: "drawsBackground")
#endif
// https://developer.apple.com/documentation/webkit/wknavigationdelegate
self.webView.navigationDelegate = self
#if DEBUG
// https://webkit.org/blog/13936/enabling-the-inspection-of-web-content-in-apps/
if #available(macOS 13.3, iOS 16.4, tvOS 16.4, *) {
// https://developer.apple.com/documentation/webkit/wkwebview/4111163-inspectable/
self.webView.isInspectable = true
}
logger?.debug("\(#function, privacy: .public) - DEBUG mode: isInspectable = true")
#endif
self.view = self.webView
}
// https://developer.apple.com/documentation/uikit/uiviewcontroller/1621495-viewdidload
override func viewDidLoad() {
super.viewDidLoad()
#if VITE
let url = URL(string: "https://userscripts.test:55173/entry-app-webview.html")!
#else
let url = URL(string: "\(AppWebViewUrlScheme):///")!
#endif
self.webView.load(URLRequest(url: url))
#if os(macOS)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleOcclusionStateChange),
name: NSApplication.didChangeOcclusionStateNotification,
object: nil
)
#endif
}
// https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455629-webview
// DOMContentLoaded
// func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// webView.evaluateJavaScript("")
// }
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
guard let url = navigationAction.request.url else {
decisionHandler(.allow)
return
}
// allow registration scheme
if url.scheme == AppWebViewUrlScheme {
decisionHandler(.allow)
return
}
// allow specified url prefixes
// let allowPrefixes = [
// "https://github.com/quoid/userscripts"
// ]
// for prefix in allowPrefixes {
// if url.absoluteString.lowercased().hasPrefix(prefix) {
// decisionHandler(.allow)
// return
// }
// }
// open from external app like safari
#if os(iOS)
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
decisionHandler(.cancel)
return
}
#elseif os(macOS)
if let _ = NSWorkspace.shared.urlForApplication(toOpen: url) {
NSWorkspace.shared.open(url)
decisionHandler(.cancel)
return
}
#endif
decisionHandler(.allow)
}
decisionHandler(.allow)
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) async -> (Any?, String?) {
guard let name = message.body as? String else {
logger?.error("\(#function, privacy: .public) - Userscripts iOS received a message without a name")
return (nil, "bad message body")
}
logger?.debug("\(#function, privacy: .public) - Got message: \(name)")
switch name {
case "INIT":
let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0.0"
let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "0"
let firstRunTime = Preferences.firstRunTime
if(firstRunTime == 0) {
Preferences.firstRunTime = Int(Date().timeIntervalSince1970 * 1000)
}
var useSettingsInsteadOfPreferences = false
#if os(iOS)
let platform = "ios"
#elseif os(macOS)
let platform = "mac"
do {
let state = try await SFSafariExtensionManager.stateOfSafariExtension(withIdentifier: extIdentifier)
self.extensionState = state.isEnabled ? .on : .off
} catch {
self.extensionState = .error
logger?.error("\(#function, privacy: .public) - \(error, privacy: .public)")
}
if #available(macOS 13, *) {
useSettingsInsteadOfPreferences = true
}
#endif
return ([
"build": buildNumber,
"version": appVersion,
"platform": platform,
"directory": getCurrentScriptsDirectoryString(),
"extStatus": self.extensionState.rawValue,
"useSettingsInsteadOfPreferences": useSettingsInsteadOfPreferences,
"enableLogger": Preferences.enableLogger,
"promptLogger": Preferences.promptLogger,
"maxLogFileSize": Preferences.maxLogFileSize,
"firstRunTime": firstRunTime,
], nil)
case "CHANGE_DIRECTORY":
changeSaveLocation()
break
case "OPEN_DIRECTORY":
#if os(iOS)
guard let saveLocation = getSaveLocation() else {
return (nil, "Uninitialized save location")
}
guard var components = URLComponents(url: saveLocation, resolvingAgainstBaseURL: true) else {
return (nil, "ScriptsDirectoryUrl malformed")
}
components.scheme = "shareddocuments"
if let url = components.url, UIApplication.shared.canOpenURL(url) {
await UIApplication.shared.open(url)
}
#elseif os(macOS)
guard openSaveLocation() else {
return (nil, "ScriptsDirectoryUrl malformed")
}
#endif
break
#if os(macOS)
case "SHOW_PREFERENCES":
do {
try await SFSafariApplication.showPreferencesForExtension(withIdentifier: extIdentifier)
} catch {
logger?.error("\(#function, privacy: .public) - \(error, privacy: .public)")
return (nil, error.localizedDescription)
}
break
#endif
case "EXPORT_LOG_FILES":
// write the logStore so far to log files (async, no guarantee)
USLogStoreToFile(prioritize: true)
// call the export func of different platforms
exportLogFiles()
break
case "DISABLE_LOGGER":
USLoggerSwitch(false)
break
case "DISMISS_LOGGER_PROMPT":
Preferences.promptLogger = false
break
default:
return (nil, "Unexpected message body")
}
return (nil, nil)
}
func updateEnableLoggerState() {
self.webView.evaluateJavaScript("webapp.switchLogger(\(Preferences.enableLogger),\(Preferences.promptLogger))")
}
func updateCurrentDirectory() {
self.webView.evaluateJavaScript("webapp.updateDirectory('\(getCurrentScriptsDirectoryString())')")
}
#if os(macOS)
deinit {
NotificationCenter.default.removeObserver(
self,
name: NSApplication.didChangeOcclusionStateNotification,
object: nil
)
DistributedNotificationCenter.default.removeObserver(
self,
name: NSNotification.Name("AppleInterfaceThemeChangedNotification"),
object: nil
)
}
#endif
}
#if os(iOS)
// https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories
// https://developer.apple.com/documentation/uikit/uidocumentpickerviewcontroller
extension ViewController: UIDocumentPickerDelegate {
// UIDocumentPickerDelegate
// https://developer.apple.com/documentation/uikit/uidocumentpickerdelegate/2902364-documentpicker
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
guard let handler = documentPickerHandler else {
logger?.debug("\(#function, privacy: .public) - documentPickerHandler not set")
return
}
// call handler
handler(url)
// reset handler
documentPickerHandler = nil
}
func changeSaveLocation() {
logger?.info("\(#function, privacy: .public) - Userscripts iOS has requested to set the readLocation")
// set handler
documentPickerHandler = changeSaveLocationHandler
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
documentPicker.delegate = self
documentPicker.directoryURL = getDocumentsDirectory()
present(documentPicker, animated: true)
}
func changeSaveLocationHandler(_ url: URL) {
Preferences.scriptsDirectoryUrl = url
self.updateCurrentDirectory()
}
func exportLogFiles() {
// set handler
documentPickerHandler = exportLogFilesHandler
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
documentPicker.delegate = self
present(documentPicker, animated: true)
}
func exportLogFilesHandler(_ url: URL) {
guard url.startAccessingSecurityScopedResource() else {
logger?.error("\(#function, privacy: .public) - failed access url: \(url, privacy: .public)")
return
}
defer { url.stopAccessingSecurityScopedResource() }
// export log files and open output directory
do {
let outURL = try USLogFilesExportTo(url)
guard var components = URLComponents(url: outURL, resolvingAgainstBaseURL: true) else {
logger?.error("\(#function, privacy: .public) - url malformed: \(url, privacy: .public)")
return
}
components.scheme = "shareddocuments"
if let url = components.url, UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}
} catch {
logger?.error("\(#function, privacy: .public) - Export failed: \(error)")
let alert = UIAlertController(
title: "Export failed",
message: "\(error)",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
}
}
}
#elseif os(macOS)
extension ViewController {
// Only update the view when the window is visible
@objc func handleOcclusionStateChange() {
let occlusionState = NSApplication.shared.occlusionState
if occlusionState.contains(.visible) {
if timerExtensionStateCheck?.isValid != true {
timerExtensionStateCheck = Timer.scheduledTimer(
timeInterval: 0.2,
target: self,
selector: #selector(handleExtensionStateChange),
userInfo: nil,
repeats: true
)
}
} else {
timerExtensionStateCheck?.invalidate()
}
}
// Update webview extension status display via timer
@objc func handleExtensionStateChange() {
SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extIdentifier) { (state, error) in
var status: ExtensionState;
if let state = state {
status = state.isEnabled ? .on : .off
} else {
status = .error
if let error = error {
logger?.error("\(#function, privacy: .public) - \(error, privacy: .public)")
} else {
logger?.error("\(#function, privacy: .public) - couldn't get safari extension state in containing app")
}
}
if self.extensionState == status { return }
self.extensionState = status
DispatchQueue.main.async {
self.webView.evaluateJavaScript("webapp.updateExtStatus('\(status.rawValue)')")
}
}
}
func changeSaveLocation() {
guard let window = self.view.window else { return }
let saveLocationURL = getSaveLocationURL()
let panel = changeSaveLocationPanel(directoryURL: saveLocationURL)
panel.beginSheetModal(for: window, completionHandler: { response in
// check if clicked open button and there is a valid result
guard response == .OK, let url: URL = panel.url else { return }
// revoke implicitly starts security-scoped access
defer { url.stopAccessingSecurityScopedResource() }
// check if path has indeed changed
if url.absoluteString == saveLocationURL.absoluteString { return }
// try set new save location path to bookmark
guard setSaveLocationURL(url: url) else { return }
// update user interface text display
self.updateCurrentDirectory()
// notify browser extension of relevant updates
sendExtensionMessage(
name: "SAVE_LOCATION_CHANGED",
userInfo: [
"saveLocation": url.absoluteString.removingPercentEncoding ?? url.absoluteString,
"returnApp": true
]
)
})
}
func exportLogFiles() {
guard let window = self.view.window else { return }
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = true
panel.canChooseFiles = false
panel.title = "Export Log Files"
panel.beginSheetModal(for: window, completionHandler: { response in
panel.close()
// check if clicked save button and there is a valid result
guard response == .OK, let url: URL = panel.url else { return }
// revoke implicitly starts security-scoped access
defer { url.stopAccessingSecurityScopedResource() }
// export log files and open output directory
do {
let outURL = try USLogFilesExportTo(url)
NSWorkspace.shared.selectFile(outURL.path, inFileViewerRootedAtPath: url.path)
} catch {
logger?.error("\(#function, privacy: .public) - Export failed: \(error)")
let alert = NSAlert()
alert.messageText = "Export failed - \(error)"
alert.runModal()
}
})
}
}
#endif