2014-04-25 33 views
1

我正在尝试为Mozilla制作一个插件,用于打印简单的SSL详细信息,如名称和证书的有效期至何日期。用于SSL的Firefox插件

这里是我的代码:

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

+0

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

回答

0

不完全确定你的代码在哪里被破坏或为什么(因为我懒惰复制像text-entry.html这些缺失的部分)。

不管怎么说,这是一个快速测试,在这两个对我的作品,以及SDK插件和便签:

// 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使用console.errorbecause

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

0

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