Topics

Firefox

Firefox chrome function : Read/write file on disk

tested on Firefox 7.0.1

const Ci = Components.interfaces;  
const Cc = Components.classes;

function ReadWrite( data ){
    
        	try{
                            Components.utils.import("resource://gre/modules/NetUtil.jsm");
                            Components.utils.import("resource://gre/modules/FileUtils.jsm");

                            var file = FileUtils.getFile("ProfD", ["FileName.txt"]);
                            var ostream = FileUtils.openSafeFileOutputStream(file );
                            var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
                                                     createInstance(Ci.nsIScriptableUnicodeConverter);

                            if( data ){
                            
                                    if ( !file.exists() ){
                                            file.create( Ci.nsIFile.NORMAL_FILE_TYPE, 420);
                                    }
                                    converter.charset = "UTF-8";
                                    var istream = converter.convertToInputStream(data);
                                    NetUtil.asyncCopy(istream , ostream, function(status) {
                                                    if (!Components.isSuccessCode(status)) {
                                                            alert( "Error! :" + status );
                                                            return false;
                                                     }
                                    });
                                                                        
                           }else{
                                    var data = '';
                                    var fstream = Cc["@mozilla.org/network/file-input-stream;1"].
                                                  createInstance(Ci.nsIFileInputStream);
                                    var cstream = Cc["@mozilla.org/intl/converter-input-stream;1"].
                                                  createInstance(Ci.nsIConverterInputStream);
                                    fstream.init(file, -1, 0, 0);
                                    cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish

                                    let (str = {}) {
                                          let read = 0;
                                          do { 
                                                read = cstream.readString(0xffffffff, str); // read as much as we can and put it in str.value
                                                data += str.value;
                                          } while (read != 0);
                                    }
                                    cstream.close(); // this closes fstream
                                    return data;

                               }

	}catch(e){
//                          alert( e);
                            return false;
	}
    }

XUL : open URL with a referrer

This is a small function to use in Firefox extension development which makes use of the referrer parameter sent to loadURI() and addTab() functions

Start multiple programs with one click on windows

It is annoying how most installed programs set them selves in windows startup which you can then manually remove through
Startmenu -> run -> msconfig -> uncheck uneeded programs or services (services could be set to start manually better than disabling it and has the same effect)
but then we need to start some favorite programs that we use often, that could be done as following