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