Login Register

Dojo in a Firefox Extension

Hello all,

I am writing a Firefox extension and I am trying to use Dojo in it, however Dojo is freaking out.
It looks like it doesn't like the fact that it is an extension and not a normal web page.

I have manually including all the files that I do an require on, and that seems to help a little but I am still getting errors all over the place.

Does anyone know how to get Dojo to work in an extension?
Does anyone have any suggestions on things I could try?

Thanks in advance!

More descriptive errors...

without knowing the errors, its hard to say...
also how does Dojo get loaded as an extension then?

-Karl

Ok here are the inital errors

Ok here are the inital errors:

Error: document.getElementsByTagName("head")[0] has no properties
Source file: chrome://myext/content/dojo/dojo.js
Line: 79

(this second one is from running a dojo.require)
Error: dojo is not defined
Source file: chrome://myext/content/myext.js
Line: 1

in my extension I am including the javascript and that is what the first error causes because there isn't a head.

I tried not including dojo.js and instead including all that I run requires on and that helped but then I get other errors.
I can include those but I have to restart firefox to see those errors so I will post them soon
-----
ok here are the errors when I manually include the JS files:

Error: dojo.AdapterRegistry is not a constructor
Source file: chrome://myext/content/dojo/src/io/cometd.js
Line: 32

Error: dojo.hostenv.startPackage is not a function
Source file: chrome://myext/content/dojo/src/hostenv_browser.js
Line: 112

Sounds like a problem for Alex...

my guess is Dojo is detecting FF and trying to load the browser environment.... which isnt there all the time for extensions since extensions exist whether there is a page loaded or not... migt have to make a new HostEnv setup for that use...

I'll try and get Alex over here to comment.

-Karl

This is likely to be a bit

This is likely to be a bit tricky, just because we have not done a lot of testing in that environment. Here are some things to try:

- Make sure you do a custom build. Do not do an xdomain build and make sure you are not using i18n bundles.
- Make sure isDebug and debugAtAllCosts are set to false.

If that does not work, then we'd probably need more info, like the version of Dojo you are using, and the custom build command you used.

The answer may be "it is unsupported at this time", particularly if we need to change more of the Dojo plumbing in order for it to work (as Karl alluded, perhaps a specialized hostenv). Depending on if the above things do not work, particularly if you are using anything before Dojo 0.9. You can file an enhancement request for 0.9 via http://trac.dojotoolkit.org.

I got it working!

Hey all,

Thanks for all your help.
I got it working.
Below is how.

Since in a Firefox extension there is no head tag it was having problems attaching the supplemental JS files.
I got it working by changing the following:

in /dojo/dojo.src I replaced (for me line 73, 74, 75):

var script = document.createElement("script");
script.src = spath;
document.getElementsByTagName("head")[0].appendChild(script);

and I replaced that with:

var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
jsLoader.loadSubScript(spath);

That solves the majority of the problems.
I also needed to update /dojo/src/io/ScriptSrcIO.js
I replaced line 302:

document.getElementsByTagName("head")[0].appendChild(element);

with:

var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
jsLoader.loadSubScript(url);

The next problem I ran into was because I am trying to use the cometd package within dojo, which tries to load an external JS file from the above (ScriptSrcIO.js) file listed above, and that is a problem because the loadSubScript function that I am using above ONLY allows local files to be loaded. Well unless your server is on the same computer as the client (which is VERY unlikely) this is a problem. To solve this problem I used Ajax, and since this is a Firefox extension I am not bound by the cross-domain limitations of normal Ajax. So I replaced the updated code in ScriptSrcIO.js (above) with the following:

var xmlHttpReq = new XMLHttpRequest();
               
xmlHttpReq.onreadystatechange  = function()
{
        if(xmlHttpReq.readyState == 4)
        {
                if(xmlHttpReq.status  == 200)
                {
                        var tempURL = "data:application/x-javascript," + encodeURIComponent(xmlHttpReq.responseText);

                        var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
                        jsLoader.loadSubScript(tempURL);
                }
                else
                {
                        alert("error: " + xmlHttpReq.responseText)
                }
        }
};

xmlHttpReq.open("POST", url, true);
xmlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttpReq.send(null);

Those 2 or 3 changes (depending on if you need to load remote JS files) will make it so you can use Dojo and/or CometD from within a Firefox extension!

Thank you. It is really good

Thank you. It is really good to know that we can use Dojo for Firefox extensions! Did you file CLA? It would be nice if you submitted an official patch.

What and How

What is a CLA?
How do I go about submitting a patch?

Ok..

Ok I found what a CLA is. I can send that in.
How do I submit a patch?
I didn't see anything about how to do that on the Developer page.

Via Trac

We non-committers (casual contributors) file patches by raising a ticket in trac, and then attaching a patch in unified diff format to the ticket. (There are lots of tools that can give you a unified diff between the old file and the new one.) The committer will review the patch and (presumably) integrate it into the build.

Hope this helps, and thanks for doing that!
--
T.J. Crowder
tj at crowder software dot com

Doe these changes...

Break dojo's functionality in a true browser environment though?

-Karl

Yes and No

If you make just those changes then yes, however I am in the process of submitting a patch which will not break any existing functionality. It will just add this as a possibility to try in the JS attaching process.

Done and Done

Hey just to update everyone....

I emailed in a signed CLA and I submitted a Ticket with the updated patch.

The Ticket is #3833
http://trac.dojotoolkit.org/ticket/3833

If you are interested, the code in the attachments implements the changes to allow Dojo to work as a Firefox extension, without losing any normal browser functionality.

So no worries Karl.

Now lets just hope it gets implemented!

Thanks!

-Dallas