谷歌不想被内化(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; }
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; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试更改内容文档,对吗? 给定您的代码,
document
指向 XUL 窗口(或您的情况下的浏览器),因此您正在尝试更改它。 您应该会收到有关访问它的body
的错误,因为它不存在。 (确保您已启用 Chrome 错误。)您真正想要的是内容.document.body.innerHTML
。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 accessingbody
on it since it won't exist. (Make sure you've enabled chrome errors.) What you really want iscontent.document.body.innerHTML
.