This is the first time I’m writing a Firefox addon. For the addon I’m trying to write, I need to make calls to Twitter’s APIs. In order to do so, I need an oauth library to start off.
I discovered that there already exists such a library in the npm package repository. Seeing how Firefox addons are written in Javascript, I decided to try using it. Unfortunately, it seems that I cannot simply use modules from NodeJS by running npm install oauth
.
Message: Module `crypto` is not found at resource://gre/modules/commonjs/crypto.js
However, is there some other way that I can use these modules, considering it is nonetheless written in Javascript? I am hoping to not have to write an oauth library from scratch just to start my addon.
To clarify, I’m writing addon code, not content script. I want the addon to be the one making the calls to Twitter API.
The code I currently have is pretty simple. I have a class twitter.js
:
var OAuth = require("oauth");
var OAuth2 = OAuth.OAuth2;
var Twitter = function() {
};
module.exports = Twitter;
And in my index.js
I simply add some code to:
var Twitter = require("./twitter");
var twitter = new Twitter();
3
Answers
It depends on the module, but it often is possible. The Browserify makes it easy to convert node modules for use in the browser.
You can but you have to browserify and polyfill everything. And there is no documentation. You should browse some github repos to see how others devs are doing it.
On NPM, there are few modules designed for Firefox addons, they are tagged jetpack. There is one for Google OAuth, it may help if you need to write your own module.
Fully working Twitter OAuth sign in, using JPM addon sdk:
https://github.com/Noitidart/jpmOAuth/
The dependencies that were needed were the CryptoJS files for the SHA1-HAMC encryption.
Install that addon. It will work perfectly.
When customizing for yourself, you will have to update the global variable
gOauth.twitter
entries ofcallback
,key
, andsecret
. You get this from the manage app on twitter:The details page, this is where you get the
key
andcallback
– for me the url is – https://apps.twitter.com/app/12197899Then from the “Keys and Access Tokens” page you get the
secret
– for me the url is – https://apps.twitter.com/app/12197899/keys