I have an add-in written for Firefox 3.6, and now I am updating it for Firefox 4.0, trying to also maintain its compatibility with 3.6. Does anyone have experience trying to do this, or tips on how to do this, without code that gets too spaghetti-ish?
There are several places where its compatibility with both versions means doing something like this:
.myAddonClass { -moz-background-size: 100% 100%; background-size: 100% 100%; }
which generates a CSS warning in both versions. I can live with it. There are other places where I do things like this:
/** get the current version of this addon */ function getVersion() { var version; if (Application.extensions) { // Fx 3.x version = Application.extensions.get(' myaddon@example.com ').version; } else { // Fx 4.x Components.utils.import('resource://gre/modules/AddonManager.jsm'); AddonManager.getAddonByID(' myaddon@example.com ', function(addon) { version = addon.version; }); sleepUntil(function() { return version; } } return version; }
(where sleepUntil is a utility function that uses the Thread.processNextEvent method)
Checking if Application.extensions defined seems cleaner than just checking the Application.version string, but maybe there are some flaws in this approach that I don't know about?
I am also having problems pasting content into web pages. In one case, doc.body.appendChild worked in 3.x but not 4.x, so I tried to do this:
try { // Fx 3.x doc.body.appendChild(myElement); } catch (e) { // Fx 4.x let span = doc.createElement('span'); doc.body.appendChild(span); span.innerHTML = outerHTML(myElement); }
The code above does not work, but if I insert throw new Error('') immediately before doc.body.appendChild(myElement) , then it works, indicating that in Firefox 4, calling appendChild seems to somehow modify myElement gives an error message. I'm sure I can figure out how to make this piece of code work, but I'm worried that I will have a lot of problems too, so I want to see if anyone else has gone through a similar process, and I have all the tips about which I need to know.
Sorry for the long question. Here is what I really ask:
- What advice do you have in order to simultaneously support an addon compatible with both Firefox 3 and Firefox 4?
- What do you think of the idea of branching the code so that we have one version for 3.x and another for 4.x? Then we had to apply any new features to both versions and test them in both versions, etc.
- In general, it is better to check for a specific function (for example, I did with
if (Application.extensions) ... or try / catch) or just check if Application.version starts with '3' or '4'?