Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
Add new EPUBNavigatorDelegate APIs to inject custom JavaScript (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaltzer authored Apr 12, 2021
1 parent 313576e commit 338405f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 18 additions & 2 deletions r2-navigator-swift/EPUB/EPUBNavigatorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 7 additions & 3 deletions r2-navigator-swift/EPUB/EPUBSpreadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand Down Expand Up @@ -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)
}

}
Expand Down

0 comments on commit 338405f

Please sign in to comment.