2015-01-05 83 views
0

我正在創建一個小Windows Sidebar小工具,用於在簡單的textarea中記錄筆記。Windows小工具可以保存數據嗎?

enter image description here

我也像往常一樣,一個gadget.xml清單文件和文件.html,見下文。

如何讀取某些數據/保存小工具中的某些數據?

我知道這是不可能的,通常用JavaScript只(注:使用localstorage是不可能的,因爲我想數據的持久性),所以如何保存/ A 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> 

回答

1

嘗試其中之一:

  1. 有所述內置methods of the System.Gadget.Settings object其可以被用來讀取來自文件的Settings.ini(存儲在C /寫:\用戶\ [用戶] \ AppData \ Local \ Microsoft \ Windows邊欄),但如果小工具關閉或卸載,則此信息將丟失。

  2. 使用FileSystemObject在任何地方創建或讀取或寫入文件夾/文件。限制:文件只能保存爲unicode或ascii。

  3. 使用ADO Stream object在任何地方創建或讀取或寫入文件。限制:無法創建文件夾 - 必須與FileSystemObject一起使用。優點:可以使用計算機上存在的任何代碼頁。

由於我喜歡用utf-8保存文本文件,下面的示例使用第三種方法,但您可能會決定放棄一些錯誤處理。 (NB - 這個例子是基於script published by Andrew Urquhart

幸運的側邊欄能夠使用knownfoldersknownfolderpaths所以找到的路徑,例如您的文檔文件夾一樣簡單

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>

+0

WAW感謝這麼多@mystifeid!這真是太棒了!最後一件事:如何使文本框和身體完全透明?即我們在文本下看到桌面,並且沒有白色/灰色背景。 – Basj

+0

嘗試使用1px x 1px透明png作爲ag:background(請參閱備註[此處](http://msdn.microsoft.com/zh-cn/library/ff486136.aspx)有關如何在html中聲明)然後調整它的大小(例如:imgBackground.style.width =「100%」; imgBackground.style.height =「100%」;) – mystifeid

+0

通過html添加黑色文本或當使用透明的g:background時,普通的javascript方法可能會出現洋紅色。您可以通過添加/刪除文本來解決此問題[g:文本對象](http://msdn.microsoft.com/zh-cn/COM/EN-US /庫/ ff486146.aspx)。這,順便說一句,超出了你原來的問題範圍。請嘗試自己搜索,閱讀並有一個體面的嘗試。如果在幾天之後,您可以不再進步,請提出另一個問題並提供您的css/script/html。 – mystifeid

相關問題