2 // ViewController.swift
5 // Created by Ruben Beltran del Rio on 9/13/23.
12 let extensionBundleIdentifier = "systems.tranquil.Junction.Extension"
14 class ViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHandler {
16 @IBOutlet var webView: WKWebView!
18 override func viewDidLoad() {
21 self.webView.navigationDelegate = self
23 self.webView.configuration.userContentController.add(self, name: "controller")
25 self.webView.loadFileURL(Bundle.main.url(forResource: "Main", withExtension: "html")!, allowingReadAccessTo: Bundle.main.resourceURL!)
28 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
29 SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionBundleIdentifier) { (state, error) in
30 guard let state = state, error == nil else {
31 // Insert code to inform the user that something went wrong.
35 DispatchQueue.main.async {
36 if #available(macOS 13, *) {
37 webView.evaluateJavaScript("show(\(state.isEnabled), true)")
39 webView.evaluateJavaScript("show(\(state.isEnabled), false)")
45 func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
46 if (message.body as! String != "open-preferences") {
50 SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionBundleIdentifier) { error in
51 DispatchQueue.main.async {
52 NSApplication.shared.terminate(nil)