From e39fa31bf706ea91e8230d9263c5d69c03f9035e Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Mon, 16 Sep 2019 21:06:35 -0500 Subject: [PATCH] Add a menu item to the Debug menu to enable the Web Inspector Just enables the "Inspect Element" item in a WKWebView's contextual menu at the moment. --- Mac/AppDefaults.swift | 11 ++++++++++ Mac/AppDelegate.swift | 12 ++++++++++ Mac/Base.lproj/Main.storyboard | 7 ++++++ .../Detail/DetailWebViewController.swift | 22 +++++++++++++++++++ Shared/AppNotifications.swift | 1 + 5 files changed, 53 insertions(+) diff --git a/Mac/AppDefaults.swift b/Mac/AppDefaults.swift index f7d8a8217..c35d99644 100644 --- a/Mac/AppDefaults.swift +++ b/Mac/AppDefaults.swift @@ -36,6 +36,8 @@ struct AppDefaults { static let timelineShowsSeparators = "CorreiaSeparators" static let showTitleOnMainWindow = "KafasisTitleMode" static let hideDockUnreadCount = "JustinMillerHideDockUnreadCount" + + static let webInspectorEnabled = "WebInspectorEnabled" } private static let smallestFontSizeRawValue = FontSize.small.rawValue @@ -138,6 +140,15 @@ struct AppDefaults { return bool(for: Key.hideDockUnreadCount) } + static var webInspectorEnabled: Bool { + get { + return bool(for: Key.webInspectorEnabled) + } + set { + setBool(for: Key.webInspectorEnabled, newValue) + } + } + static var timelineSortDirection: ComparisonResult { get { return sortDirection(for: Key.timelineSortDirection) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index f1a3673ef..804b79d1f 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -309,6 +309,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, if item.action == #selector(showAddFeedWindow(_:)) || item.action == #selector(showAddFolderWindow(_:)) { return !isDisplayingSheet && !AccountManager.shared.activeAccounts.isEmpty } + if item.action == #selector(toggleWebInspectorEnabled(_:)) { + (item as! NSMenuItem).state = AppDefaults.webInspectorEnabled ? .on : .off + } return true } @@ -525,6 +528,15 @@ extension AppDelegate { @IBAction func debugSearch(_ sender: Any?) { AccountManager.shared.defaultAccount.debugRunSearch() } + + @IBAction func toggleWebInspectorEnabled(_ sender: Any?) { + let newValue = !AppDefaults.webInspectorEnabled + AppDefaults.webInspectorEnabled = newValue + // An attached inspector can display incorrectly on certain setups (like mine); default to displaying in a separate window, + // And reset to a separate window when the preference is toggled off and on again in case the inspector is accidentally reattached. + UserDefaults.standard.set(false, forKey: "__WebInspectorPageGroupLevel1__.WebKit2InspectorStartsAttached") + NotificationCenter.default.post(name: .WebInspectorEnabledDidChange, object: newValue) + } } private extension AppDelegate { diff --git a/Mac/Base.lproj/Main.storyboard b/Mac/Base.lproj/Main.storyboard index 8d270183a..e12b64504 100644 --- a/Mac/Base.lproj/Main.storyboard +++ b/Mac/Base.lproj/Main.storyboard @@ -469,6 +469,13 @@ + + + + + + + diff --git a/Mac/MainWindow/Detail/DetailWebViewController.swift b/Mac/MainWindow/Detail/DetailWebViewController.swift index b4b92ad52..b1b364a51 100644 --- a/Mac/MainWindow/Detail/DetailWebViewController.swift +++ b/Mac/MainWindow/Detail/DetailWebViewController.swift @@ -27,6 +27,22 @@ final class DetailWebViewController: NSViewController, WKUIDelegate { } } } + + private var webInspectorEnabled: Bool { + get { + if let webView = webView { + let val: NSNumber? = webView.configuration.preferences.value(forKey: "developerExtrasEnabled") as? NSNumber + return val != nil ? val!.boolValue : false + } + + return false + } + set { + if let webView = webView { + webView.configuration.preferences.setValue(newValue, forKey: "developerExtrasEnabled") + } + } + } private var waitingForFirstReload = false private let keyboardDelegate = DetailKeyboardDelegate() @@ -87,6 +103,12 @@ final class DetailWebViewController: NSViewController, WKUIDelegate { webView.isHidden = true waitingForFirstReload = true + webInspectorEnabled = AppDefaults.webInspectorEnabled + + NotificationCenter.default.addObserver(forName: .WebInspectorEnabledDidChange, object: nil, queue: OperationQueue.main) { (notification) in + self.webInspectorEnabled = notification.object! as! Bool + } + reloadHTML() } diff --git a/Shared/AppNotifications.swift b/Shared/AppNotifications.swift index b847f1988..3fd03355a 100644 --- a/Shared/AppNotifications.swift +++ b/Shared/AppNotifications.swift @@ -13,6 +13,7 @@ extension Notification.Name { static let InspectableObjectsDidChange = Notification.Name("TimelineSelectionDidChangeNotification") static let UserDidAddFeed = Notification.Name("UserDidAddFeedNotification") static let UserDidRequestSidebarSelection = Notification.Name("UserDidRequestSidebarSelectionNotification") + static let WebInspectorEnabledDidChange = Notification.Name("WebInspectorEnabledDidChange") } typealias UserInfoDictionary = [AnyHashable: Any]