用于SSL的Firefox插件

时间:2014-04-25 23:24:56

标签: javascript firefox firefox-addon xul

我正在尝试为Mozilla创建一个插件,它可以打印简单的SSL详细信息,例如Name,并且证书在有效期之前有效。

这是我的代码:

var data = require("sdk/self").data;

var text_entry = require("sdk/panel").Panel({
  width: 412,
  height: 400,
  contentURL: data.url("text-entry.html"),
  contentScriptFile: data.url("get-text.js")
});

require("sdk/widget").Widget({
  label: "Text entry",
  id: "text-entry",
  contentURL: "http://www.mozilla.org/favicon.ico",    
  panel: text_entry,    
});
text_entry.on("show", function() {
  text_entry.port.emit("show");
});

text_entry.port.on("text-entered", function (text) {
  console.log(text);  

var requrl = require("sdk/tabs").activeTab.url;
console.log(requrl);

const {Ci,Cc} = require("chrome");
  //var req = new XMLHttpRequest();


var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
req.open('GET', requrl, false);
req.onload = function(e) {
 console.log(req);
let channel = req.channel;
console.log(requrl);

   if (! channel instanceof  Ci.nsIChannel) {
        console.log("No channel available\n");
        return;
    }
console.log(requrl);

var secInfo = req.securityInfo;
var cert = secInfo.QueryInterface(Ci.nsISSLStatusProvider).SSLStatus.QueryInterface(Ci.nsISSLStatus).serverCert    ;
var validity = cert.validity.QueryInterface(Ci.nsIX509CertValidity);

console.log(requrl);


  console.log("\tCommon name (CN) = " + cert.commonName + "\n");
  console.log("\tOrganisation = " + cert.organization + "\n");
  console.log("\tIssuer = " + cert.issuerOrganization + "\n");
  console.log("\tSHA1 fingerprint = " + cert.sha1Fingerprint + "\n");       
  console.log("\tValid from " + validity.notBeforeGMT + "\n");
  console.log("\tValid until " + validity.notAfterGMT + "\n");


 };

});

它说,XMLHttpRequest没有定义。打印到控制台时,通道结构也是空的。

2 个答案:

答案 0 :(得分:0)

不完全确定代码被破坏的地方或原因(因为我懒得复制像text-entry.html这样的缺失部分。)

无论如何,这是一个适用于我的快速测试,以及SDK附加组件和Scratchpad:

// Save as your main.js.
// Alternatively execute in a Scratchpad in about:newTab.
var sdk = false;
if (!("Cc" in this)) {
  try {
    // add-on SDK version
    this.Cc = require("chrome").Cc;
    this.Ci = require("chrome").Ci;
    this.log = console.error.bind(console);
    this.sdk = true;
    log("using SDK");
  }
  catch (ex) {
    // Scratchpad on about:newtab version
    this.Cc = Components["classes"];
    this.log = console.log.bind(console);
    log("using scratchpad");
  }
}
let r = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
  .createInstance(Ci.nsIXMLHttpRequest);
r.open("GET", "https://tn123.org/");
r.onloadend = function(e) {
  let ok = "OK";
  try {
    log(e);
    // Note: instanceof is an implicit QueryInterface!
    log(this.channel instanceof Ci.nsIChannel);
    log(this.channel.securityInfo instanceof Ci.nsISSLStatusProvider);
    let status, cert;
    log(status = this.channel.securityInfo.SSLStatus);
    log(status.cipherName);
    log(cert = status.serverCert);
    log("Common name (CN) = " + cert.commonName);
    log("Organisation = " + cert.organization);
    log("Issuer = " + cert.issuerOrganization);
    log("SHA1 fingerprint = " + cert.sha1Fingerprint);       
    log("Valid from " + cert.validity.notBeforeGMT);
    log("Valid until " + cert.validity.notAfterGMT);
    for (let k of Object.keys(cert)) {
      if (k[0].toUpperCase() === k[0]) {
        // skip constants
        continue;
      }
      let v = cert[k];
      if (typeof v === "function") {
        continue;
      }
      log(k + ": " + v);
    }
  }
  catch (ex) {
    log("Caught exception", ex);
    ok = ex;
  }
  if (sdk) {
   require("notifications").notify({
     title: "Test done",
     text: "HTTPS test done; result=" + ok
   });
  }
  log("HTTPS test done; result=" + ok);
};
r.send();

PS:我在SDK because中使用console.error

  

如果您使用Extension Auto-installer开发附加组件,   然后加载项安装在Firefox中,这意味着消息将会   出现在浏览器控制台中。但请参阅日志记录的讨论   级别:默认情况下,使用log(),info(),trace()或   warn()在这些情况下不会被记录。

答案 1 :(得分:0)

您是否在内容脚本中写过这个?如果是这样,您就无法从内容脚本发出请求(这就是为什么它不存在)。你需要在main.js中写这个。如果你想与你的内容脚本(html,窗口等)进行通信,你必须使用消息传递:port.emit和addon.emit来发送消息,port.on和addon.on来监听消息。