最近常看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> </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访问的话可以把那个地址改成相对路径。
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




