2

I have a Firefox addon which is able to save a piece of text to a file on the user's hard-disk.
I've been doing some research on porting this part of functionality over to Google Chrome but looks like there is a lot of challenges, examples:

How can a Chrome extension save many files to a user-specified directory?

Is there a standardized way of achieving this functionality?
I am amazed at how difficult it is to implement something so trivial.
The suggested answer above is to basically implement an extension and packaged app, but this is not an option for me as it will be available to user on internet.
And having to install 2 separate entities seem arkward.

Sample of my Firefox addon code which saves text to a file in a directory of my choice.
I'm looking for the same kind of functionality except in Google Chrome ..

var ostream;

var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath('/home/rob/Desktop/');
file.append('Test.txt');

try{        

    if (file.exists() === false) {file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 420);}

    ostream = FileUtils.openFileOutputStream(file, FileUtils.MODE_WRONLY | FileUtils.MODE_APPEND);

    var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
    converter.charset = "UTF-8";

    var istream = converter.convertToInputStream('This is my string of text');

    // The last argument (the callback) is optional.
    NetUtil.asyncCopy(istream, ostream, function(status) {

        if (!components.isSuccessCode(status)) {

            alert('error');                 

        }else{

            alert('success');
        }
    });
} catch (e) {
    return false;
}
Community
  • 1
  • 1
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150
  • Have you already explored the options in the question and answers that you've referenced? If yes, what's your issue? Where exactly are you stuck? If you need help with choosing the best solution, then you need to elaborate a little bit on what your addon is supposed to do. One very relevant detail is whether you develop the extension for personal use, or for some random strangers on the internet. – Rob W Oct 06 '14 at 15:57
  • The extension will be for random strangers on the internet. Its a basic note taking extension and want to avoid users having to install an app and extension just for save functionality to work. Also I'd like no download prompt in order to select directory or filename. – bobbyrne01 Oct 06 '14 at 16:03
  • Do you really have to save the notes as a file on the disk? It seems that saving the data to a browser-controlled (sandboxed) section of the disk will also fit. E.g. chrome.storage, localStorage, HTML FileSystem API, IndexedDB. Have you already considered these options? – Rob W Oct 06 '14 at 16:06
  • I think you misunderstand "packaged app". Apps are available for everyone the same way extensions are. You also need to consider whether you'll actually need the extension component, or an app alone would siffice. – Xan Oct 06 '14 at 16:14
  • @RobW That would mean users cannot access the files directly using a file system browser. They would be dependent on their web browser to access/backup notes. – bobbyrne01 Oct 06 '14 at 16:15
  • @bobbyrne01 Does your addon really have to be integrated in the browser (as in: does it need to access your browsing session)? If not, you can just develop a Chrome V2 app which *can* get write access to the filesystem after one initial prompt. See https://developer.chrome.com/apps/fileSystem – Rob W Oct 06 '14 at 16:19
  • @RobW yes, it needs to integrate with browser and access content from current page. User may want to save selected text as a note for offline use at a later time. – bobbyrne01 Oct 06 '14 at 16:24
  • @bobbyrne01 Then I suggest to save the data in one of the ways that I described in an earlier comment, and offer a Save As/Open File option if the user wishes to export/import the data. Perhaps your projected user wants their browser extension to pollute their filesysten, but real users may not like that behavior. You have to choose between ease of installation, or ease of access to the filesystem. You can't have both at the same time. – Rob W Oct 06 '14 at 16:29
  • In fact the real users do want the option to save files/text anywhere on file system. It is the most prefered feature of the `Firefox` version of the addon. – bobbyrne01 Oct 06 '14 at 16:41

0 Answers0