谷歌不想被内化(XPCOM)

发布于 2024-08-01 18:48:00 字数 1183 浏览 7 评论 0原文

我正在尝试制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-08-08 18:48:00

您正在尝试更改内容文档,对吗? 给定您的代码,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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文