2014-04-25 32 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來偵聽消息。