I love Firefox. I used it for many years, way past when most people switched to Chrome. Even though it was slower, I still loved the amazing selection of Addons, it’s open sourceness, and it helped that Adblock Plus seemed to work a lot better on it.
So when I got the opportunity recently to build a Firefox Add-on, I was excited. I had built a chrome extension before, and it had been fairly painless. Since there were so many Add-ons, I figured it must be just as easy to program Firefox Add-ons.
Well, it ended up being a lot more frustrating than you might think.
So when I first started out I thought I’d use their Add-on Builder, an awesome tool that allows you to build Add-ons straight from the browser. No setting up profiles, no setting up cfx, none of that. Awesome, great deal, let’s use it!
One of the neatest things is that they have a tool that lets you search for libraries you might need for your project by clicking the “plus” symbol next to Libraries. Alright, here goes nothing:
After some Googling, we come across this bug report:
https://bugzilla.mozilla.org/show_bug.cgi?id=923623
No one else is having this issue, and no one has answered it or responded to it, and it’s been 7 days. What is going on?
Let’s try this again
Alright, maybe they’re having some issues, and most people maybe develop by downloading the sdk.
So I install it, get cfx to run in my console (which launches your addon and provides logging of what’s going on).
The Firefox Addon that I was building for the client required geolocation, so one of the first things I did is try to find an easy Firefox addon that used geolocation. Enter this page:
https://addons.mozilla.org/en-US/developers/docs/sdk/Firefox-24/dev-guide/tutorials/reusable-modules.html
Great, a step by step directions to building a Geolocation Addon for Firefox. Let’s get to it!
The page (as it displays now) says:
var {Cc, Ci} = require("chrome");
// Implement getCurrentPosition by loading the nsIDOMGeoGeolocation
// XPCOM object.
function getCurrentPosition(callback) {
var xpcomGeolocation = Cc["@mozilla.org/geolocation;1"]
.getService(Ci.nsIDOMGeoGeolocation);
xpcomGeolocation.getCurrentPosition(callback);
}
var widget = require("sdk/widget").Widget({
id: "whereami",
label: "Where am I?",
contentURL: "http://www.mozilla.org/favicon.ico",
onClick: function() {
getCurrentPosition(function(position) {
console.log("latitude: ", position.coords.latitude);
console.log("longitude: ", position.coords.longitude);
});
}
});
Try it out: create a new directory called "whereami" and navigate to it execute cfx init open "lib/main.js" and add the code above execute cfx run, then cfx run again
For those of you who want to go through this process with me, go ahead, download and install the sdk and follow the instructions. I’ll wait.
Done? For those of you who didn’t go through the steps, let me spoil it for you: it breaks spectacularly. Here’s the error:
error: whereami: An exception occurred.
NS_ERROR_XPC_BAD_IID: Component returned failure code: 0x80570018 (NS_ERROR_XPC_BAD_IID) [nsIJSCID.getService]
Traceback (most recent call last):
File "resource://gre/modules/commonjs/sdk/timers.js", line 31, in notify
callback.apply(null, args);
File "resource://gre/modules/commonjs/sdk/widget.js", line 850, in WC_addEventHandlers/listener/<
self._widget._onEvent(EVENTS[e.type], null, self.node);
File "resource://gre/modules/commonjs/sdk/widget.js", line 428, in WidgetView__onEvent
this._baseWidget._onEvent(type, this._public);
File "resource://gre/modules/commonjs/sdk/widget.js", line 280, in _onEvent
this._emit(type, eventData);
File "resource://gre/modules/commonjs/sdk/deprecated/events.js", line 123, in _emit
return this._emitOnObject.apply(this, args);
File "resource://gre/modules/commonjs/sdk/deprecated/events.js", line 153, in _emitOnObject
listener.apply(targetObj, params);
File "resource://jid1-dlsevcszz55fsa-at-jetpack/whereami/lib/main.js", line 15, in widget<.onClick
getCurrentPosition(function(position) {
File "resource://jid1-dlsevcszz55fsa-at-jetpack/whereami/lib/main.js", line 6, in getCurrentPosition
var xpcomGeolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsIDOMGeoGeolocation);
You do not get in the console what they say you will:
info: latitude: 29.45799999
info: longitude: 93.0785269
So here I am, about 1 hour into development, and I can't get their showing a basic geolocation addon to work. I search and search, but finally ran across a Russian Firefox Support forum where some very talented developer figures out what to do:
http://translate.google.com/translate?hl=en&sl=ru&u=http://forum.mozilla-russia.org/viewtopic.php%3Fid%3D60044
They say:
Also see https://github.com/mozilla/r2d2b2g/pull/438
This is what you need.If someone will face the decision:
Developers decided to make the interface nsIDOMGeoGeolocation unavailable for this, so consult with the 23 version of JS it can not be. Instead, you need to use the interface nsISupports.Here is a code works fine on version 23 and younger:
Components.classes ["@ mozilla.org / geolocation; 1"]. getService (Components.interfaces.nsISupports);
So the correct code is
const {components, Cc, Ci} = require("chrome");
function getCurrentPosition(callback) {
var xpcomGeolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
xpcomGeolocation.getCurrentPosition(callback);
}
var widget = require("sdk/widget").Widget({
id: "whereami",
label: "Where am I?",
contentURL: "http://www.mozilla.org/favicon.ico",
onClick: function() {
getCurrentPosition(function(position) {
console.log("latitude: ", position.coords.latitude);
console.log("longitude: ", position.coords.longitude);
});
}
});
I'm an hour in. I'm afraid of what I find if I go for longer.
Comments are closed, but trackbacks and pingbacks are open.