update JS

This commit is contained in:
Chris Li 2016-10-17 16:34:27 -04:00
parent 06083263e5
commit 9bd8c3e462
11 changed files with 49 additions and 109 deletions

View File

@ -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 {

View File

@ -52,6 +52,7 @@ extension MainController: UIWebViewDelegate, SFSafariViewControllerDelegate, LPT
configureNavigationButtonTint()
configureTableOfContents()
JSInjection.adjustFontSizeIfNeeded(webView)
JSInjection.inject(webView)
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError) {

View File

@ -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

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.8.1780</string>
<string>1.8.1783</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View File

@ -69,7 +69,7 @@
<rect key="frame" x="0.0" y="116" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="cam-ik-ZIw" id="ooG-35-3Wp">
<frame key="frameInset" width="414" height="43.5"/>
<frame key="frameInset" width="414" height="43.666666666666664"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="ry7-2s-QyS">

View File

@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.8.1780</string>
<string>1.8.1783</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>

View File

@ -50,10 +50,8 @@
973DD4191D343F2F009D45DB /* libzim.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 973DD40E1D343F2F009D45DB /* libzim.a */; };
973DD4281D36E3E4009D45DB /* SettingDetailController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 973DD4271D36E3E4009D45DB /* SettingDetailController.swift */; };
974000151DB008C6009A740D /* MainInterface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9788419C1DA2FF2A00D22D3C /* MainInterface.storyboard */; };
974C49631DA307FF00E276E1 /* adjustlayoutiPad.js in Resources */ = {isa = PBXBuildFile; fileRef = 974C495F1DA307FF00E276E1 /* adjustlayoutiPad.js */; };
974C49641DA307FF00E276E1 /* adjustlayoutiPhone.js in Resources */ = {isa = PBXBuildFile; fileRef = 974C49601DA307FF00E276E1 /* adjustlayoutiPhone.js */; };
974C49651DA307FF00E276E1 /* getSnippet.js in Resources */ = {isa = PBXBuildFile; fileRef = 974C49611DA307FF00E276E1 /* getSnippet.js */; };
974C49661DA307FF00E276E1 /* getTableOfContents.js in Resources */ = {isa = PBXBuildFile; fileRef = 974C49621DA307FF00E276E1 /* getTableOfContents.js */; };
974C49661DA307FF00E276E1 /* injection.js in Resources */ = {isa = PBXBuildFile; fileRef = 974C49621DA307FF00E276E1 /* injection.js */; };
974C49681DA4266200E276E1 /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 974C49671DA4266200E276E1 /* CloudKit.framework */; };
974F33C61D99CAD700879D35 /* BookmarkOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 974F33C51D99CAD700879D35 /* BookmarkOperation.swift */; };
975227CD1D0227E8001D1DDE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 975227CA1D0227E8001D1DDE /* Main.storyboard */; };
@ -219,10 +217,8 @@
973DD40D1D343F2F009D45DB /* libxapian.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libxapian.a; path = Kiwix/libkiwix/iOS/libxapian.a; sourceTree = "<group>"; };
973DD40E1D343F2F009D45DB /* libzim.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libzim.a; path = Kiwix/libkiwix/iOS/libzim.a; sourceTree = "<group>"; };
973DD4271D36E3E4009D45DB /* SettingDetailController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SettingDetailController.swift; path = "Kiwix-iOS/Controller/Setting/SettingDetailController.swift"; sourceTree = SOURCE_ROOT; };
974C495F1DA307FF00E276E1 /* adjustlayoutiPad.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = adjustlayoutiPad.js; sourceTree = "<group>"; };
974C49601DA307FF00E276E1 /* adjustlayoutiPhone.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = adjustlayoutiPhone.js; sourceTree = "<group>"; };
974C49611DA307FF00E276E1 /* getSnippet.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = getSnippet.js; sourceTree = "<group>"; };
974C49621DA307FF00E276E1 /* getTableOfContents.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = getTableOfContents.js; sourceTree = "<group>"; };
974C49621DA307FF00E276E1 /* injection.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = injection.js; sourceTree = "<group>"; };
974C49671DA4266200E276E1 /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = System/Library/Frameworks/CloudKit.framework; sourceTree = SDKROOT; };
974F33C51D99CAD700879D35 /* BookmarkOperation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BookmarkOperation.swift; sourceTree = "<group>"; };
975227CA1D0227E8001D1DDE /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = Main.storyboard; path = "Kiwix-iOS/Storyboard/Main.storyboard"; sourceTree = SOURCE_ROOT; };
@ -560,10 +556,8 @@
974C495E1DA307FF00E276E1 /* JavaScripts */ = {
isa = PBXGroup;
children = (
974C495F1DA307FF00E276E1 /* adjustlayoutiPad.js */,
974C49601DA307FF00E276E1 /* adjustlayoutiPhone.js */,
974C49611DA307FF00E276E1 /* getSnippet.js */,
974C49621DA307FF00E276E1 /* getTableOfContents.js */,
974C49621DA307FF00E276E1 /* injection.js */,
);
path = JavaScripts;
sourceTree = "<group>";
@ -994,14 +988,12 @@
files = (
971A10811D022F74007FC62C /* Pic_P.png in Resources */,
975227D01D022814001D1DDE /* LaunchScreen.storyboard in Resources */,
974C49631DA307FF00E276E1 /* adjustlayoutiPad.js in Resources */,
97F03CE21D2440470040D26E /* Search.storyboard in Resources */,
9734E54E1D289D060061C39B /* Welcome.storyboard in Resources */,
971A10161D022872007FC62C /* Assets.xcassets in Resources */,
97E850CB1D2DA5B300A9F688 /* About.html in Resources */,
97C601DC1D7F15C400362D4F /* Bookmark.storyboard in Resources */,
974C49641DA307FF00E276E1 /* adjustlayoutiPhone.js in Resources */,
974C49661DA307FF00E276E1 /* getTableOfContents.js in Resources */,
974C49661DA307FF00E276E1 /* injection.js in Resources */,
975227CD1D0227E8001D1DDE /* Main.storyboard in Resources */,
975227CE1D0227E8001D1DDE /* Setting.storyboard in Resources */,
971A10801D022F74007FC62C /* Pic_I.png in Resources */,

View File

@ -1,15 +0,0 @@
/**
* Created by chrisli on 10/5/15.
*/
//var tables = document.getElementsByTagName("table");
//var i;
//for (i = 0; i < tables.length; i++) {
// if (tables[i].className.indexOf("infobox") == -1) {
// org_html = tables[i].outerHTML;
// new_html = "<div id='slidesInner' style ='width:100%;'>" + org_html + "</div>";
// tables[i].outerHTML = new_html;
// tables[i].parentNode.style.overflow = "auto";
// tables[i].style.margin = "auto";
// tables[i].style.width = "100%";
// }
//}

View File

@ -1,39 +0,0 @@
/**
* Created by chrisli on 10/5/15.
*/
function adjustTables() {
var tables = document.getElementsByTagName("table");
var i;
for (i = 0; i < tables.length; i++) {
org_html = tables[i].outerHTML;
new_html = "<div id='slidesInner' style ='width:100%;'>" + org_html + "</div>";
tables[i].outerHTML = new_html;
tables[i].parentNode.style.overflow = "auto";
tables[i].style.margin = "auto";
tables[i].style.width = "100%";
}
}
function adjustThumbLayout(thumbs) {
var i;
for (i = 0; i < thumbs.length; i++) {
thumbs[i].style.width = "100%";
thumbs[i].style.paddingLeft = "4px";
thumbs[i].style.paddingRight = "4px";
var inner = thumbs[i].getElementsByClassName("thumbinner")[0];
if (inner) {
inner.style.width = "100%";
var captions = inner.getElementsByClassName("thumbcaption");
var j;
for (j= 0; j < captions.length; j++) {
captions[j].style.textAlign = "center";
}
}
}
}
adjustTables();
//adjustThumbLayout(document.getElementsByClassName("thumb tright"));
//adjustThumbLayout(document.getElementsByClassName("thumb tleft"));
//adjustThumbLayout(document.getElementsByClassName("thumb tmulti tright"));
//adjustThumbLayout(document.getElementsByClassName("thumb tmulti tleft"));

View File

@ -1,24 +0,0 @@
function getTableOfContents() {
var h1 = document.getElementsByTagName('h1')[0];
var headers = [elementToObject(h1)];
var content = document.getElementById("mw-content-text");
var headerTags = ['h2', 'h3', 'h4'].map(function(x){ return x.toUpperCase() });
for (var i = 0; i < content.children.length; i++) {
var element = content.children[i];
if (headerTags.includes(element.tagName)) {
headers.push(elementToObject(element));
}
}
return headers.length > 1 ? headers : undefined;
}
function elementToObject(element) {
var obj = {};
obj.id = element.id;
obj.textContent = element.textContent;
obj.tagName = element.tagName;
return obj;
}
getTableOfContents();

View File

@ -0,0 +1,32 @@
function TableOfContents () {
this.getHeaderElements = function () {
var h1 = document.getElementsByTagName('h1')[0];
var elememts = [h1];
var content = document.getElementById("mw-content-text");
var headerTags = ['h2', 'h3', 'h4'].map(function(x){ return x.toUpperCase() });
for (var i = 0; i < content.children.length; i++) {
var element = content.children[i];
if (headerTags.includes(element.tagName)) {
elememts.push(element);
}
}
return elememts;
}
this.headerElements = this.getHeaderElements();
this.getHeaderObjects = function () {
return this.getHeaderElements().map( elementToObject );
}
this.headerObjects = this.getHeaderObjects();
function elementToObject(element) {
var obj = {};
obj.id = element.id;
obj.textContent = element.textContent;
obj.tagName = element.tagName;
return obj;
}
}