用JavaScript将搜索关键字保存到文本文件

用JavaScript将搜索关键字保存到文本文件,javascript,Javascript,我正在定制chrome extension(Google Dictionary)的功能,我想通过按下按钮将我查找到的每个单词保存到文本文件中。下面的代码是将用户键入的每个单词保存在一个单独的文件中,并询问保存文件的位置。但是我想把每个单词都附加到一个永久的,比如日志文件中。在PHP中这将是非常容易的,但是在javascript中这是有问题的,因为客户端脚本功能。我找到了一个解决方案,但这真的很复杂,我不知道如何实现它。如有任何其他解决方案/建议,将不胜感激 <!DOCTYPE html&g

我正在定制chrome extension(Google Dictionary)的功能,我想通过按下按钮将我查找到的每个单词保存到文本文件中。下面的代码是将用户键入的每个单词保存在一个单独的文件中,并询问保存文件的位置。但是我想把每个单词都附加到一个永久的,比如日志文件中。在PHP中这将是非常容易的,但是在javascript中这是有问题的,因为客户端脚本功能。我找到了一个解决方案,但这真的很复杂,我不知道如何实现它。如有任何其他解决方案/建议,将不胜感激

<!DOCTYPE html>
<html>
  <input type="text" id="inputTextToSave"><button id="define-btn" class="btn btn-primary" value="Define" onclick="saveTextAsFile()">Define</button>
<script type='text/javascript'>
 function saveTextAsFile()
    {
var textToWrite = document.getElementById("inputTextToSave").value;
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});

var downloadLink = document.createElement("a");
downloadLink.download = "log.txt";
downloadLink.innerHTML = "Download File";

downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
downloadLink.click();
    }
 </script>
 </html>

定义
函数saveTextAsFile()
{
var textToWrite=document.getElementById(“InputExtToSave”).value;
var textFileAsBlob=newblob([textToWrite],{type:'text/plain'});
var downloadLink=document.createElement(“a”);
downloadLink.download=“log.txt”;
downloadLink.innerHTML=“下载文件”;
downloadLink.href=window.webkitURL.createObjectURL(textFileAsBlob);
downloadLink.click();
}

这里有一个建议:与其这样做,不如将其保存在本地存储中(或根据文件大小而定的存储中)


然后在扩展名的背景页中,提供一个选项,使用常规文件对话框将这些单词作为文件下载。

您可以使用。事实上,它适用于所有主流浏览器。以防你想把你的应用程序扩展到其他浏览器。然后可以将读取的数据从存储器写入文件

如果有人对脚本解决方案感兴趣:

<!DOCTYPE html>
<html>
  <input type="text" id="inputTextToSave"><button id="define-btn" class="btn btn-primary" value="Define" onclick="saveTextAsFile()">Define</button>
<script type='text/javascript'>

function saveTextAsFile()
{
    var textToWrite = document.getElementById("inputTextToSave").value;
    var textToWriteOld = localStorage.getItem("inputTextStorage");
    if(textToWriteOld === null) textToWriteOld = "";
    localStorage.setItem("inputTextStorage", textToWriteOld + "\n" + textToWrite);
    var textFileAsBlob = new Blob([localStorage.getItem("inputTextStorage")], {type:'text/plain'});


    var downloadLink = document.createElement("a");
    downloadLink.download = "log.txt";
    downloadLink.innerHTML = "Download File";
    downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    downloadLink.click();
}
</script>
</html>

定义
函数saveTextAsFile()
{
var textToWrite=document.getElementById(“InputExtToSave”).value;
var textToWriteOld=localStorage.getItem(“inputTextStorage”);
如果(textToWriteOld==null)textToWriteOld=“”;
setItem(“inputTextStorage”,textToWriteOld+“\n”+textToWrite);
var textFileAsBlob=newblob([localStorage.getItem(“inputTextStorage”)],{type:'text/plain'});
var downloadLink=document.createElement(“a”);
downloadLink.download=“log.txt”;
downloadLink.innerHTML=“下载文件”;
downloadLink.href=window.webkitURL.createObjectURL(textFileAsBlob);
downloadLink.click();
}

任何其他改进都将不胜感激。

但是,在我关闭浏览器或关闭计算机后,是否可以使用localStorage检索以前的数据?当然!!它之所以被称为存储,是因为它是持久存储—数据进入磁盘。