diff --git a/CHANGELOG.md b/CHANGELOG.md index a08b067e..69cbd832 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added + +* New `EPUBNavigatorDelegate` APIs to inject custom JavaScript. + * Override `navigator(_:setupUserScripts:)` to register additional user script to the `WKUserContentController` of each web view. + * Override `navigator(_:userContentController:didReceive:)` to receive callbacks from your scripts. + ### Changed * CocoaPods is not supported anymore. diff --git a/r2-navigator-swift/EPUB/EPUBNavigatorViewController.swift b/r2-navigator-swift/EPUB/EPUBNavigatorViewController.swift index 8343da65..7801ecaa 100644 --- a/r2-navigator-swift/EPUB/EPUBNavigatorViewController.swift +++ b/r2-navigator-swift/EPUB/EPUBNavigatorViewController.swift @@ -17,7 +17,12 @@ import SwiftSoup public protocol EPUBNavigatorDelegate: VisualNavigatorDelegate { - + + // MARK: - WebView Customization + + func navigator(_ navigator: EPUBNavigatorViewController, setupUserScripts userContentController: WKUserContentController) + func navigator(_ navigator: EPUBNavigatorViewController, userContentController: WKUserContentController, didReceive message: WKScriptMessage) + // MARK: - Deprecated // Implement `NavigatorDelegate.navigator(didTapAt:)` instead. @@ -34,7 +39,10 @@ public protocol EPUBNavigatorDelegate: VisualNavigatorDelegate { } public extension EPUBNavigatorDelegate { - + + func navigator(_ navigator: EPUBNavigatorViewController, setupUserScripts userContentController: WKUserContentController) {} + func navigator(_ navigator: EPUBNavigatorViewController, userContentController: WKUserContentController, didReceive message: WKScriptMessage) {} + func middleTapHandler() {} func willExitPublication(documentIndex: Int, progression: Double?) {} func didChangedDocumentPage(currentDocumentIndex: Int) {} @@ -635,6 +643,10 @@ extension EPUBNavigatorViewController: EPUBSpreadViewDelegate { present(viewController, animated: true) } + func spreadView(_ spreadView: EPUBSpreadView, userContentController: WKUserContentController, didReceive message: WKScriptMessage) { + delegate?.navigator(self, userContentController: userContentController, didReceive: message) + } + } extension EPUBNavigatorViewController: EditingActionsControllerDelegate { @@ -663,6 +675,10 @@ extension EPUBNavigatorViewController: PaginationViewDelegate { contentInset: config.contentInset ) spreadView.delegate = self + + let userContentController = spreadView.webView.configuration.userContentController + delegate?.navigator(self, setupUserScripts: userContentController) + return spreadView } diff --git a/r2-navigator-swift/EPUB/EPUBSpreadView.swift b/r2-navigator-swift/EPUB/EPUBSpreadView.swift index 2eb2e25a..9c588d1a 100644 --- a/r2-navigator-swift/EPUB/EPUBSpreadView.swift +++ b/r2-navigator-swift/EPUB/EPUBSpreadView.swift @@ -30,6 +30,9 @@ protocol EPUBSpreadViewDelegate: class { /// Called when the spread view needs to present a view controller. func spreadView(_ spreadView: EPUBSpreadView, present viewController: UIViewController) + + /// Called when the spread view receives an unknown JavaScript message. + func spreadView(_ spreadView: EPUBSpreadView, userContentController: WKUserContentController, didReceive message: WKScriptMessage) } @@ -395,10 +398,11 @@ extension EPUBSpreadView: WKScriptMessageHandler { /// Handles incoming calls from JS. func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { - guard let handler = JSMessages[message.name] else { - return + if let handler = JSMessages[message.name] { + handler(message.body) + } else { + delegate?.spreadView(self, userContentController: userContentController, didReceive: message) } - handler(message.body) } }