I am developing a conventional extension, and now have the basics
working. The extension loads and the javascript executes. At the
moment, all the javascript does is addEventListeners() for window
"load" and "mousedown" events. The registered event listener
functions in my javascript are called at the appropriate times. My
"mousedown" event handler function calls:
var win = window.open("file://home/max/firefox/extension/content/
clarify.html", "clarify", featurestring)
to create a tiny new window with contents from the specified file.
The window is created and displayed at the screenX, screenY, width,
height specified in the featurestring argument.
The problem is, the javascript code is essentially incapable of
working on the contents of the window, either in my "load" event
handler function, or in the lines following the win = window.open()
function call.
My test code reads and prints about 100 variables from the event
argument in the mousedown event handler function, and from the win
variable (return argument of the window.open() function). And only
ONE of those 100 variables makes any sense. Immediately after the win
= window.open() function call, I capture and print out
win.name, and
that prints out as "clarify", which is the correct name assigned by
the second argument to the window.open() function. Everything else is
pure nonsense.
In the "mousedown" event handler, I can't even find a window with a
non-null name, much less the correct name ("clarify"). And I try
everything I can think of from the event argument to that function,
including:
event.target // XULDocument
event.currentTarget // ChromeWindow
event.target.defaultView // ChromeWindow
event.currentTarget.top // ChromeWindow
event.target.defaultView.top // ChromeWindow
event.currentTarget.content // window
event.target.defaultView.top // window
event.currentTarget.content.name // <null>
event.target.defaultView.content.name // <null>
A bunch of other attempts generate errors (and stop execution of the
function at that point). No matter what I do, I cannot find a window
with a name, much less the name I gave the window in window.open().
Furthermore, all attempts to find the <p> or <b> or <i> tags in the
displayed content with .getElementsByTagName(string) fail - they
return an empty HTMLEventCollection (as proved by its .length == 0.
Similarly, all attempts to find the "clarify" id
with .getElementById("clarify") also fail. Note
that .getElementsByTagName("html") and .getElementsByTagName("body")
both return 1 element, but that's true on every HTML document/webpage
I browse too, so that means nothing, and helps nothing.
The above is what my code sees in the "load" event handler. But I get
the exact same results on the lines immediately following win =
window.open() too --- except for the single difference that
win.name
== "clarify" as it should.
The following is the contents in the file loaded by window.open().
Note that is content DOES display correctly in the window displayed by
window.open().
<html>
<body bgcolor="#000000", text="#50ffa0", topmargin="0",
leftmargin="0">
<font face="arial unicode ms", "katrina", "averia serif", "katrina",
"souvienne", "arial unicode ms", size="2">
<p id="clarify" align="left", style="margin-top: 0px"><img src="x.png"
border="0"> <img src="e.png" border="0"> clarify <b>window</b> : test
<i>this</i> text node.</p>
</font>
</body>
</html>
So... where the hell is the window I created? And if that win
variable does contain the window, then why can't
win.document.getElementsByTagName find the <p>, <b>, <i> elements
within it, and why can't win.document.getElementById("clarify") find
the "clarify" id in it? Note that I've tried all sorts of variations
too, like substituting win.content and win.defaultView and other
combinations for win in the above. Nothing I have tried yet works,
and it drives me nuts.
Clearly I'm in the twilight zone here. Clearly there is something
very fundamental and obscure going on that probably makes perfect
sense of this. However, being new to extensions, javascript, mozilla
and firefox has me groping around aimlessly in the dark, even given
the dozen books I bought to find ideas in. Unfortunately, none of the
books is about extensions per-se, but I've also not found anything to
explain this in the dozens of disorganized MDN webpages either. Of
course, I probably read the answer, but didn't understand the
implications, or something. Right?
So, how the hell does my extension javascript work with contents of
windows it creates by calling the window.open() function? What's the
trick? I've probably spent 20 hours already just trying to find the
freaking <p> tag or "clarify" ID in the freaking window I opened.
That's just absurd!