【问题标题】:How to create a JSoup Document including JS?如何创建包含 JS 的 JSoup 文档?
【发布时间】:2015-10-25 17:12:53
【问题描述】:

我创建了一个 JSoup 文档,其中包含两个 <script type="application/javascript">/* .. */</script> 元素。

问题:当我调用 .html().toString() 时,JSoup 会转义我的 JavaScript。

if (foo && bar) 

得到

if (foo && bar)

是否可以将 JSoup 配置为在转义时忽略 <script> 元素??


这(基本上)是我创建 jsoup 文档的方式。

final Document doc = Document.createShell("");
final Element head = doc.head();
head.appendElement("meta").attr("charset", "utf-8");
final String myJS = ...;
head.appendElement("script").attr("type", "application/javascript").text(myJS);

我目前的解决方法是在.html() 上用String.replace 替换占位符。但这有点 hacky。

head.appendElement("script").attr("type", "application/javascript").text("$MYJS$");
String s = doc.html();
s = s.replace("$MYJS$", myJS);

【问题讨论】:

  • 我已经更新了我的答案。

标签: javascript java jsoup


【解决方案1】:

outerHtmlHead 方法不能再被覆盖。

这是我用的:

head
    .appendElement("script")
    .attr("type","text/javascript")
    .appendChild(new DataNode(getJavaScript(),""));

【讨论】:

  • 我希望 .text() 能够工作。我觉得这是一个错误。 .text() 不应该做 html 编码。
猜你喜欢
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 2016-06-28
  • 2018-09-03
  • 2017-02-07
  • 2016-03-16
  • 1970-01-01
  • 2015-01-21
相关资源
最近更新 更多