How do I enable local javascript to read / write files on my PC?

Since Greasemonkey cannot read / write files from the local hard drive, I heard people suggest Google tools, but I have no idea about transfers.

So I decided to add

<script type="text/javascript" src="file:///c:/test.js">/script>

      

This test will now use FileSystemObject to read / write the file. Since it file:///c:/test.js

is a javascript file from my local hard drive, it should probably be able to read / write the file on my local hard drive.

I tried but Firefox forbade the file:///c:/test.js

script to read / write files from the local disk. :(

Is there any setting in Firefox about:config

where we can tell a specific script from, say, localfile or xyz.com, to have read and write permissions on local disk files?

+2


a source to share


1 answer


You can use them within the chrome area.

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

+4


a source







All Articles