2

动机:https ://stackoverflow.com/questions/28120689/create-self-modifying-html-page-on-box

Bug: 字符串转义、格式化htmljs生成初始编辑、保存htmljs

例如,

a) 如果在本地浏览器打开“saveFile.html”;

b) 输入“abc” textarea

c) 点击save file按钮;

d) 点击对话框SaveSave File

e)file-*[date according to universal time].html保存到磁盘;

f)file-*[date according to universal time].html在浏览器中打开;

g) 输入“def” textarea

h) 重复 d)、e)、f);

i) 错误:第二个结果file-*[date according to universal time].html确实显示textarea包含“abc def”文本内容; button 显示在html

// at rendered `html` from second `file-*[date according to universal time].html`
// `textarea` containing "abc def" displayed here , 
// `button` _not_ displayed ; following string displayed following `textarea`:
');"console.log(clone);var file = new Blob([clone], {'type':'text/html'});a.href = URL.createObjectURL(file);a.download = 'file-' + new Date().getTime() + '.html';a.click();};

在第26行生成,“saveFile.html”

+ "var clone = '<!doctype html>'+ document.documentElement.outerHTML.replace(/<textarea>.*<.+textarea>/, '<textarea>'+document.getElementsByTagName('textarea')[0].value+'<\/textarea>');"

“保存文件.html”v 1.0.0

html, js

<!doctype html>
<html>
<!-- saveFile.html 1.0.0 2015 guest271314 edit, save `html` document -->
<head>
</head>
<body>
<textarea>
</textarea>
<button>save file</button>
<script type="text/javascript">
var saveFile = document.getElementsByTagName("button")[0];
var input = document.getElementsByTagName("textarea")[0];
var a = document.createElement("a");

saveFile.onclick = function(e) {

  var clone = ["<!doctype html><head></head><body><textarea>"
              + input.value
              + "</textarea>"
              + "<button>save file</button>"
              + "<script type='text/javascript'>"
              + "var saveFile = document.getElementsByTagName('button')[0];"
              + "var input = document.getElementsByTagName('textarea')[0];"
              + "var a = document.createElement('a');"
              + "saveFile.onclick = function(e) {"
              + "var clone = '<!doctype html>'+ document.documentElement.outerHTML.replace(/<textarea>.*<.+textarea>/, '<textarea>'+document.getElementsByTagName('textarea')[0].value+'<\/textarea>');"
              + "console.log(clone);"
              + "var file = new Blob([clone], {'type':'text/html'});"
              + "a.href = URL.createObjectURL(file);"
              + "a.download = 'file-' + new Date().getTime() + '.html';"
              + "a.click();"
              + "};"
              + "</scr"+"ipt>"
              + "</body>"
              + "</html>"];

  var file = new Blob([clone], {"type":"text/html"});  
  a.href = URL.createObjectURL(file);
  a.download = "file-" + new Date().getTime() + ".html";
  a.click();  

};
</script>
</body>
</html>
4

2 回答 2

1

您的替换功能将替换,直到/textarea>您的clone变量中。它不会从第一个文件中执行此操作,因为 html 中的 textarea 之后有一个换行符。解决它的一种方法是newline在生成的 html 中添加一个字符。像这样:

var clone = ["<!doctype html><head></head><body><textarea>"
          + input.value
         // add newline here
          + "</textarea>\n"
          + "<button>save file</button>"
          + "<script type='text/javascript'>"
          + "var saveFile = document.getElementsByTagName('button')[0];"
          + "var input = document.getElementsByTagName('textarea')[0];"
          + "var a = document.createElement('a');"
          + "saveFile.onclick = function(e) {"
          + "var clone = '<!doctype html>'+ document.documentElement.outerHTML.replace(/<textarea>.*<.+textarea>/, '<textarea>'+document.getElementsByTagName('textarea')[0].value+'<\/textarea>');"
          + "console.log(clone);"
          + "var file = new Blob([clone], {'type':'text/html'});"
          + "a.href = URL.createObjectURL(file);"
          + "a.download = 'file-' + new Date().getTime() + '.html';"
          + "a.click();"
          + "};"
          + "</scr"+"ipt>"
          + "</body>"
          + "</html>"];
于 2015-06-01T03:11:09.303 回答
1

我不确定是什么破坏了第三代克隆,从而导致将 js 信息输出到页面,但最好使用实际的文档对象来克隆/操作原始文件并将其内容输出为对象的字符串Blob。例如,我使用您的基本 saveFile.html 进行了测试,并进行了以下更改:

//remove original clone var and replace with:
var clone = document.cloneNode(true);

// grab textarea elements from both original document and clone:
var doc_input = document.getElementsByTagName("textarea")[0];
var clone_input = clone.getElementsByTagName("textarea")[0];

// set clone textarea's innerHTML to current textarea value:
clone_input.innerHTML = doc_input.value;

// use outerHTML of clone.documentElement to get string for Blob
var clone_string = [clone.documentElement.outerHTML];
var file = new Blob([clone_string], {"type":"text/html"});

我看到的唯一缺点是:

  1. 这可能很难扩展到更通用的框架来生成加载 HTML 页面当前状态的“实时 HTML 文件”(尽管它不应该比您的示例方法更复杂)。

  2. 返回的字符串clone.documentElement.outerHTML似乎将文档类型声明放到一个简单的元素中,这样:

不在输出字符串中。你可能会使用类似的东西:

var clone_string = ["<!doctype html>" + clone.documentElement.outerHTML];

作为一种解决方法。或者,对于更强大的东西:

var doc_doctype = new XMLSerializer().serializeToString(document.doctype);

var clone_string = [doc_doctype + clone.documentElement.outerHTML];
于 2015-06-01T04:16:29.143 回答