Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a textfile with firefox add-on sdk?

here is my main.js:

var widgets = require("sdk/widget");
var {Cc, Ci, Cu} = require("chrome");
var promptSvc = Cc["@mozilla.org/embedcomp/prompt-service;1"].
    getService(Ci.nsIPromptService);
var stringtosave = 'secret information';

var widget = widgets.Widget({
    id: "save_text_button",
    label: "save text",
    contentURL: "http://www.mozilla.org/favicon.ico",
    onClick: function() {
        promptSvc.alert(null, "My Add-on", stringtosave + " saved! ");
    }
});

It can alert the string using XPCOM.

How can I save the stringtosave in a textfile somewhere on the PC's harddrive?

Maybe there is a simple solution, that also makes use of XPCOM.

like image 927
Andromeda Avatar asked Feb 14 '23 05:02

Andromeda


1 Answers

Assuming you want to use the profile directory

const { pathFor } = require('sdk/system');
const path = require('sdk/fs/path');
const file = require('sdk/io/file');

function saveText(name, str){
  var filename = path.join(pathFor('ProfD'), name);
  var textWriter = file.open(filename, 'w');
  textWriter.write(str);
  textWriter.close();
}

function readText(name){
  var filename = path.join(pathFor('ProfD'), name);
  if(!file.exists(filename)){
    return null;
  }
  var textReader = file.open(filename, 'r');
  var str = textReader.read();
  textReader.close();
  return str;
}
like image 103
paa Avatar answered Apr 09 '23 21:04

paa