如何启用本地JavaScript来读/写我的电脑上的文件? [英] How to enable local javascript to read/write files on my PC?
问题描述
所以,我决定添加一个
< script type =text / javascriptsrc =file:// /c:/test.js\">/script>
现在,这个测试将使用FileSystemObject来读取/写入文件。因为 file:/// c:/test.js
是本地硬盘上的javascript文件,所以它应该可以在本地硬盘上读写文件磁盘。
我试过了,但Firefox阻止了 file:/// c:/test.js
脚本读取/从本地磁盘写入文件。 :(
在Firefox的
about:config
中有什么设置可以让我们指定一个特定的脚本从本地文件或xyz.com,对我的本地磁盘文件有读/写权限?您可以在chrome
var FileManager =
{
写:
函数(File,Text)
{
if(!File)return;
const unicodeConverter = Components.classes [@ mozilla.org/intl/scriptableunicodeconverter]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter );
unicodeConverter.charset =UTF-8;
Text = unicodeConverterFromUnicode(Text);
const os = Components.classes [@ mozilla.org/network/file-output-stream;1]
.createInstance(Components.interfaces.nsIFileOutputStream);
os.init(File,0x02 | 0x08 | 0x20,0700,0);
os.wr ite(Text,Text.length);
os.close();
},
阅读:
函数(File)
{
if(!File)return;
var res;
const is = Components.classes [@ mozilla.org/network/file-input-stream; 1]
.createInstance(Components.interfaces.nsIFileInputStream);
const sis = Components.classes [@ mozilla.org/scriptableinputstream;1]
.createInstance(Components.interfaces.nsIScriptableInputStream);
is.init(File,0x01,0400,null);
sis.init(is);
res = sis.read(sis.available());
is.close();
return res;
$ b例子:
var x = FileManager.Read(C:\\test.js);
另见
- OneManga管理器插件中的完整文件类
- 使用OMM File class读取插件内的文件
ul>
Since Greasemonkey can't read/write files from a local hard disk, I've heard people suggesting Google gears but I've no idea about gears.
So, I've decided to add a
<script type="text/javascript" src="file:///c:/test.js">/script>
Now, this test will use FileSystemObject to read/write file. Since, the file:///c:/test.js
is a javascript file from local hard disk, it should probably be able to read/write file on my local hard disk.
I tried it but Firefox prevented the file:///c:/test.js
script to read/write files from the local disk. :(
Is there any setting in Firefox's about:config
where we can specify to let a particular script, say from localfile or xyz.com, to have read/write permission on my local disk files?
解决方案 You can use these within chrome scope.
var FileManager =
{
Write:
function (File, Text)
{
if (!File) return;
const unicodeConverter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
unicodeConverter.charset = "UTF-8";
Text = unicodeConverter.ConvertFromUnicode(Text);
const os = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
os.init(File, 0x02 | 0x08 | 0x20, 0700, 0);
os.write(Text, Text.length);
os.close();
},
Read:
function (File)
{
if (!File) return;
var res;
const is = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
const sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
.createInstance(Components.interfaces.nsIScriptableInputStream);
is.init(File, 0x01, 0400, null);
sis.init(is);
res = sis.read(sis.available());
is.close();
return res;
},
}
Example:
var x = FileManager.Read("C:\\test.js");
See Also
这篇关于如何启用本地JavaScript来读/写我的电脑上的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!