在 Android 应用程序中,我试图通过 WebView 显示 EPUB 书籍。为了浏览这本书,我使用EPUBJS和Pure Drawer来获得一些视觉效果。当我在使用 Android KitKat(4.4) 或更高版本的设备中运行我的应用程序时,这可以正常工作,但是如果我尝试使用 Jelly Beans(4.3) 等以下版本。的某些功能Epub.js
不起作用,例如Book.prev
, Book.next
,Book.goto(url)
而Book.renderTo()
其他功能像Book.getToc()
andBook.getMetadata()
仍然可以正常工作,并且我可以获得所有书籍章节。
我将所有代码放在一个javascript文件中,如下所示
function init_js_code(url){
var Book = ePub(url, { restore: true});
Book.getMetadata().then(function(meta){
document.title = meta.bookTitle;
});
$( "#next" ).click(function() {
Book.nextPage();
});
$( "#prev" ).click(function() {
Book.prevPage();
});
Book.getToc().then(function(toc){
var $select = document.getElementById("toc"),
$docfrag = document.createDocumentFragment();
var cList = $('ul.nav-primary');
toc.forEach(function(chapter) {
// logic to create a menu
});
});
Book.ready.all.then(function(){
document.getElementById("loader").style.display = "none";
});
Book.renderTo("area");
在 html 文件中只调用了这个函数
<head>
<script src="jquery.js"></script>
...
</head>
<body>
...
<script>
function getPDFURL(){
...
}
var url = 'http://' + getPDFURL() + '.epub';
init_js_code(url);
</script>
</body>
html代码以这种方式呈现在活动中
final WebView webView = (WebView) findViewById(R.id.webView1);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
settings.setAllowUniversalAccessFromFileURLs(true);
}
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB) {
settings.setDisplayZoomControls(false);
}
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
..
}
});
webView.loadUrl("file:///android_asset/.../index.html?url="+material_url);
尽管问题出在什么地方,但某些 Javascript 功能在此设备中被阻止了?还有其他方法吗?