简体   繁体   English

是否可以在Firefox插件中使用“ oauth”之类的NodeJS模块?

[英]Is it possible to use NodeJS modules like “oauth” in my Firefox Addon?

This is the first time I'm writing a Firefox addon. 这是我第一次编写Firefox插件。 For the addon I'm trying to write, I need to make calls to Twitter's APIs . 对于我要编写的插件,我需要调用Twitter的API In order to do so, I need an oauth library to start off. 为此,我需要一个oauth库来开始。

I discovered that there already exists such a library in the npm package repository. 我发现npm软件包存储已经存在这样的库 Seeing how Firefox addons are written in Javascript, I decided to try using it. 看到Firefox插件如何用Javascript编写后,我决定尝试使用它。 Unfortunately, it seems that I cannot simply use modules from NodeJS by running npm install oauth . 不幸的是,似乎我不能通过运行npm install oauth来简单地使用NodeJS中的模块。

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? 但是,考虑到它还是用Javascript编写的,还有其他方法可以使用这些模块吗? I am hoping to not have to write an oauth library from scratch just to start my addon. 我希望不必为了开始我的插件而从头开始编写oauth库。

To clarify, I'm writing addon code, not content script. 为了澄清,我在写附加代码,而不是内容脚本。 I want the addon to be the one making the calls to Twitter API. 我希望该插件成为对Twitter API进行调用的插件。

The code I currently have is pretty simple. 我目前拥有的代码非常简单。 I have a class twitter.js : 我有一个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: 在我的index.js我只需将一些代码添加到:

var Twitter = require("./twitter");
var twitter = new Twitter();

It depends on the module, but it often is possible. 它取决于模块,但通常是可能的。 The Browserify makes it easy to convert node modules for use in the browser. Browserify可以轻松转换要在浏览器中使用的节点模块。

You can but you have to browserify and polyfill everything. 你可以,但你必须browserify和填充工具应有尽有。 And there is no documentation. 而且没有文档。 You should browse some github repos to see how others devs are doing it. 您应该浏览一些github仓库,以了解其他开发人员的工作方式。

On NPM, there are few modules designed for Firefox addons, they are tagged jetpack . 在NPM上,很少有为Firefox插件设计的模块,它们被标记为jetpack There is one for Google OAuth , it may help if you need to write your own module. Google OAuth有一个,如果您需要编写自己的模块,可能会有所帮助。

Fully working Twitter OAuth sign in, using JPM addon sdk: 使用JPM插件SDK可以正常运行Twitter OAuth登录:

https://github.com/Noitidart/jpmOAuth/ https://github.com/Noitidart/jpmOAuth/

The dependencies that were needed were the CryptoJS files for the SHA1-HAMC encryption. 所需的依赖项是SHA1-HAMC加密的CryptoJS文件。

Install that addon. 安装该插件。 It will work perfectly. 它将完美地工作。

When customizing for yourself, you will have to update the global variable gOauth.twitter entries of callback , key , and secret . 为自己进行自定义时,您将必须更新callbackkeysecret的全局变量gOauth.twitter条目。 You get this from the manage app on twitter: 您可以从Twitter上的管理应用程序获得此信息:

The details page, this is where you get the key and callback - for me the url is - https://apps.twitter.com/app/12197899 详细信息页面,这是您获取keycallback -对我来说,URL是-https: //apps.twitter.com/app/12197899

Then from the "Keys and Access Tokens" page you get the secret - for me the url is - https://apps.twitter.com/app/12197899/keys 然后从“密钥和访问令牌”页面中获得secret -对我而言,URL是-https: //apps.twitter.com/app/12197899/keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM