2005-08-12

GTD ツール NextAction に保存機能をつけてみる

GTD 支援ツール NextAction に、自動保存機能を追加してみた。Windows XP + FireFox 1.0.6 のみで、なんちゃって動作確認。



以下の2つのファイルを、nextaction.htm と同じフォルダに保存して、firstaction.htm を開いて使う。やっつけ。




<html>
<!-- firstaction.htm -->
<head><title>NextAction</title></head>
<frameset rows="0,*">
<frame src="saver.htm" />
<frame src="" name="nextaction" />
</frameset>
</html>



<html>
<!-- saver.htm -->
<head>
<title>Saver</title>
<script language="javascript">
var _path = "C:\\your\\path\\to\\nextaction.htm";
var _previousData;

function save() {

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

data = "<html>" + htmlContent() + "</html>";

data = convertUnicodeToUTF8(data);

var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);

file.initWithPath(_path);

if (file.exists()) { file.remove(true); }

file.create(file.NORMAL_FILE_TYPE, 0666);



var outputStream = Components.classes[ "@mozilla.org/network/file-output-stream;1" ].createInstance( Components.interfaces.nsIFileOutputStream );

//outputStream.init( file, 0x04 | 0x08, 420, 0 );

outputStream.init( file, 2, 0x200, false);

var result = outputStream.write( data, data.length );

outputStream.close();

}



function convertUnicodeToUTF8(s) {

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);

converter.charset = "UTF-8";

var u = converter.ConvertFromUnicode(s);

var fin = converter.Finish();

if(fin.length > 0)

return u + fin;

else

return u;

}



function start() {

window.open("nextaction.htm", "nextaction");

_previousData = htmlContent();

setInterval("saveIfNecessary()", 1000);

}



function saveIfNecessary() {

if (modified()) {

save();

_previousData = htmlContent();

}

}



function modified() {

return _previousData != htmlContent();

}



function htmlContent() {

return parent.frames["nextaction"].document.getElementsByTagName("html")[0].innerHTML;

}



</script>

</head>



<body onload="start()"></body>

</html>