2016-01-06 72 views

回答

1

一個只能使用npm-懸掛的第三方SDK模塊,目前沒有很多這種類型的模塊,例如menuitem

如果你想創建散列,那麼你可以使用crypto.js庫。但是你不能直接在index.js.中使用它。因爲你必須創建pageworker並且在你想創建散列時做消息傳遞。

要創建index.js代碼pageworker將事情是這樣的:

var HashWorker = require('sdk/page-worker').Page({ 
    contentURL: "./hash.html", 
    contentScriptFile : ["./crypto.js","./myfile.js"] 
}); 

在myfile.js您將使用crypto.js函數來創建hash.Note所有文件hash.html, crypto.js和myfile.js必須位於插件的數據目錄中。

hash.html看起來會是這樣的:

<html> 
<head> 
    <title></title> 
</head> 
<body> 

</body> 
</html> 

一旦所有這些設置,您可以從index.js通過消息傳遞溝通myfile.js反之亦然。

要創建的東西散,你可以發送消息給myfile.js這將是這個樣子: index.js

//send message to myfile.js to create hash of some value 
HashWorker.port.emit('createHash',{data : "whose hash to create"}); 

//message received from myfile.js containing hash of specified value 
HashWorker.port.on('hash_result',function(message){ 
    console.log(message.hash); 
}); 

在myfile.js消息傳遞將是這個樣子: MYFILE。 JS

//message received from index.js to create hash of specified value 
self.port.on('createHash',function(message){ 
    var value = message.data 
    var hash = ...//cryptojs function to create hash of above value. 

    //send hash created to index.js 
    self.port.emit('hash_result',{hash : hash}); 
}); 
0

can't I use standard node modules in firefox addon development?

你的意思是core modules,不,你不能。

您需要look for兼容瀏覽器的加密模塊。這裏是one

+0

我也試過crypto-js,但是當我需要它的時候出現「TypeError:C_x64 not found」錯誤。 –

+0

@MichaelPittino你最好創建一個問題@ [他們的github回購](https://github.com/brix/crypto-js),因爲它似乎'C_x64'是一個[他們定義的變量](https:// github的.com /白利糖度/加密JS /搜索?UTF8 =%E2%9C%93&q = C_x64) – minj

0

您可以使用Mozilla自己的Components

const { Cu } = require("chrome"); 
Cu.importGlobalProperties(["crypto"]); 

crypto object導入插件。既然你剛剛問過這個問題,你可能會意識到如何使用它到create a hash。該示例說,使用它就像

function sha256(str) { 
    // We transform the string into an arraybuffer. 
    var buffer = new TextEncoder("utf-8").encode(str); 
    return crypto.subtle.digest("SHA-256", buffer).then(function (hash) { 
    // whatever you want to do with hash; 
    }); 
}