views:

381

answers:

2

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"&gt;
<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>
A: 

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).

sdwilsh
https://developer.mozilla.org/En/Working_with_windows_in_chrome_codeunder accessing content document"Assume you have a document loaded in a <tabbrowser>, <browser>, or <iframe> element inside your document. You can use browser.contentDocument to access that document"
John
and your suggestion works. Thanks.
John
In that case, browser is referring to the <browser> element which you'd have to get a reference from. You should mark my answer as correct if it did the trick!
sdwilsh
A: 

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

hvtuananh