XMLHTTP打造漂亮实用的在线桌面翻译

网络整理 - 09-14
 

最近常看e文文档,只怪当时读书不努力,现在知道吃力了,经常打开Kingsoft的词霸搜索网站来翻译,搞的很麻烦,无聊之际想到了搞个[桌面翻译]。如果能直接在桌面上输入要翻译的词然后就可以返回结果那应该方便许多吧,嘿嘿。说干就干。

怎么弄呢?在桌面属性里有一个WEB选项,允许你的桌面用当前主页或其他的网页来显示。我们不如就做个网页好了。开始想着直接写一个提交表单的页就算了然后打开新页来看结果,后来想想这样也不是很好,还要自己翻,不能把最主要的东西一下直接的显示出来,所以就有了[偷]结果的想法。

[偷]结果我们常用的就是用XMLHTTP的方式来请求数据啦,这样的话我们基本任务就已经确定下来了:

写一个提交页面,再写一个截取结果的页面。

本来是说用javascript来完成所有操作,但目标站点的数据采用的是UTF-8编码,取回来的数据一下都成了乱码,没办法,头都大了,只好换ASP了。

提交页面iciba.htm

<html>
<head>
<script language="javascript">
function view(){
 var s = document.all.s.value;
 var l = document.all.lang.value;
 var t = document.all.t.value;
 var cs = "s=" + s + "&lang=" + l + "&t=" + t;
 var url = "http://localhost/xml/result.asp?"
 top.ShowResult.location = url + cs;
 return false;
}
</script>
</head>
<body bgcolor="#3A6EA5" leftmargin="0" topmargin="0" bottommargin="0" rightmargin="0" scroll="no" oncontextmenu="return false">
<table cellpadding="0" cellspacing="0">
 <tr>
  <td> </td>
  <td valign="bottom">
   <table cellpadding="0" cellspacing="0">
    <tr>
 <td>
  <iframe frameborder="0" src="http://localhost/xml/result.asp"></iframe>
 </td>
    </tr>
    <tr>
 <td>
  <form>
         <input size="25" maxlength="255" type="text" />
         <input type="hidden" value="utf-8" />
         <input type="hidden" value="word" />
         <input type="button" value="翻译" />
      </form>
 </td>
    </tr>
   </table>
  </td>
 </tr>
</table>
</body>
</html>

截取数据页面result.asp

<html>
<head>
<script language="javascript" type="text/javascript">
function str2img(str)
{
    var lenStr = str.length;
    var rsString    = "";
    for (var i=0;i<lenStr;i++ ) {
        var theChar   = str.substr(i,1);
        if (theChar == " "){
            rsString += " ";}
        else if (theChar == "-"){
            rsString += "<img src=\"ui/i/yinbiao/zhonggangxian.png\" border=\"0\" align=absmiddle>";}                       
        else if (theChar == "_"){
            rsString += "<img src=\"ui/i/yinbiao/xiahuaxian.png\" border=\"0\" align=absmiddle>";}           
        else if (theChar == "."){
            rsString += "<img src=\"ui/i/yinbiao/dian.png\" border=\"0\" align=absmiddle>";}
        else if (theChar == "\\"){
            rsString += "<img src=\"ui/i/yinbiao/xiexian.png\" border=\"0\" align=absmiddle>";}
        else if (theChar == "http://www.cuoxin.com/"){
            rsString += "<img src=\"ui/i/yinbiao/fanxiexian.png\" border=\"0\" align=absmiddle>";}
        else if (theChar == "?"){
            rsString += "<img src=\"ui/i/yinbiao/wenhao.png\" border=\"0\" align=absmiddle>";}
        else{
            rsString += "<img src=\"ui/i/yinbiao/"+theChar+".png\" border=\"0\" align=absmiddle>";}
    }
    document.write(rsString);
}
</script>
<base href="http://cb.kingsoft.com/">
<LINK href="ui/c/main.css" type="text/css">
<style>
body{font-size:12px;background-color:#3A6EA5;}
</style>
</head>
<body topmargin="0" bottommargin="0">
<%
on error resume next

function getHTTPPage(url)
   dim Http
   set http=createobject("Microsoft.XMLHTTP")
   Http.open "GET",url,false
   Http.send()
   if Http.readystate<>4 then
   exit function
   end if
   getHTTPPage=bytesToBSTR(Http.responseBody,"UTF-8")
   set http=nothing
   if err.number<>0 then err.Clear
end function
Function BytesToBstr(body,Cset)
 dim objstream
 set objstream = Server.CreateObject("adodb.stream")
 objstream.Type = 1
 objstream.Mode =3
 objstream.Open
 objstream.Write body
 objstream.Position = 0
 objstream.Type = 2
 objstream.Charset = Cset
 BytesToBstr = objstream.ReadText
 objstream.Close
 set objstream = nothing
End Function
Function toUTF8(szInput)
    Dim wch, uch, szRet
    Dim x
    Dim nAsc, nAsc2, nAsc3
   
    If szInput = "" Then
        toUTF8 = szInput
        Exit Function
    End If
    For x = 1 To Len(szInput)
        wch = Mid(szInput, x, 1)
        nAsc = AscW(wch)
        If nAsc < 0 Then nAsc = nAsc + 65536
   
        If (nAsc And &HFF80) = 0 Then
            szRet = szRet & wch
        Else
            If (nAsc And &HF000) = 0 Then
                uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
                szRet = szRet & uch
            Else
                uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
                            Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
                            Hex(nAsc And &H3F Or &H80)
                szRet = szRet & uch
            End If
        End If
    Next
       
    toUTF8 = szRet
End Function

for each i in request.querystring
 cs = cs & "&" & i & "="
 If instr(cs,"s")<1 then
  cs = cs & request.querystring(i)
 else
  cs = cs & toUTF8(request.querystring(i))
 end if
Next

cs = right(cs,len(cs)-1)

url = "http://cb.kingsoft.com/search?" & cs
'response.write url
'response.end

If url<>"" then
    str = getHTTPPage(url)
    ls = instr(str,"<divbackground: #F6F6F6")-15
    rs = instr(str,"</div><p")+6
    ns = rs-ls
    resultstr = mid(str,ls,ns)
    resultstr = replace(resultstr,"./search?","http://localhost/xml/result.asp?")
    Response.write resultstr
End if

%>

</body>
</html>

因为iciba.htm要放到桌面上如果直接查找文件选择它的话就不能用HTTP访问,所以里边用了result.asp的绝对路径,当然如果大家在桌面上选择网页的时候选择地址的话即用HTTP访问的话可以把那个地址改成相对路径。