[英]Can a Windows Gadget save data?
我正在创建一个小的Windows边栏小工具,用于在简单的textarea
做笔记。
和往常一样,我有一个gadget.xml
清单文件和一个.html
文件,请参见下文。
如何在Gadget中读取一些数据/保存一些数据?
我知道通常仅使用JavaScript不可能localstorage
这一点( 注意:因为我要持久化数据 ,所以无法使用localstorage
),那么如何在Windows Gadget
保存/读取数据?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Unicode" />
<title>NeverForget</title>
<style type="text/css">
body
{
margin: 0;
width: 300px;
height: 200px;
background-color: transparent;
}
#gadgetContent
{
width: 100%;
height: 100%;
overflow: hidden;
border: none;
background-color: transparent;
}
</style>
<script type="text/jscript" language="jscript">
function init() {
// how to load notes from a file here on startup?
}
window.onkeydown = function () {
// how to save data to file?
}
</script>
</head>
<body onload="init()">
<textarea id="gadgetContent">Bonjour</textarea>
</body>
</html>
尝试以下方法之一:
有System.Gadget.Settings对象的内置方法,可用于从Settings.ini文件(存储在C:\\ Users \\ [user] \\ AppData \\ Local \\ Microsoft \\ Windows Sidebar中)读取/写入,但是如果小工具关闭或卸载,此信息将丢失。
使用FileSystemObject在任何地方创建或读取或写入文件夹/文件。 限制:文件只能保存为unicode或ascii。
使用ADO Stream对象可以在任何地方创建或读取或写入文件。 限制:无法创建文件夹-必须与FileSystemObject结合使用。 优点:可以使用计算机上存在的任何代码页。
由于我喜欢用utf-8保存文本文件,因此下面的示例使用第三种方法,但是您可以决定放弃一些错误处理。 (注意-此示例基于Andrew Urquhart发布的脚本 )
幸运的是,侧边栏能够使用已知的文件夹和已知的文件夹路径,因此例如查找文档文件夹的路径就像
var docs = System.Shell.knownFolderPath("Documents");
请记住,反斜杠是javascript中的转义字符,因此字符串中的路径必须将其反斜杠加倍。
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>NeverForget</title> <style type="text/css"> <!-- body {margin:0;width:300px;height:200px;background-color:transparent;padding:10px;} #gadgetContent {width:280px;height:144px;overflow:auto;border:1px solid black;background-color:#eee;} button {margin-left:66px;margin-top:10px;} #message {display:none;width:280px;height:180px;position:absolute;top:10px;left:10px;background-color:#eee;border:1px solid red;} #messageContent {width:278px;height:144px;word-wrap:break-word;overflow-y:auto;} #newButton {position:absolute;bottom:10px;left:54px;} --> </style> <script type="text/jscript"> //globals var myFolderPath=System.Shell.knownFolderPath("Documents")+"\\\\myFiles"; //end globals function showMessage(msg){ message.style.display="block"; messageContent.innerText=msg; } function closeMessage(){ message.style.display="none"; messageContent.innerText=""; } function loadFile(strAbsoluteFilePath, strCharSet){ var adReadAll=-1, adReadLine=-2, strFileContents="", objStream=new ActiveXObject("ADODB.Stream"), fso=new ActiveXObject("Scripting.FileSystemObject"); try{ if(!strAbsoluteFilePath){ throw new Error(1, "Required parameter \\"strAbsoluteFilePath\\" was not defined"); } if(!strCharSet){ throw new Error(2, "Required parameter \\"strCharSet\\" was not defined"); } if(!fso.FolderExists(myFolderPath)){ throw new Error(3, "Folder \\""+myFolderPath+"\\" does not exist"); } objStream.Open(); try{ objStream.CharSet=strCharSet; objStream.LoadFromFile(strAbsoluteFilePath); strFileContents=objStream.ReadText(adReadAll); gadgetContent.innerText=strFileContents; } catch(err){ throw new Error(err.number, "Loading failed:\\r\\n" + err.description); } finally{ objStream.Close(); // Always close the stream regardless of what happens objStream=null; fso=null; } } catch(err){ showMessage("Function loadFile() failed with parameters strAbsoluteFilePath=\\"" + strAbsoluteFilePath + "\\", strCharSet=\\"" + strCharSet + "\\". Message=\\r\\n" + err.description+"\\r\\nError Number: "+err.number); } } function saveFile(strAbsoluteFilePath, strCharSet, strFileContents, blnOverwrite){ var adSaveCreateNotExist=1, adSaveCreateOverWrite=2, objStream = new ActiveXObject("ADODB.Stream"), fso=new ActiveXObject("Scripting.FileSystemObject"); try{ if(!strAbsoluteFilePath){ throw new Error(1, "Required parameter \\"strAbsoluteFilePath\\" was not defined"); } if(!strCharSet){ throw new Error(2, "Required parameter \\"strCharSet\\" was not defined"); } if(typeof strFileContents != "string"){ throw new Error(3, "Required parameter \\"strFileContents\\" was not a string"); } if(!fso.FolderExists(myFolderPath)){ fso.CreateFolder(myFolderPath); } objStream.Open(); try{ objStream.CharSet=strCharSet; objStream.WriteText(strFileContents); objStream.SaveToFile(strAbsoluteFilePath, (blnOverwrite ? adSaveCreateOverWrite : adSaveCreateNotExist)); return true; } catch(err){ throw new Error(err.number, "SaveToFile failed:\\r\\n" + err.description); } finally{ objStream.Close(); // Always close the stream regardless of what happens objStream=null; fso=null; } return false; } catch(err){ showMessage("Function saveFile() failed with parameters strAbsoluteFilePath=\\"" + strAbsoluteFilePath + "\\", strCharSet=\\"" + strCharSet + "\\", strFileContents=\\"" + strFileContents + "\\", blnOverwrite=\\"" + blnOverwrite + "\\". Message=\\r\\n" + err.description+"\\r\\nError Number: "+err.number); } } function init(){ loadButton.onclick=function(){loadFile(myFolderPath+"\\\\myFile.txt","utf-8");}; saveButton.onclick=function(){saveFile(myFolderPath+"\\\\myFile.txt", "utf-8", gadgetContent.innerText, true);}; closeButton.onclick=closeMessage; } </script> </head> <body onload="init()"> <textarea id="gadgetContent">Bonjour</textarea> <div id="message"> <div id="messageContent"></div> <div id="newButton"><button id="closeButton">Close</button></div> </div> <button id="loadButton">Load</button><button id="saveButton">Save</button> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.