Skip to content

Commit 6f36c9b

Browse files
committed
Remove userScripts property
1 parent 87e0594 commit 6f36c9b

File tree

3 files changed

+36
-31
lines changed

3 files changed

+36
-31
lines changed

Demo/WKJavaScriptController-Demo/ViewController.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,26 @@ class ViewController: UIViewController {
5858
let javaScriptController = WKJavaScriptController(name: "native", target: self, bridgeProtocol: JavaScriptInterface.self)
5959

6060
// [Optional] Add your javascript.
61+
let configuration = WKWebViewConfiguration()
62+
63+
let userContentController = WKUserContentController()
6164
let jsPath = Bundle.main.path(forResource: "index", ofType: "js")!
62-
let jsString = try! String(contentsOfFile: jsPath, encoding: String.Encoding.utf8)
63-
let userScript = WKUserScript(source: jsString, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
64-
javaScriptController.addUserScript(userScript)
65+
let jsCode = try! String(contentsOfFile: jsPath, encoding: String.Encoding.utf8)
66+
let userScript = WKUserScript(source: jsCode, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
67+
userContentController.addUserScript(userScript)
68+
69+
configuration.userContentController = userContentController
6570

66-
webView = WKWebView(frame: view.frame)
71+
// Create WKWebView instance.
72+
webView = WKWebView(frame: view.frame, configuration: configuration)
6773
webView.uiDelegate = self
6874
view.addSubview(webView)
6975

7076
// Assign javaScriptController.
7177
webView.javaScriptController = javaScriptController
7278

7379
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")!
74-
let htmlString = try! String(contentsOfFile: htmlPath, encoding: String.Encoding.utf8)
80+
let htmlString = try! String(contentsOfFile: htmlPath, encoding: .utf8)
7581
webView.prepareForJavaScriptController() // Call prepareForJavaScriptController before initializing WKWebView or loading page.
7682
webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
7783
}

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,16 @@ class ViewController: UIViewController {
6969
let javaScriptController = WKJavaScriptController(name: "native", target: self, bridgeProtocol: JavaScriptInterface.self)
7070

7171
// [Optional] Add your javascript.
72-
let jsString = ...
73-
let userScript = WKUserScript(source: jsString, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)
74-
javaScriptController.addUserScript(userScript)
72+
let configuration = WKWebViewConfiguration()
7573

76-
let webView = WKWebView(...)
77-
...
74+
let jsCode = ...
75+
let userScript = WKUserScript(source: jsCode, injectionTime: .AtDocumentEnd, forMainFrameOnly: true)
76+
userContentController.addUserScript(userScript)
77+
78+
configuration.userContentController = userContentController
79+
80+
// Create WKWebView instance.
81+
webView = WKWebView(frame: view.frame, configuration: configuration)
7882

7983
// Assign javaScriptController.
8084
webView.javaScriptController = javaScriptController

Sources/WKJavaScriptController/WKJavaScriptController.swift

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public extension Notification.Name {
6666
static let WKJavaScriptControllerWillMethodInvocation = Notification.Name("WKJavaScriptControllerWillMethodInvocationNotification")
6767
}
6868

69+
private let identifier = "/* WKJavaScriptController */"
70+
6971
open 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

Comments
 (0)