From 9bd8c3e4627c5f16ef96731878d57b14be059e67 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Mon, 17 Oct 2016 16:34:27 -0400 Subject: [PATCH] update JS --- Kiwix-iOS/Controller/Main/JSInjection.swift | 24 ++++-------- .../Main/MainControllerDelegates.swift | 1 + .../Others/TableOfContentsController.swift | 1 + Kiwix-iOS/Info.plist | 2 +- Kiwix-iOS/Storyboard/Main.storyboard | 2 +- Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- Kiwix.xcodeproj/project.pbxproj | 16 ++------ Kiwix/JavaScripts/adjustlayoutiPad.js | 15 ------- Kiwix/JavaScripts/adjustlayoutiPhone.js | 39 ------------------- Kiwix/JavaScripts/getTableOfContents.js | 24 ------------ Kiwix/JavaScripts/injection.js | 32 +++++++++++++++ 11 files changed, 49 insertions(+), 109 deletions(-) delete mode 100644 Kiwix/JavaScripts/adjustlayoutiPad.js delete mode 100644 Kiwix/JavaScripts/adjustlayoutiPhone.js delete mode 100644 Kiwix/JavaScripts/getTableOfContents.js create mode 100644 Kiwix/JavaScripts/injection.js diff --git a/Kiwix-iOS/Controller/Main/JSInjection.swift b/Kiwix-iOS/Controller/Main/JSInjection.swift index 849ba411..a4c0834f 100644 --- a/Kiwix-iOS/Controller/Main/JSInjection.swift +++ b/Kiwix-iOS/Controller/Main/JSInjection.swift @@ -10,19 +10,12 @@ import UIKit import JavaScriptCore class JSInjection { - - class func injectTableWrappingJavaScriptIfNeeded(webView: UIWebView, traitCollection: UITraitCollection) { - if Preference.webViewInjectJavascriptToAdjustPageLayout { - if traitCollection.horizontalSizeClass == .Compact { - guard let path = NSBundle.mainBundle().pathForResource("adjustlayoutiPhone", ofType: "js") else {return} - guard let jString = try? String(contentsOfFile: path) else {return} - webView.stringByEvaluatingJavaScriptFromString(jString) - } else { - guard let path = NSBundle.mainBundle().pathForResource("adjustlayoutiPad", ofType: "js") else {return} - guard let jString = try? String(contentsOfFile: path) else {return} - webView.stringByEvaluatingJavaScriptFromString(jString) - } - } + + class func inject(webView: UIWebView) { + let context = webView.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as? JSContext + let path = NSBundle.mainBundle().pathForResource("injection", ofType: "js") + let jString = try? String(contentsOfFile: path!) + context?.evaluateScript(jString!) } class func adjustFontSizeIfNeeded(webView: UIWebView) { @@ -31,14 +24,13 @@ class JSInjection { webView.stringByEvaluatingJavaScriptFromString(jString) } - class func getTitle(from webView: UIWebView) -> String? { + class func getTitle(from webView: UIWebView) -> String? { return webView.stringByEvaluatingJavaScriptFromString("document.title") } class func getTableOfContents(webView: UIWebView) -> [HTMLHeading] { + let jString = "(new TableOfContents()).headerObjects;" guard let context = webView.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as? JSContext, - let path = NSBundle.mainBundle().pathForResource("getTableOfContents", ofType: "js"), - let jString = try? String(contentsOfFile: path), let elements = context.evaluateScript(jString).toArray() as? [[String: String]] else {return [HTMLHeading]()} var headings = [HTMLHeading]() for element in elements { diff --git a/Kiwix-iOS/Controller/Main/MainControllerDelegates.swift b/Kiwix-iOS/Controller/Main/MainControllerDelegates.swift index 47ddd523..27997dd9 100644 --- a/Kiwix-iOS/Controller/Main/MainControllerDelegates.swift +++ b/Kiwix-iOS/Controller/Main/MainControllerDelegates.swift @@ -52,6 +52,7 @@ extension MainController: UIWebViewDelegate, SFSafariViewControllerDelegate, LPT configureNavigationButtonTint() configureTableOfContents() JSInjection.adjustFontSizeIfNeeded(webView) + JSInjection.inject(webView) } func webView(webView: UIWebView, didFailLoadWithError error: NSError) { diff --git a/Kiwix-iOS/Controller/Others/TableOfContentsController.swift b/Kiwix-iOS/Controller/Others/TableOfContentsController.swift index 7a4bd663..fe0e9ec5 100644 --- a/Kiwix-iOS/Controller/Others/TableOfContentsController.swift +++ b/Kiwix-iOS/Controller/Others/TableOfContentsController.swift @@ -71,6 +71,7 @@ class TableOfContentsController: UIViewController, UITableViewDelegate, UITableV func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { delegate?.scrollTo(headings[indexPath.row]) + tableView.deselectRowAtIndexPath(indexPath, animated: true) } // MARK: - Empty table datasource & delegate diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 9141232e..9c5d1bd2 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.1780 + 1.8.1783 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOS/Storyboard/Main.storyboard b/Kiwix-iOS/Storyboard/Main.storyboard index 7dfb2735..31c152ac 100644 --- a/Kiwix-iOS/Storyboard/Main.storyboard +++ b/Kiwix-iOS/Storyboard/Main.storyboard @@ -69,7 +69,7 @@ - +