@@ -17,13 +17,7 @@ class ReadiumBugLogger: ReadiumShared.WarningLogger {
1717}
1818
1919private let readiumBugLogger = ReadiumBugLogger ( )
20-
21- private let scrollScripts = [
22- false : WKUserScript (
23- source: " setScrollMode(false); " , injectionTime: . atDocumentEnd, forMainFrameOnly: false ) ,
24- true : WKUserScript (
25- source: " setScrollMode(true); " , injectionTime: . atDocumentEnd, forMainFrameOnly: false ) ,
26- ]
20+ private var userScripts : [ WKUserScript ] = [ ]
2721
2822class ReadiumReaderView : NSObject , FlutterPlatformView , EPUBNavigatorDelegate {
2923
@@ -32,7 +26,6 @@ class ReadiumReaderView: NSObject, FlutterPlatformView, EPUBNavigatorDelegate {
3226 private var textLocatorStreamHandler : EventStreamHandler ?
3327 private let _view : UIView
3428 private let readiumViewController : EPUBNavigatorViewController
35- private let userScript : WKUserScript
3629 private var isVerticalScroll = false
3730
3831 var publicationIdentifier : String ?
@@ -84,11 +77,12 @@ class ReadiumReaderView: NSObject, FlutterPlatformView, EPUBNavigatorDelegate {
8477 . compact: ( top: 0 , bottom: 0 ) ,
8578 . regular: ( top: 0 , bottom: 0 ) ,
8679 ]
80+ // TODO: Make this config configurable from Flutter
8781 config. preloadPreviousPositionCount = 1
8882 config. preloadNextPositionCount = 1
8983 config. debugState = true
9084 if ( defaultPreferences != nil ) {
91- config. preferences = defaultPreferences!;
85+ config. preferences = defaultPreferences!
9286 }
9387
9488 readiumViewController = try ! EPUBNavigatorViewController (
@@ -97,17 +91,10 @@ class ReadiumReaderView: NSObject, FlutterPlatformView, EPUBNavigatorDelegate {
9791 config: config,
9892 httpServer: sharedReadium. httpServer!
9993 )
100-
101- // Add epub.js script for highlighting etc. and comics.js script for handling Nota's guided comics.
102- let comicJsAssetPath = registrar. lookupKey ( forAsset: " assets/helpers/comics.js " , fromPackage: " flutter_readium " )
103- let epubJsAssetPath = registrar. lookupKey ( forAsset: " assets/helpers/epub.js " , fromPackage: " flutter_readium " )
104- let sourceFiles = [ comicJsAssetPath, epubJsAssetPath]
105- let source = sourceFiles. map { sourceFile -> String in
106- let path = Bundle . main. path ( forResource: sourceFile, ofType: nil ) !
107- let data = FileManager ( ) . contents ( atPath: path) !
108- return String ( data: data, encoding: . utf8) !
109- } . joined ( separator: " \n " )
110- userScript = WKUserScript ( source: " const isAndroid=false,isIos=true; \n " + source, injectionTime: . atDocumentStart, forMainFrameOnly: false )
94+
95+ if userScripts. isEmpty {
96+ initUserScripts ( registrar: registrar)
97+ }
11198
11299 _view = UIView ( )
113100 super. init ( )
@@ -133,8 +120,10 @@ class ReadiumReaderView: NSObject, FlutterPlatformView, EPUBNavigatorDelegate {
133120
134121 // override EPUBNavigatorDelegate::navigator:setupUserScripts
135122 func navigator( _ navigator: EPUBNavigatorViewController , setupUserScripts userContentController: WKUserContentController ) {
136- print ( TAG, " setupUserScripts: " )
137- userContentController. addUserScript ( userScript)
123+ print ( TAG, " setupUserScripts: adding \( userScripts. count) scripts " )
124+ for script in userScripts {
125+ userContentController. addUserScript ( script)
126+ }
138127 }
139128
140129 // override EPUBNavigatorDelegate::middleTapHandler
@@ -223,10 +212,10 @@ class ReadiumReaderView: NSObject, FlutterPlatformView, EPUBNavigatorDelegate {
223212 switch await self . evaluateJavascript ( " window.epubPage.getLocatorFragments( \( locatorJson) , \( isVerticalScroll) ); " ) {
224213 case . success( let jresult) :
225214 let locatorWithFragments = try ! Locator ( json: jresult as? Dictionary < String , Any ? > , warnings: readiumBugLogger) !
226- return locatorWithFragments;
215+ return locatorWithFragments
227216 case . failure( let err) :
228217 print ( TAG, " getLocatorFragments failed! \( err) " )
229- return nil ;
218+ return nil
230219 }
231220 }
232221
@@ -245,7 +234,7 @@ class ReadiumReaderView: NSObject, FlutterPlatformView, EPUBNavigatorDelegate {
245234
246235 if shouldGo {
247236 print ( TAG, " goToLocator: Go to \( locator. href) " )
248- let goToSuccees = await readiumViewController. go ( to: locator, options: NavigatorGoOptions ( animated: false ) ) ;
237+ let goToSuccees = await readiumViewController. go ( to: locator, options: NavigatorGoOptions ( animated: false ) )
249238 if ( goToSuccees && shouldScroll) {
250239 await self . scrollTo ( locations: locations, toStart: false )
251240 self . emitOnPageChanged ( )
@@ -418,6 +407,40 @@ class ReadiumReaderView: NSObject, FlutterPlatformView, EPUBNavigatorDelegate {
418407 }
419408}
420409
410+ func initUserScripts( registrar: FlutterPluginRegistrar ) {
411+ let comicJsKey = registrar. lookupKey ( forAsset: " assets/helpers/comics.js " , fromPackage: " flutter_readium " )
412+ let comicCssKey = registrar. lookupKey ( forAsset: " assets/helpers/comics.css " , fromPackage: " flutter_readium " )
413+ let epubJsKey = registrar. lookupKey ( forAsset: " assets/helpers/epub.js " , fromPackage: " flutter_readium " )
414+ let epubCssKey = registrar. lookupKey ( forAsset: " assets/helpers/epub.css " , fromPackage: " flutter_readium " )
415+ let jsScripts = [ comicJsKey, epubJsKey] . map { sourceFile -> String in
416+ let path = Bundle . main. path ( forResource: sourceFile, ofType: nil ) !
417+ let data = FileManager ( ) . contents ( atPath: path) !
418+ return String ( data: data, encoding: . utf8) !
419+ }
420+ let addCssScripts = [ comicCssKey, epubCssKey] . map { sourceFile -> String in
421+ let path = Bundle . main. path ( forResource: sourceFile, ofType: nil ) !
422+ let data = FileManager ( ) . contents ( atPath: path) !. base64EncodedString ( )
423+ return """
424+ (function() {
425+ var parent = document.getElementsByTagName('head').item(0);
426+ var style = document.createElement('style');
427+ style.type = 'text/css';
428+ style.innerHTML = window.atob(' \( data) ');
429+ parent.appendChild(style)})();
430+ """
431+ }
432+ /// Add JS scripts right away, before loading the rest of the document.
433+ for jsScript in jsScripts {
434+ userScripts. append ( WKUserScript ( source: jsScript, injectionTime: . atDocumentStart, forMainFrameOnly: false ) )
435+ }
436+ /// Add css injection scripts after primary document finished loading.
437+ for addCssScript in addCssScripts {
438+ userScripts. append ( WKUserScript ( source: addCssScript, injectionTime: . atDocumentEnd, forMainFrameOnly: false ) )
439+ }
440+ /// Add simple script used by our JS to detect OS
441+ userScripts. append ( WKUserScript ( source: " const isAndroid=false,isIos=true; " , injectionTime: . atDocumentStart, forMainFrameOnly: false ) )
442+ }
443+
421444private func canScroll( locations: Locator . Locations ? ) -> Bool {
422445 guard let locations = locations else { return false }
423446 return locations. domRange != nil || locations. cssSelector != nil || locations. progression != nil
0 commit comments