@@ -66,6 +66,8 @@ public extension Notification.Name {
6666 static let WKJavaScriptControllerWillMethodInvocation = Notification . Name ( " WKJavaScriptControllerWillMethodInvocationNotification " )
6767}
6868
69+ private let identifier = " /* WKJavaScriptController */"
70+
6971open class WKJavaScriptController : NSObject {
7072 // If true, do not allow NSNull(when `undefined` or `null` passed from JavaScript) for method arguments.
7173 // That is, if receive NSNull as an argument, method call ignored.
@@ -99,9 +101,6 @@ open class WKJavaScriptController: NSObject {
99101 private let name : String
100102 private weak var target : AnyObject ?
101103
102- // User script that will use the bridge.
103- private var userScripts = [ WKUserScript] ( )
104-
105104 fileprivate weak var webView : WKWebView ?
106105
107106 fileprivate var bridges = [ MethodBridge] ( )
@@ -272,21 +271,23 @@ open class WKJavaScriptController: NSObject {
272271 }
273272
274273 fileprivate func injectTo( _ userContentController: WKUserContentController ) {
275- userContentController. removeAllUserScripts ( )
276- var forMainFrameOnly = true
277- for userScript in userScripts {
278- forMainFrameOnly = forMainFrameOnly && userScript. isForMainFrameOnly
279- userContentController. addUserScript ( userScript)
274+ let userScripts = userContentController. userScripts. filter {
275+ !$0. source. hasPrefix ( identifier)
280276 }
281- userContentController. addUserScript ( bridgeScript ( forMainFrameOnly) )
277+ userContentController. removeAllUserScripts ( )
278+
279+ userContentController. addUserScript ( bridgeScript ( ) )
282280 for bridge in bridges {
283281 userContentController. removeScriptMessageHandler ( forName: bridge. jsSelector)
284282 userContentController. add ( self , name: bridge. jsSelector)
285283 }
284+
285+ userScripts. forEach { userContentController. addUserScript ( $0) }
286+
286287 isInjectRequired = false
287288 }
288289
289- private func bridgeScript( _ forMainFrameOnly : Bool ) -> WKUserScript {
290+ private func bridgeScript( ) -> WKUserScript {
290291 var source = """
291292 window. \( name) = {
292293 \( ReserveKeyword . createUUID) : function() {
@@ -345,24 +346,18 @@ open class WKJavaScriptController: NSObject {
345346 });
346347 """
347348 }
348- return WKUserScript ( source: source, injectionTime: . atDocumentStart, forMainFrameOnly: forMainFrameOnly)
349+ return WKUserScript (
350+ source: " \( identifier) \n \( source) " ,
351+ injectionTime: . atDocumentStart,
352+ forMainFrameOnly: true
353+ )
349354 }
350355
351356 private func log( _ message: String ) {
352357 if logEnabled {
353358 NSLog ( " [WKJavaScriptController] \( message) " )
354359 }
355360 }
356-
357- open func addUserScript( _ userScript: WKUserScript ) {
358- userScripts. append ( userScript)
359- isInjectRequired = true
360- }
361-
362- open func removeAllUserScripts( ) {
363- userScripts. removeAll ( )
364- isInjectRequired = true
365- }
366361}
367362
368363// MARK: - WKScriptMessageHandler
0 commit comments