Firefox 扩展,使用 browser.contentDocument 获取对加载文档的引用

发布于 2024-08-14 12:17:11 字数 837 浏览 2 评论 0原文

我想写一个简单的 Firefox 扩展。

如何在浏览器窗口中获取对已加载文档对象的引用?例如,我如何访问 stackoverflow 上加载的这个 html 页面中的文档?根据模糊的mozilla开发中心,我可以使用browser.contentDocument,但它对我不起作用。

    <?xml version="1.0"?>
<overlay id="sample" 
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript">
 function change(){
  //var doc = document.getElementsByTagName("browser")[0].contentDocument;
  //var doc = document.browser.contentDocument;
  var doc = browser.contentDocument;


  var body = doc.getElementsByTagName("body")[0];
  var text = doc.createTextNode("blah");
  body.appendChild(text);
 }
</script>
<statusbar id="status-bar">
  <statusbarpanel id="my-panel" label="click me" onclick="change();"  />
 </statusbar>
</overlay>

I want to write a simple firefox extension.

How can I get a reference to the loaded document object in a browser window? For example, how can I access the document in this html page loaded here on stackoverflow? According to the vague mozilla development center I can use browser.contentDocument, but it is not working for me.

    <?xml version="1.0"?>
<overlay id="sample" 
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript">
 function change(){
  //var doc = document.getElementsByTagName("browser")[0].contentDocument;
  //var doc = document.browser.contentDocument;
  var doc = browser.contentDocument;


  var body = doc.getElementsByTagName("body")[0];
  var text = doc.createTextNode("blah");
  body.appendChild(text);
 }
</script>
<statusbar id="status-bar">
  <statusbarpanel id="my-panel" label="click me" onclick="change();"  />
 </statusbar>
</overlay>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

漫漫岁月 2024-08-21 12:17:11

我不确定您在哪里读到该变量是 browser.contentDocument,因为它在多个位置列出为 content.document(但我链接到常见问题解答)。

I'm not sure where you read that the variable is browser.contentDocument, since it's listed in several spots to be content.document (but I'm linking to the FAQ).

够钟 2024-08-21 12:17:11

用于访问当前显示文档内容的变量:window.content.document

Variable for accessing content of current displayed document: window.content.document

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