java - Jsoup 将我的 javascript 字符串转换为一行

标签 java javascript android jsoup

我在使用 Jsoup 中的 .append 函数时遇到问题。我将简单的 Javascript 文件响应字符串附加到 Jsoup 元素。但在附加后,我的回复变成了一行,这对我造成了很大的伤害。

我的字符串是这样的

(function () {

var count = 0;
var root = this;
var require = root.require;
//var require = cordova.require;
require.config({

    config: {
        text: { //this hacks the text api to allow cross domain loading of the templates. it is needed only for the packaged applications
            useXhr: function (url, protocol, hostname, port) {
                //console.log("text.useXhr request came from : " + url + ", " + protocol + " and " + hostname);
                return true;
                //return true if you want to allow this url, given that the 
                //text plugin thinks the request is coming from protocol, hostname, port. 
            }
        },
        'is': {
            isBundled: true
        }
    },
    waitSeconds: 45,
    baseUrl: 'scripts/app',
    deps: ["app"],

但是附加到元素后,它会变成

(function () { var count = 0; var root = this; var require = root.require; //var require = cordova.require; require.config({ config: { text: { //this hacks the text api to allow cross domain loading of the templates. it is needed only for the packaged applications useXhr: function (url, protocol, hostname, port) { //console.log("text.useXhr request came from : " + 

我的代码就是用于此

String temp=script.attr("src");
                    temp=temp.replace("/"+Heirarchy, DomainName+"/"+Heirarchy.toLowerCase());
                    script.attr("src", temp);
                    script.removeAttr("data-main");
                    script.removeAttr("async");
                    String innerHtml="</script>\n<script>\n"+old_configString;
                    script.append(innerHtml);

old_configString 是我的 Javascript 响应字符串....

最佳答案

您应该使用 DataNode用于添加脚本或样式。

script.after(new DataNode("<script>" + old_configString + "</script>", "http://domain.tld/path"));

关于java - Jsoup 将我的 javascript 字符串转换为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23925988/

相关文章:

android - 如何将首选项与列表绑定(bind)

java - 将 IST/IST 时间转换为 UTC 时间

Linux 中的 Java 进程状态

java - 插入 map 时按值保留顺序

android - onChildClickListener 在 ExapandableListView 中没有响应

android - 隔板和容器之间的空间 flutter

java - 减少 ITextPDF 中的图像大小(字节)

javascript - 事件发生后按顺序解决 promise

javascript - React `componentDidCatch` 未被调用但需要处理错误?

javascript - 为什么我在 Node 的 HTTP 服务器上收到这么多连接事件?