javascript - 如何在 ios uiwebview 中为我的电子书阅读器使用 xml 中的 JavaScript?

标签 javascript html css xml uiwebview

我可以使用这段代码在 HTML 中加载 JavaScripts 标签:

function loadjscssfile(filename, filetype){

if (filetype=="js"){ //if filename is a external JavaScript file
var fileref = document.createElement('script');
fileref.src = filename;
fileref.async = false;
}
else if (filetype=="css"){ //if filename is an external CSS file
    var fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined"){
    document.head.appendChild(fileref);
      }
}
loadjscssfile("jquery.min.js", "js") //dynamically load and add this .js file
loadjscssfile("annotator.min.css", "css") ////dynamically load and add this .css file

但我想在 epub 阅读器中使用它,它分解为 xml/html。我希望在 ios uiwebview 中打开我的电子书阅读器时执行相同的 js。

示例 xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
   <head>
      <title>CHAPTER 13 - DR. SEWARD'S DIARY&#8212;cont. | Dracula</title>
      <link rel="stylesheet" href="css/book.css" type="text/css"/>
      <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
      <meta name="EPB-UUID" content="01D8803E-6BF7-1014-BF2E-F46A8E11922F"/>
   </head>
   <body>
      <div class="body">
         <div class="chapter">
            <h3 class="chapter-title">CHAPTER 1</h3>
            <h4 class="chapter-subtitle">DR. DIARY&#8212;cont.</h4>
            <p>"She makes a very beautiful corpse, sir. It's quite a privilege to attend on her. It's not too much to say that she will do credit to our establishment!"</p>
        </div>
      </div>
   </body>
</html>

最佳答案

你只需要为JS和CSS添加这两个函数:

    - (void) addJSScripts
{
    NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"jquery.min" ofType:@"js"];
    NSURL *jsURL = [NSURL fileURLWithPath:jsFilePath];
    NSString *javascriptCode1 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];

jsFilePath = [[NSBundle mainBundle] pathForResource:@"annotator.offline.min" ofType:@"js"];
jsURL = [NSURL fileURLWithPath:jsFilePath];
NSString *javascriptCode2 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];

jsFilePath = [[NSBundle mainBundle] pathForResource:@"offlinejs" ofType:@"js"];
jsURL = [NSURL fileURLWithPath:jsFilePath];
NSString *javascriptCode3 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];

jsFilePath = [[NSBundle mainBundle] pathForResource:@"startjs" ofType:@"js"];
jsURL = [NSURL fileURLWithPath:jsFilePath];
NSString *javascriptCode4 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];

[_webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@\n%@\n%@\n%@",javascriptCode1, javascriptCode2, javascriptCode3, javascriptCode4]];

- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *path = [[NSBundle mainBundle] pathForResource:@"annotator.min" ofType:@"css"];
NSString *javascriptString = @"var link = document.createElement('link'); link.href = '%@'; link.rel = 'stylesheet'; document.head.appendChild(link)";
NSString *javascriptWithPathString = [NSString stringWithFormat:javascriptString, path];
[_webview stringByEvaluatingJavaScriptFromString:javascriptWithPathString];

path = [[NSBundle mainBundle] pathForResource:@"annotator.touch" ofType:@"css"];
javascriptString = @"var link = document.createElement('link'); link.href = '%@'; link.rel = 'stylesheet'; document.head.appendChild(link)";
javascriptWithPathString = [NSString stringWithFormat:javascriptString, path];
    [_webview stringByEvaluatingJavaScriptFromString:javascriptWithPathString];
[self addJSScripts];

关于javascript - 如何在 ios uiwebview 中为我的电子书阅读器使用 xml 中的 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586069/

相关文章:

javascript - TinyMCE 4 自定义插件 - 从 iframe 获取值(value)

javascript - iOS 键盘不关闭,由 iframe 中的输入触发

jquery - 如何使用 Bootstrap 创建无边框表格

javascript - Crossfilter reduce 函数

javascript - 封装在 Javascript 中

html - 使滚动粘性元素的优先级高于更改元素位置

html - 如何在带阴影的表格上制作三 Angular 形?

javascript - react : How to use arrow up/down to select previous/next item in a scroll list and make the list scroll to keep the selected element on screen

ios - 仅在 iOs 上,嵌套的固定元素不能大于父元素

javascript - 如何为 Selenium IDE 选择用 CSS 编码的下拉菜单中的元素?