@@ -3,9 +3,7 @@ import WebKit
33
44class DocumentationViewController :
55 NSViewController ,
6- WKNavigationDelegate ,
7- WKUIDelegate ,
8- WKScriptMessageHandler
6+ WKNavigationDelegate
97{
108
119 @objc enum ViewerState : Int {
@@ -29,13 +27,20 @@ class DocumentationViewController:
2927 loadWebsite ( )
3028 }
3129
30+ override func viewDidDisappear( ) {
31+ let ucc = webView. configuration. userContentController
32+ ucc. removeScriptMessageHandler ( forName: " vcBus " )
33+ ucc. removeAllUserScripts ( )
34+ super. viewDidDisappear ( )
35+ }
36+
3237 private func setupWebView( ) {
3338 let config = WKWebViewConfiguration ( )
3439
3540 let userContentController = WKUserContentController ( )
3641 config. userContentController = userContentController;
3742
38- userContentController. add ( self , name: " vcBus " ) ;
43+ userContentController. add ( WeakWKScriptMessageHandler . init ( self ) , name: " vcBus " ) ;
3944
4045 if let integrationScript = readUserScript ( " integration " ) {
4146 let integration = WKUserScript ( source: integrationScript, injectionTime: . atDocumentStart, forMainFrameOnly: true )
@@ -95,6 +100,8 @@ class DocumentationViewController:
95100 webView. load ( request)
96101 }
97102
103+ // MARK:- Page search
104+
98105 func showSearchControl( ) {
99106 if viewerState != . ready {
100107 return
@@ -108,60 +115,6 @@ class DocumentationViewController:
108115 vc. dismissSearch ( self )
109116 }
110117
111- // MARK:- WKUIDelegate
112-
113- func webView( _ webView: WKWebView ,
114- createWebViewWith configuration: WKWebViewConfiguration ,
115- for navigationAction: WKNavigationAction ,
116- windowFeatures: WKWindowFeatures ) -> WKWebView ? {
117- guard let requestURL = navigationAction. request. url else {
118- return nil
119- }
120-
121- if let host = requestURL. host, host == " devdocs.io " {
122- DocumentationWindows . shared. newWindowFor ( url: requestURL)
123- return nil
124- }
125-
126- if let scheme = requestURL. scheme {
127- switch scheme {
128- case " http " , " https " , " mailto " :
129- NSWorkspace . shared. open ( requestURL)
130- default :
131- break ;
132- }
133- }
134-
135- return nil
136- }
137-
138- // MARK:- WKScriptMessageHandler
139-
140- func userContentController( _ userContentController: WKUserContentController , didReceive message: WKScriptMessage ) {
141- guard let msg = message. body as? [ AnyHashable : Any ] else {
142- return
143- }
144- guard let type = msg [ " type " ] as? String else {
145- return
146- }
147- switch type {
148- case " afterInit " :
149- handleAfterInit ( )
150- case " titleNotification " :
151- guard let args = msg [ " args " ] as? [ AnyHashable : Any ] else {
152- return
153- }
154- handleTitleNotification ( args)
155- case " locationNotification " :
156- guard let args = msg [ " args " ] as? [ AnyHashable : Any ] else {
157- return
158- }
159- handleLocationNotification ( args)
160- default :
161- return
162- }
163- }
164-
165118 // MARK:- JS integration
166119
167120 func useDarkMode( _ using: Bool ) {
@@ -216,6 +169,63 @@ class DocumentationViewController:
216169 }
217170}
218171
172+ // MARK:- WKUIDelegate
173+ extension DocumentationViewController : WKUIDelegate {
174+ func webView( _ webView: WKWebView ,
175+ createWebViewWith configuration: WKWebViewConfiguration ,
176+ for navigationAction: WKNavigationAction ,
177+ windowFeatures: WKWindowFeatures ) -> WKWebView ? {
178+ guard let requestURL = navigationAction. request. url else {
179+ return nil
180+ }
181+
182+ if let host = requestURL. host, host == " devdocs.io " {
183+ DocumentationWindows . shared. newWindow ( forURL: requestURL)
184+ return nil
185+ }
186+
187+ if let scheme = requestURL. scheme {
188+ switch scheme {
189+ case " http " , " https " , " mailto " :
190+ NSWorkspace . shared. open ( requestURL)
191+ default :
192+ break ;
193+ }
194+ }
195+
196+ return nil
197+ }
198+ }
199+
200+ // MARK:- WKScriptMessageHandler
201+ extension DocumentationViewController : WKScriptMessageHandler {
202+ func userContentController( _ userContentController: WKUserContentController ,
203+ didReceive message: WKScriptMessage ) {
204+ guard let msg = message. body as? [ AnyHashable : Any ] else {
205+ return
206+ }
207+ guard let type = msg [ " type " ] as? String else {
208+ return
209+ }
210+ switch type {
211+ case " afterInit " :
212+ handleAfterInit ( )
213+ case " titleNotification " :
214+ guard let args = msg [ " args " ] as? [ AnyHashable : Any ] else {
215+ return
216+ }
217+ handleTitleNotification ( args)
218+ case " locationNotification " :
219+ guard let args = msg [ " args " ] as? [ AnyHashable : Any ] else {
220+ return
221+ }
222+ handleLocationNotification ( args)
223+ default :
224+ return
225+ }
226+ }
227+ }
228+
219229// MARK:- SearchControlDelegate
220230extension DocumentationViewController : SearchControlDelegate {
221231 func search( term: String ) {
0 commit comments