下载地址:https://files.cnblogs.com/joyrice/ipboy.xpi
最近学习了下firefox扩展的编写,这个算是实习作品,可以在浏览网站的时候,显示网站ip:
主要代码如下:
var IpBoy = {
on: false,
oldURL: null,
init: function() {
// Listen for webpage loads
gBrowser.addProgressListener(this,
Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
},
uninit: function() {
gBrowser.removeProgressListener(this);
},
processNewURL: function(aURI) {
if (aURI.spec == '' || aURI.spec == 'about:blank')
{
var lbl = document.getElementById("ipboy_statusbar_label");
lbl.value = '^_^';
return;
}
try
{
if (aURI.spec == this.oldURL)
return;
var x = new RegExp("http:\/\/([^:\/\?\&]*)", "ig");
var execR = x.exec(aURI.spec);
var hostname = RegExp.$1;
//alert(hostname);
//return;
var dns = Components.classes["@mozilla.org/network/dns-service;1"].
getService(Components.interfaces.nsIDNSService);
var dnsResult = dns.resolve(hostname, 0);
var ip = 'ip?';
if (dnsResult.hasMore())
ip = (dnsResult.getNextAddrAsString());
var lbl = document.getElementById("ipboy_statusbar_label");
lbl.value = ip;
this.oldURL = aURI.spec;
}
catch(ex)
{
//alert(aURI.spec);
var lbl = document.getElementById("ipboy_statusbar_label");
lbl.value = 'error';
}
},
QueryInterface: function(aIID)
{
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_NOINTERFACE;
},
onLocationChange: function(aProgress, aRequest, aURI)
{
this.processNewURL(aURI);
},
onStateChange: function() {},
onProgressChange: function() {},
onStatusChange: function() {},
onSecurityChange: function() {},
onLinkIconAvailable: function() {},
onMenuItemCommand: function() {
if (this.on)
{
this.uninit();
var lbl = document.getElementById("ipboy_statusbar_label");
lbl.value = "ipboy off";
this.on = false;
}
else
{
this.init();
var lbl = document.getElementById("ipboy_statusbar_label");
lbl.value = "ipboy on";
this.on = true;
}
//var pnl = document.getElementById("hostboypanel");
//pnl.hidden = !pnl.hidden;
//window.open("chrome://hostboy/content/hostboy.xul", "", "chrome");
}
};
浙公网安备 33010602011771号