谷歌不想被内心(XPCOM)

时间:2009-08-18 20:33:15

标签: javascript mozilla xpcom

我正在尝试制作firefox扩展程序。为什么当我想使用document.body.innerHTML = data;在新打开的选项卡中,它不起作用。这是我的代码:

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;

}

1 个答案:

答案 0 :(得分:2)

您正在尝试更改内容文档,对吗?鉴于您的代码,document指向XUL窗口(或您的情况下的浏览器),因此您尝试更改它。您应该收到有关访问body的错误,因为它不存在。 (Make sure you've enabled chrome errors。)你真正想要的是content.document.body.innerHTML

相关问题