Javascript 用于SSL的Firefox插件

Javascript 用于SSL的Firefox插件,javascript,firefox,firefox-addon,xul,Javascript,Firefox,Firefox Addon,Xul,我正在尝试为Mozilla制作一个插件,它可以打印简单的SSL细节,比如名称和证书,有效期到什么时候 这是我的密码: 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-

我正在尝试为Mozilla制作一个插件,它可以打印简单的SSL细节,比如名称和证书,有效期到什么时候

这是我的密码:

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没有定义。当打印到控制台时,通道结构也是空的。

不太清楚代码在哪里被破坏或者为什么会被破坏(因为我懒得复制缺少的部分,比如
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中的错误

如果您正在使用扩展自动安装程序开发附加组件, 然后,该插件安装在Firefox中,这意味着消息将 出现在浏览器控制台中。但是请参见日志记录的讨论 级别:默认情况下,使用log()、info()、trace()或 在这些情况下,不会记录warn()


你在内容脚本中写过这个吗?如果是这样,您就不能从内容脚本发出请求(这就是为什么它说它不存在的原因)。您需要在main.js中编写此代码。如果要与内容脚本(html、窗口等)通信,则必须使用消息传递:port.emit和addon.emit发送消息,port.on和addon.on侦听消息

XMLHttpRequest
main.js
的上下文中不可用。你真的调用了
req
send
方法吗?