Loading...

dev-extensions@lists.mozilla.org

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: help! access window created by open.window() in extension bootstrap Wed Apr 04 12:02:04 2012

On Apr 2, 3:49 pm, bootstrap <[EMAIL PROTECTED]> wrote:
> 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!

 NOTE:  The following is also in a separate but similar message.
 I am sorry more than one thread got started on this issue, but
 my original post didn't appear for a whole day (to me, anyway),
 and I figure it got lost, which led to my my repost.

 -----

 I created a stripped-down version of the extension
 that only contains statements to print-to-error-console to
 show the values of relevant variables.  Download the files:
 http://www.iceapps.com/meanings20120402.zip .

 There is a readme.txt file in there somewhere that notes
 how to make the directory structure work on linux.  I have
 not tried working with extensions on windoze, so I have
 no idea what needs to be changed to work on windoze.


 Let me know if the behavior is different for you.  On my
 computer, no matter what I do, the event listener function
 for the window load event executes *immediately* when
 the window.open() function is called, before the line
 after the window.open() function call executes.


 You can verify all this by starting the browser, browse
 to some webpage with normal HTML text, then click
 on some word in the text.  Inside the mousedown event
 listener function the window.open() function is called,
 which displays a long, thin window just below where
 you clicked the mouse.  Then go click on a word in
 that new window too, to create a complete record in
 the error window.  Then display the "error console",
 in the Tools => WebDeveloper menu and scroll down
 through the information my meanings.js javascript
 code prints out.


 There you will clearly see three things:
   1: window.open() invokes "load event handler".
   2: can't find the window or its contents in the "load event
handler"
   3: can't find the window or its contents after window.open()
returns.


 The one exception to #3 is that cw.name DOES
 report the correct window name as supplied in the
 second argument of window.open() - namely "clarify".
 However, nothing else is correct.  Note, for example,
 that it can't find the "clarify" ID within the tiny window,
 and it can't find the <p> or <b> or <i> tags in the
 tiny window either.


 I put visible ENTER FUNCTION and LEAVE FUNCTION
 printouts for both those event handler functions, so it is
 very easy to see how the program flow goes.  Everything
 is started by a mousedown event (within a text node).
 Inside the mousedown event handler the code calls the
 window.open() function, which immediately causes the
 windowload event handler to execute, then return to
 the line after window.open().  You'll see that clearly.
 And you'll also see that the information about the
 window and its contents are WRONG, both inside
 the window load event handler and also inside the
 mousedown event handler after the window.load()
 function is called and the windowload handler is
 finished executing.

 Download the files here:
 http://www.iceapps.com/meanings20120402.zip
_______________________________________________
dev-extensions mailing list
[EMAIL PROTECTED]
https://lists.mozilla.org/listinfo/dev-extensions