base2.JavaScript.bind(window);
...from within a XUL overlay onto browser.xul, the entire browser
window receives the "base2ified" versions of Array, Function, etc.
This means that other extensions will receive the Base2 versions of
these constructors, which is not what I want.
I considered using Base2 only within Javascript code modules within
Firefox, which are executed in their own context, but I am not certain
I understand the ramifications and/or limitations of invoking the
base2.JavaScript.bind function within such a context. If I import a
module from an overlay, will my code written using Base2 still work?
Alternatively, I could just abandon the idea of using bind and simply
eval the namespaces for packages I need, using the more verbose Base2
usage patterns. I'd rather not do this, but does anybody have a
solution?
Regarding the base2.JavaScript.bind(window) issue, it was admittedly
only "fantasy code" because when I actually tried to load base2 via
<script src="chrome://myextension/content/base2.js">, I get a really
funky error regarding evals that looks like this:
Result: Error: missing ] after element list
Source File: chrome://myextension/content/base2.js
Line: 10, Column: 21
Source Code:
var ns=base2.[object ChromeWindow]
I think base2 really really doesn't like the fact that it's being
loaded from a XUL overlay.
For the moment, I've given up on Base2 and gone with jQuery. However,
I'd like to revisit the possibility of using Base2 later. Despite the
fact that I'm trying to write a Firefox extension, I'd like for as
little code to change as possible when doing a Chrome or web version
of the application. Base2 was very enticing in this regard because it
seems like it could essentially normalize Javascript across different
platforms, but this initial foray seems rather discouraging :(
You should use base2.JavaScript.bind to bind a host object. In a plugin
window is the host object for the HTML document. I don't know enough
about plugin development but you could try this:
base2.JavaScript.bind(this);
Run this outside of a function. Let me know if it works! :)
> I considered using Base2 only within Javascript code modules within
> Firefox, which are executed in their own context, but I am not certain
> I understand the ramifications and/or limitations of invoking the
> base2.JavaScript.bind function within such a context. If I import a
> module from an overlay, will my code written using Base2 still work?
I'm sorry I don't know enough about plugins to answer. Can't you just
try it?
-dean
Are you including base2-dom.js or just base2.js? In base2.js there is
only one mention of document and it is inside an if statement:
if (global.navigator) { // browser
var MSIE = /MSIE[\d.]+/g;
var element = document.createElement("span");
...
}
Which version of base2 are you using?
> Regarding the base2.JavaScript.bind(window) issue, it was admittedly
> only "fantasy code" because when I actually tried to load base2 via
> <script src="chrome://myextension/content/base2.js">, I get a really
> funky error regarding evals that looks like this:
>
> Result: Error: missing ] after element list
> Source File: chrome://myextension/content/base2.js
> Line: 10, Column: 21
> Source Code:
> var ns=base2.[object ChromeWindow]
>
> I think base2 really really doesn't like the fact that it's being
> loaded from a XUL overlay.
Can you send me your code? I'm really interested in this. I know that
others have used base2 in their Firefox extensions without any problems.
> For the moment, I've given up on Base2 and gone with jQuery. However,
> I'd like to revisit the possibility of using Base2 later. Despite the
> fact that I'm trying to write a Firefox extension, I'd like for as
> little code to change as possible when doing a Chrome or web version
> of the application. Base2 was very enticing in this regard because it
> seems like it could essentially normalize Javascript across different
> platforms, but this initial foray seems rather discouraging :(
Well, I'm sorry you feel this way. I'm continuing to develop base2 and a
new release is due very soon. If you send me your code I will try to
look into the problem. I've run base2 successfully in Rhino and WSH and
I know that others use base2 in their Firefox extensions too. But I
can't debug text. You will need to send me some code to look at. Even if
it is just a simple extension that shows the error.
-dean
Hi Dean, actually, I tried it with a fresh new extension skeleton and
it worked! The problem was actually only in my head. Unfortunately,
this only works with Base2 proper (not Base2-DOM).
If you're interested, here are the gory details: in a Firefox
extension (plugins are different), you can run Javascript inside a
code module or inside an overlay. An overlay's host object is called
a ChromeWindow (this could be what is choking up Base2 in overlays,
maybe), and the document is actually a XULDocument, not an
HTMLDocument. Everybody's overlays will be merged into the browser's
XUL document (if you want to add a menu or icon or whatever to
Firefox). In a module, all the code that is run is run in a separate
context from anything (so there is no window, no document, no
XMLHttpRequest, no setInterval, etc). So doing base2.JavaScript.bind
on the host object...
a) ...in an overlay will give everybody's extension code Base2 (bad).
I don't think there's a way around this at all, due to the way Firefox
works.
b) ...in a module will do nothing harmful... I hope!
>
> > I considered using Base2 only within Javascript code modules within
> > Firefox, which are executed in their own context, but I am not certain
> > I understand the ramifications and/or limitations of invoking the
> > base2.JavaScript.bind function within such a context. If I import a
> > module from an overlay, will my code written using Base2 still work?
>
> I'm sorry I don't know enough about plugins to answer. Can't you just
> try it?
I did try this, and it works as I expected, but I don't believe I
understand the long term ramifications/implications of using such an
approach.
>
> -dean
On Mar 10, 1:10 pm, Dean Edwards <dean.edwa...@gmail.com> wrote:
> On 10/03/2010 06:43, Min Huang wrote:
>
> > I just tried importing Base2 as a Javascript module in Firefox, and it
> > doesn't work; I get some flavor of NS_ERROR complaining that the
> > document object does not exist. This occurs if I simply try to use
> > Base2 proper (not Base2 DOM); I'm a little confused as to why that
> > would happen as I'm not interested in a document at all. Indeed,
> > neither window nor document exist within a code module.
>
> Are you including base2-dom.js or just base2.js? In base2.js there is
> only one mention of document and it is inside an if statement:
>
> if (global.navigator) { // browser
> var MSIE = /MSIE[\d.]+/g;
> var element = document.createElement("span");
> ...
>
> }
>
> Which version of base2 are you using?
I included only base2.js version 1.0 beta 2, but this turns out to be
my error.
>
> > Regarding the base2.JavaScript.bind(window) issue, it was admittedly
> > only "fantasy code" because when I actually tried to load base2 via
> > <script src="chrome://myextension/content/base2.js">, I get a really
> > funky error regarding evals that looks like this:
>
> > Result: Error: missing ] after element list
> > Source File: chrome://myextension/content/base2.js
> > Line: 10, Column: 21
> > Source Code:
> > var ns=base2.[object ChromeWindow]
>
> > I think base2 really really doesn't like the fact that it's being
> > loaded from a XUL overlay.
>
> Can you send me your code? I'm really interested in this. I know that
> others have used base2 in their Firefox extensions without any problems.
Yes, they probably have used Base2 inside code modules or in another
version of Firefox (I'm using 3.5.8 on Linux). Firefox is wonderfully
different across different versions and OSes (hurray!).
I'm happy to just use Base2 inside code modules, but then there's
really no point in ever using Base2-DOM as I can't even get it to be
sourced in an overlay. Here's a sample extension showing the problem:
http://github.com/retiman/firefox-ifsi/tree/firefox-base2-overlay
and here's a direct link to the XPI:
http://github.com/retiman/firefox-ifsi/blob/firefox-base2-overlay/base2test.xpi
You can install it by having Firefox open it as a file. Check the
error console after installing to see the problem:
Error: missing ] after element list
Source File: chrome://basetest/content/base2-p.js
Line: 10, Column: 21
Source Code:
var ns=base2.[object ChromeWindow]
The file that loads base2 is here:
http://github.com/retiman/firefox-ifsi/blob/firefox-base2-overlay/chrome/content/firefoxOverlay.xul
Err.. my error as in there is no issue with including that, other than
the fact that the first time I did it, I failed to utter the magical
incantations that make Firefox report a sensible error to me.