繁体   English   中英

Firefox附加内容脚本中的Crypto getRandomValue

[英]Crypto getRandomValue in Firefox add-on content script

我需要在Firefox插件中使用crypto.getRandomValue( MDN )。 但是,我找不到在内容脚本中访问加密货币的方法。 有办法吗?

更新

我弄错了,我没有在内容脚本中尝试过,而是直接在main.js中尝试过。 我是否仅需要为此功能使用内容脚本,否则可以不使用它?

对我的作品就好了......也许你拼错getRandomValuesgetRandomValue在你的代码。

内容脚本

var {PageMod} = require("sdk/page-mod");

// Content scripts should be able to use crypto just fine.
PageMod({
  include: "*",
  contentScript: 'console.log(crypto.getRandomValues(new Uint8Array(10)));'
});

按预期记录一些随机数据。

SDK模块,例如main.js

// SDK modules do not have a window, but we can always borrow the
// hidden window.

var {Cu} = require("chrome");
Cu.import("resource://gre/modules/Services.jsm");
var window = Services.appShell.hiddenDOMWindow;
console.log(window.crypto.getRandomValues(new Uint8Array(10)));

按预期记录一些随机数据。

您正在使用哪个版本的Firefox? 在内容脚本中的Firefox 31中运行此代码对我来说似乎很好:

var arr = new Uint16Array(6);
console.log(window.crypto.getRandomValues(arr));

暂无
暂无

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

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