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

类别:Asp 点击:0 评论:0 推荐:

最近常看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 width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
 <tr>
  <td>&nbsp;</td>
  <td width="250" height="100%" align="center" valign="bottom">
   <table width="250" height="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
 <td>
  <iframe id="ShowResult" name="ShowResult" style="background:#3A6EA5;" width="250" height="100%" frameborder="0" src="http://localhost/xml/result.asp"></iframe>
 </td>
    </tr>
    <tr>
 <td height="50" align="center" style="FILTER:progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr='#3A6EA5',endColorStr='#3A6EA5');">
  <form name="kf" id="form">
         <input name="s" id="word" size="25" style="font-size:12px" maxlength="255" type="text" onkeydown="if(event.keyCode==13){return view();}" />
         <input type="hidden" name="lang" value="utf-8" />
         <input type="hidden" name="t" value="word" id="t" />
         <input type="button" style="font-size:12px" value="翻译" onclick="return view();" />
      </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 == "/"){
            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" rel="stylesheet">
<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,"<div style=""background: #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访问的话可以把那个地址改成相对路径。

好了,看看效果吧:

PS:您的电脑要在INTERNET上哦!

是不是不错啊?赶快试试吧! ^_^

---------------------------------------------------------------------------------------------2005-05-14

现已有更新,看看吧:

增加了风格选择,放大,缩小,后退,前进,刷新等新功能,另在结果截取方面也有所修改,使之变得更方便通用!欢迎大家适用。

把你的活动桌面地址设置为:

http://www.v-ec.com/community/iciba/iciba.htm

也欢迎大家在适用后对此提出您的看法和意见。

源码下载地址:http://www.v-ec.com/community/iciba.rar

本文地址:http://com.8s8s.com/it/it6976.htm