代码如下:
<html>
<head>
<title>hosts 文件编辑器</title>
<meta http-equiv=content-type content="text/html; charset=gbk"'ui_table');
var bestheight = 70+ui_table.scrollheight;
if(bestheight > screen.height) bestheight = screen.height;
window.resizeto(windoww, bestheight);
//window.moveto((screen.width-windoww)/2,(screen.height-bestheight)/2);
}
var hostfile = '\\drivers\\etc\\hosts';
function getlist(){
var html='';
var ids='';
var i=1;
fso = new activexobject(scripting.filesystemobject);
var spath = fso.getspecialfolder(1)+hostfile;
host = fso.opentextfile(spath, 1, false);
while(!host.atendofstream){
tmp = host.readline();
if(/^#?\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+([a-za-z0-9\-]+\.[a-za-z0-9\-]+(\.(com|net|org|edu|info|biz|cc|tv|gov|mobi|name|cn|en|us|hk|tw))+)$/.test(tmp))
{
as=tmp.replace(/\s+/,'|:|').split('|:|');
ids+='['+i+'],';
if(/^\#/.test(as[0])){
zt = '外网';
ip = as[0].replace(/^\#/,'');
co = '#eee!important';
cz = '<a href=# style=color:red; onclick=zh('+i+',0);>切本地</a> | <a href=# style=color:red; onclick=ed(this,'+i+');>编辑</a> | <a href=# style=color:red; onclick=zh('+i+',2);>删除</a>';
}else{
zt = '本地';
ip = as[0];
co = '#fff!important';
cz = '<a href=# onclick=zh('+i+',1);>切外网</a> | <a href=# style=color:red; onclick=ed(this, '+i+');>编辑</a> | <a href=# style=color:red; onclick=zh('+i+',2);>删除</a>';
}
html += '<tr><td style=background:'+co+';>'+zt+'</td><td style=background:'+co+';text-align:left;text-indent:6px;>'+as[1]+'</td><td style=text-align:left;text-indent:6px;background:'+co+';>'+ip+'</td><td style=background:'+co+';>'+cz+'</td></tr>';
}
i++;
}
var o=document.getelementbyid('ids');
o.value=ids;
host.close();
return html;
}
function ed(eo, iid){
document.getelementbyid('editid').value=iid;
document.getelementbyid('pip').value=eo.parentelement.parentelement.children[2].innertext;
document.getelementbyid('purl').value=eo.parentelement.parentelement.children[1].innertext;
document.getelementbyid('addbutt').style.display='none';
document.getelementbyid('editbutt').style.display='inline';
document.getelementbyid('hostlist').style.display='none';
document.getelementbyid('addhost').style.display='block';
window.resizeto(windoww2,windowh2);
}
function edits(sip, surl){
var oi=document.getelementbyid(sip);
var ou=document.getelementbyid(surl);
if(oi.value=='' || /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(oi.value)==false) {
alert('ip地址不能为空或你输入的ip格式错误.');
oi.focus();
return;
}
if(ou.value=='' || /^[a-za-z0-9\-]+\.[a-za-z0-9\-]+(\.(com|net|org|edu|info|biz|cc|tv|gov|mobi|name|cn|en|us|hk|tw))+$/.test(ou.value)==false) {
alert('域名不能为空或你输入的域名格式错误.');
ou.focus();
return;
}
var iid = document.getelementbyid('editid').value;
var body='';
var i=1;
fso = new activexobject(scripting.filesystemobject);
var spath = fso.getspecialfolder(1)+hostfile;
host = fso.opentextfile(spath, 1, false);
while(!host.atendofstream){
tmp = host.readline();
if(i==iid)
{
body+=oi.value+\t+ou.value+\n;
}
else{
body+=tmp+\n;
}
i++;
}
host.close();
svf = fso.opentextfile(spath, 2, false);
svf.write(body);
svf.close();
window.location.reload();
}
function zh(irs,stype)
{
var body='';
var i=1;
fso = new activexobject(scripting.filesystemobject);
var spath = fso.getspecialfolder(1)+hostfile;
host = fso.opentextfile(spath, 1, false);
while(!host.atendofstream){
tmp = host.readline();
if(i==irs)
{
if(stype==0){
body+=tmp.replace(/^\#/,'');
body+=\n;
}else if(stype==1){
if(/^\#/.test(tmp)){
body+=tmp+\n;
}else{
body+='#'+tmp +\n;
}
}else{
}
}
else{
body+=tmp+\n;
}
i++;
}
host.close();
svf = fso.opentextfile(spath, 2, false);
svf.write(body);
svf.close();
window.location.reload();
}
function zhall(stype)
{
var body='';
var i=1;
var ids1=document.getelementbyid(ids).value;
if(ids1=='') return;
fso = new activexobject(scripting.filesystemobject);
var spath = fso.getspecialfolder(1)+hostfile;
host = fso.opentextfile(spath, 1, false);
while(!host.atendofstream){
tmp = host.readline();
if(stype==0 && ids1.indexof('['+i+'],')!== -1){
body+=tmp.replace(/^\#/,'');
body+=\n;
}else if(stype==1 && ids1.indexof('['+i+'],')!== -1){
if(/^\#/.test(tmp)){
body+=tmp+\n;
}else{
body+='#'+tmp +\n;
}
}else{
body+=tmp+\n;
}
i++;
}
host.close();
svf = fso.opentextfile(spath, 2, false);
svf.write(body);
svf.close();
window.location.reload();
}
function addhosts(sip, surl){
var oi=document.getelementbyid(sip);
var ou=document.getelementbyid(surl);
if(oi.value=='' || /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(oi.value)==false) {
alert('ip地址不能为空或你输入的ip格式错误.');
oi.focus();
return;
}
if(ou.value=='' || /^[a-za-z0-9\-]+\.[a-za-z0-9\-]+\.((net)|(com)|(cn)|(org)|(cc)|(tv))$/.test(ou.value)==false) {
alert('域名不能为空或你输入的域名格式错误.');
ou.focus();
return;
}
fso = new activexobject(scripting.filesystemobject);
var spath = fso.getspecialfolder(1)+hostfile;
host = fso.opentextfile(spath, 8, false);
host.write(\n+oi.value+\t+ou.value+\n);
host.close();
alert('增加域名成功!');
window.location.reload();
}
</script>
<style>
body{
background-color:#99ccff;
}
body *{
font-size:12px;
margin:0px;
padding:0px;
}
.butt{
border: #2c59aa 1px solid;
font-size: 12px;
color: black;
filter: progid:dximagetransform.microsoft.gradient(gradienttype=0, startcolorstr=#ffffff, endcolorstr=#c3daf5);
height:22px;
padding: 3px 5px 0px;
text-decoration: none;
}
.edit{
border: #2c59aa 1px solid;
padding:1px;
}
#ui_table{
width:99%;cell-spacing:0px; margin:1px; border-collapse:collapse; border:1px solid #7d7d7d;
}
#ui_table tr.toptitle{
text-align:center;
}
#ui_table tr.toptitle td {
background-color:#c8e2f7;
}
#ui_table tr td{
border:1px solid #7d7d7d;text-align:center;padding:1px;
}
</style>
</head>
<body onload=init() oncontextmenu=return false; style=text-align:center; margin:0px;overflow-y:auto;overflow-x:hidden;border:none;background:menu;>
<div id=hostlist>
<input type=hidden name=ids/>
<table id=ui_table >
<tr class=toptitle>
<td>状态</td>
<td style=width:140px;>域名</td>
<td>ip地址</td>
<td style=width:140px;>操作</td>
</tr>
<script language=javascript type=text/javascript>
document.writeln(getlist());
</script>
</table>
<div style=margin:6px;text-align:center;>
<input type=button value=全部切本地 name=b4 onclick=zhall(0); class=butt />
<input type=button value=全部切外网 name=b5 onclick=zhall(1); class=butt />
<input type=button value=增加域名 name=b6 onclick=document.getelementbyid('hostlist').style.display='none';document.getelementbyid('addhost').style.display='block'; window.resizeto(windoww2,windowh2); class=butt />
</div>
</div>
<div id=addhost style=display:none;>
<input type=hidden name=editid name=editid />
<div style=text-align:left;margin:30px auto 0px; width:300px;>ip地址:<input type=text name=pip id=pip class=edit /> <input type=button value=127.0.0.1 name=b2 onclick=document.getelementbyid('pip').value='127.0.0.1'; class=butt /></div>
<div style=text-align:left;margin:20px auto; width:300px;>域 名:<input type=text name=purl id=purl class=edit /></div>
<div style=margin:10px;text-align:center;>
<input type=button value=提 交 name=b1 id=addbutt onclick=addhosts('pip','purl'); class=butt />
<input type=button value=编 辑 name=b2 id=editbutt style=display:none; onclick=edits('pip','purl'); class=butt />
<input type=button value=返 回 name=b3 onclick=window.location.reload(); class=butt />
</div>
</div>
<script>
//屏蔽部分键盘操作,比如 f5
function document.onkeydown()
{wek=window.event.keycode;
if((window.event.altkey)&&((wek==37)||(wek==39)))
{window.alert(error:invalid operation!);event.returnvalue=false;}
if((wek==116)||(event.ctrlkey&&wek==82))
{window.event.keycode=0;event.returnvalue=false;}
if((event.ctrlkey)&&(wek==78))
event.returnvalue=false;
if((event.shiftkey)&&(wek==121))
event.returnvalue=false;
if(window.event.srcelement.tagname==a&&window.event.shiftkey)
window.event.returnvalue=false;
if((window.event.altkey)&&(wek==115))
{window.showmodelessdialog(about:blank,,dialogwidth:1px;dialogheight:1px);return false;}
if(wek==122){window.event.keycode=0;window.alert(error:invalid operation!);return false;}
}
</script>
</body>
</html>