views:

121

answers:

2

Hi, I'm trying to make an firefox extension. Why when I want to use document.body.innerHTML = data; in new opened tab, it doesn't work. Here is my code:

function change() {


//Open google in new Tab and select it
tab=gBrowser.addTab("http://www.google.com");
gBrowser.selectedTab=tab;

//Create nslFile object
var path="/home/foo/notify.txt"
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(path);

//Put file content into data variable
var data = "";
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
                        createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
                        createInstance(Components.interfaces.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 = {}) {
  cstream.readString(-1, str); // read the whole file and put it in str.value
  data = str.value;
}
cstream.close(); // this closes fstream



//change content of google page by file content
document.body.innerHTML = data;

}
A: 

Could you be more specific? What is the JS error you are experiencing?

bsmedberg
+2  A: 

You are trying to change the content document, right? Given your code, document points to the XUL window (or the browser in your case), so you are trying to change that. You should be getting an error about accessing body on it since it won't exist. (Make sure you've enabled chrome errors.) What you really want is content.document.body.innerHTML.

sdwilsh