aboutsummaryrefslogtreecommitdiff
path: root/safari/Junction/ViewController.swift
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2023-09-13 18:39:01 +0200
committerRuben Beltran del Rio <ruben@unlimited.pizza>2023-09-13 18:39:01 +0200
commit4a191e8036581893bf5f4cfb284770d1bfe9a807 (patch)
treeabbf7be12b65b56c9b6a11861fd96ad406ad4590 /safari/Junction/ViewController.swift
parent06b7617a863d00dbcc91d1e46f09942c6aad366e (diff)
Add safari code
Diffstat (limited to 'safari/Junction/ViewController.swift')
-rw-r--r--safari/Junction/ViewController.swift57
1 files changed, 57 insertions, 0 deletions
diff --git a/safari/Junction/ViewController.swift b/safari/Junction/ViewController.swift
new file mode 100644
index 0000000..91f4596
--- /dev/null
+++ b/safari/Junction/ViewController.swift
@@ -0,0 +1,57 @@
+//
+// ViewController.swift
+// Junction
+//
+// Created by Ruben Beltran del Rio on 9/13/23.
+//
+
+import Cocoa
+import SafariServices
+import WebKit
+
+let extensionBundleIdentifier = "systems.tranquil.Junction.Extension"
+
+class ViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHandler {
+
+ @IBOutlet var webView: WKWebView!
+
+ override func viewDidLoad() {
+ super.viewDidLoad()
+
+ self.webView.navigationDelegate = self
+
+ self.webView.configuration.userContentController.add(self, name: "controller")
+
+ self.webView.loadFileURL(Bundle.main.url(forResource: "Main", withExtension: "html")!, allowingReadAccessTo: Bundle.main.resourceURL!)
+ }
+
+ func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
+ SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionBundleIdentifier) { (state, error) in
+ guard let state = state, error == nil else {
+ // Insert code to inform the user that something went wrong.
+ return
+ }
+
+ DispatchQueue.main.async {
+ if #available(macOS 13, *) {
+ webView.evaluateJavaScript("show(\(state.isEnabled), true)")
+ } else {
+ webView.evaluateJavaScript("show(\(state.isEnabled), false)")
+ }
+ }
+ }
+ }
+
+ func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
+ if (message.body as! String != "open-preferences") {
+ return;
+ }
+
+ SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionBundleIdentifier) { error in
+ DispatchQueue.main.async {
+ NSApplication.shared.terminate(nil)
+ }
+ }
+ }
+
+}