HTA编辑HOST文件的脚本



HOSTS 文件编辑器


APPLICATIONNAME="hostedit"
BORDER="thick"
BORDERSTYLE="normal"
CAPTION="yes"
ICON="C:\WINDOWS\system32\rasphone.exe" 
MAXIMIZEBUTTON="yes"
MINIMIZEBUTTON="yes"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes"
VERSION="1.0"
WINDOWSTATE="normal" />

window.onerror = function ()
{
    window.alert("程序发生了未知错误,当前窗口将被关闭!");
    window.close();
    return true;
}
var windowW=450;    //窗体宽
var windowH=200;    //窗体高
var canresize=false;//是否可以改变大小
var windowW2=windowW;//编辑时窗体宽
var windowH2=200;//编辑时窗体高
window.resizeTo(windowW,windowH);
//window.moveTo((screen.width-windowW)/2,(screen.height-windowH)/2);
window.onresize=function(){if(!canresize)window.resizeTo(windowW,windowH);}
function init()
{
    var ui_table = document.getElementById('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 = '切本地 | 编辑 | 删除';

        }else{
            zt = '本地';
            ip = as[0];
            co = '#fff!important';
            cz = '切外网 | 编辑 | 删除';    
        }

            html += ''+zt+''+as[1]+''+ip+''+cz+'';
        }
        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();
}





    
    
        
            状态
            域名
            IP地址
            操作    
        
        
               document.writeln(getlist());
        
    
    
            
            
        
    



    
    IP地址:  

    域  名:

    
            
            
        
    







hostedit.rar

你可能感兴趣的:(HTA编辑HOST文件的脚本)