Skip to content

Commit 3cdfda1

Browse files
authored
Inject EPUB scripts with WKWebView (#204)
Former-commit-id: 06d877f
1 parent d40c3de commit 3cdfda1

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

navigator/r2-navigator-swift/EPUB/EPUBFixedSpreadView.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import Foundation
88
import UIKit
99
import WebKit
10+
import R2Shared
1011

1112

1213
/// A view rendering a spread of resources with a fixed layout.
@@ -17,6 +18,15 @@ final class EPUBFixedSpreadView: EPUBSpreadView {
1718
/// URL to load in the iframe once the wrapper page is loaded.
1819
private var urlToLoad: URL?
1920

21+
private static let fixedScript = loadScript(named: "readium-fixed")
22+
23+
required init(publication: Publication, spread: EPUBSpread, resourcesURL: URL, readingProgression: ReadingProgression, userSettings: UserSettings, scripts: [WKUserScript], animatedLoad: Bool, editingActions: EditingActionsController, contentInset: [UIUserInterfaceSizeClass: EPUBContentInsets]) {
24+
var scripts = scripts
25+
scripts.append(WKUserScript(source: Self.fixedScript, injectionTime: .atDocumentStart, forMainFrameOnly: false))
26+
27+
super.init(publication: publication, spread: spread, resourcesURL: resourcesURL, readingProgression: readingProgression, userSettings: userSettings, scripts: scripts, animatedLoad: animatedLoad, editingActions: editingActions, contentInset: contentInset)
28+
}
29+
2030
override func setupWebView() {
2131
super.setupWebView()
2232

navigator/r2-navigator-swift/EPUB/EPUBReflowableSpreadView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ final class EPUBReflowableSpreadView: EPUBSpreadView {
1616
private var topConstraint: NSLayoutConstraint!
1717
private var bottomConstraint: NSLayoutConstraint!
1818

19+
private static let reflowableScript = loadScript(named: "readium-reflowable")
20+
1921
required init(publication: Publication, spread: EPUBSpread, resourcesURL: URL, readingProgression: ReadingProgression, userSettings: UserSettings, scripts: [WKUserScript], animatedLoad: Bool, editingActions: EditingActionsController, contentInset: [UIUserInterfaceSizeClass: EPUBContentInsets]) {
2022
var scripts = scripts
23+
2124
let layout = ReadiumCSSLayout(languages: publication.metadata.languages, readingProgression: readingProgression)
2225
scripts.append(WKUserScript(
2326
source: "window.readiumCSSBaseURL = '\(resourcesURL.appendingPathComponent(layout.readiumCSSBasePath))'",
2427
injectionTime: .atDocumentStart,
2528
forMainFrameOnly: false
2629
))
30+
31+
scripts.append(WKUserScript(source: Self.reflowableScript, injectionTime: .atDocumentStart, forMainFrameOnly: false))
32+
2733
super.init(publication: publication, spread: spread, resourcesURL: resourcesURL, readingProgression: readingProgression, userSettings: userSettings, scripts: scripts, animatedLoad: animatedLoad, editingActions: editingActions, contentInset: contentInset)
2834
}
2935

navigator/r2-navigator-swift/EPUB/EPUBSpreadView.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class EPUBSpreadView: UIView, Loggable, PageView {
4646

4747
let readingProgression: ReadingProgression
4848
let userSettings: UserSettings
49-
let scripts: [WKUserScript]
5049
let editingActions: EditingActionsController
5150

5251
private var lastClick: ClickEvent? = nil
@@ -73,7 +72,6 @@ class EPUBSpreadView: UIView, Loggable, PageView {
7372
self.resourcesURL = resourcesURL
7473
self.readingProgression = readingProgression
7574
self.userSettings = userSettings
76-
self.scripts = scripts
7775
self.editingActions = editingActions
7876
self.animatedLoad = animatedLoad
7977
self.webView = WebView(editingActions: editingActions)
@@ -404,6 +402,13 @@ class EPUBSpreadView: UIView, Loggable, PageView {
404402
applyUserSettingsStyle()
405403
}
406404

405+
406+
// MARK: - Scripts
407+
408+
class func loadScript(named name: String) -> String {
409+
return Bundle.module.url(forResource: "\(name)", withExtension: "js", subdirectory: "Assets/Static/scripts")
410+
.flatMap { try? String(contentsOf: $0) }!
411+
}
407412
}
408413

409414
// MARK: - WKScriptMessageHandler for handling incoming message from the javascript layer.

0 commit comments

Comments
 (0)