Skip to content

Commit

Permalink
added options to inject userScripts and messageHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejcheng committed May 28, 2020
1 parent fcbba84 commit a776d60
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
12 changes: 12 additions & 0 deletions Sources/WKZombie/RenderOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ internal class RenderOperation : Operation {
var authenticationBlock : AuthenticationHandler?
var postAction: PostAction = .none

var messageHandlers: [String:(_ message: WKScriptMessage)->Void] = [:]

internal fileprivate(set) var result : Data?
internal fileprivate(set) var response : URLResponse?
internal fileprivate(set) var error : Error?
Expand Down Expand Up @@ -160,6 +162,10 @@ internal class RenderOperation : Operation {

fileprivate func setupReferences() {
dispatch_sync_on_main_thread {
for name in messageHandlers.keys {
webView?.configuration.userContentController.removeScriptMessageHandler(forName: name)
webView?.configuration.userContentController.add(self, name: name)
}
webView?.configuration.userContentController.add(self, name: "doneLoading")
webView?.navigationDelegate = self
}
Expand Down Expand Up @@ -191,6 +197,12 @@ extension RenderOperation : WKScriptMessageHandler {
webView.stopLoading()
self.webView(webView, didFinish: nil)
}

for (name, messageHandler) in self.messageHandlers {
if message.name == name {
messageHandler(message)
}
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions Sources/WKZombie/Renderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ internal class Renderer {
}()

fileprivate var webView : WKWebView!
fileprivate var messageHandlers: [String:(_ message: WKScriptMessage)->Void]


init(processPool: WKProcessPool? = nil) {
init(processPool: WKProcessPool? = nil, userScripts: [WKUserScript] = [], messageHandlers: [String:(_ message: WKScriptMessage)->Void] = [:]) {
let doneLoadingWithoutMediaContentScript = "window.webkit.messageHandlers.doneLoading.postMessage(\(Renderer.scrapingCommand));"
let doneLoadingUserScript = WKUserScript(source: doneLoadingWithoutMediaContentScript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)

Expand All @@ -71,7 +71,13 @@ internal class Renderer {
let contentController = WKUserContentController()
contentController.addUserScript(doneLoadingUserScript)
contentController.addUserScript(getElementUserScript)


for userScript in userScripts {
contentController.addUserScript(userScript)
}

self.messageHandlers = messageHandlers

let config = WKWebViewConfiguration()
config.processPool = processPool ?? WKProcessPool()
config.userContentController = contentController
Expand Down Expand Up @@ -181,6 +187,7 @@ internal class Renderer {
}
operation.requestBlock = requestBlock
operation.authenticationBlock = authenticationHandler
operation.messageHandlers = messageHandlers
return operation
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/WKZombie/WKZombie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public class WKZombie {

- returns: A WKZombie instance.
*/
public init(name: String? = "WKZombie", processPool: WKProcessPool? = nil) {
public init(name: String? = "WKZombie", processPool: WKProcessPool? = nil, userScripts: [WKUserScript] = [], messageHandlers: [String:(_ message: WKScriptMessage)->Void] = [:]) {
self.name = name
self._renderer = Renderer(processPool: processPool)
self._renderer = Renderer(processPool: processPool, userScripts: userScripts, messageHandlers: messageHandlers)
self._fetcher = ContentFetcher()
}

Expand Down

0 comments on commit a776d60

Please sign in to comment.