在运行框中翻译

1、原理

  •  原理:爬取搜狗页面翻译结果然后放到运行框中。
  • 使用方法:
    • ①.使用v.1 u32版本运行脚本
    • ②. win+r打开运行框,
    • ③.输入关键字mean+空格+想要翻译的内容 然后【回车】就可以得到结果,只输入mean就会翻译剪切板内容

2、运行效果

在运行框中翻译

3、完整代码(v1.x_u32)

;keyWords:="\u4F60\u597D\u5440"
;keyWords:=""
;keyWords:=Util.decodeUtf8(keyWords)
;
;msgbox % SoGouFanyi.mean("baidu")

#If WinActive(Util.decodeUtf8("\u8FD0\u884C")) ;运行
;执行自定义命令
$ENTER::
    ;解决当前搜狗输入饭干扰,其他输入法需要加入这个判定
    if WinExist("ahk_class SoPY_Comp")
    {
        send {ENTER}
        return
     }
    ControlGetText, inputCmd , Edit1,  % Constant.RUN_BOX_TITLE
    excuteFlag:=RunBoxUtil.runCmd(inputCmd)
    ;没有找到对应的自定义命令
    if(excuteFlag=1 ||excuteFlag=0)
    {
        if(excuteFlag==1)
            send {ENTER}
        Util.closeRunWd()
    }
;        ControlSend, % Constant.RUN_BOX_CONTROL, {ENTER}, % Constant.RUN_BOX_TITLE
    return
#if
;----------------------------------------------------------------------------------------------------------运行框类
class RunBoxUtil
{
    ;excel计算对象
    ;static oExcel := ComObjCreate("Excel.Application")

    ;当前是什么操作
    static OPERATE:=""
    ;当前表达式
    static EXPRESSION:=""
    static RUN_BOX_CONTROL="Edit1"
    static RUN_BOX_TITLE=Util.decodeUtf8("\u8FD0\u884C")
    ;翻译
    runCmd(rawStr)
    {
        if(inStr(rawStr,"mean")==1)
        {

            if(Strlen(trim(rawStr))==4)
            {
                keyWords:=clipBoard   ;计算粘贴板
                msg:=util.decodeUtf8("[\u7C98\u8D34\u677F]")
             }
            else
                StringReplace, keyWords,rawStr,mean
             ;搜狗翻译
             result:=SoGouFanyi.mean(keyWords)
             result:=rawStr msg "=" result
             ControlsetText,  % RunBoxUtil.RUN_BOX_CONTROL,% result, % RunBoxUtil.RUN_BOX_TITLE
             ControlSend, % RunBoxUtil.RUN_BOX_CONTROL, {END}, % RunBoxUtil.RUN_BOX_TITLE
             return 3
        }else
            return 1
    }
}
;----------------------------------------------------------------------------------------------------------运行框类
;----------------------------------------------------------------------------------------------------------搜狗翻译类2
class SoGouFanyi
{
      ;https://fanyi.sogou.com/text?keyword=hello&transfrom=auto&transto=en&model=general 英文到中文
      ;https://fanyi.sogou.com/text?keyword=%E4%BD%A0%E5%A5%BD&transfrom=auto&transto=zh-CHS&model=general 中文到英文
      static url:="https://fanyi.sogou.com/text?keyword="
      static User_Agent:="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
      ;翻译,调用该方法即可
      mean(keyWords)
      {
        return SoGouFanyi.dealResult(SoGouFanyi.request(encode_url:=Util.urlEncode(SoGouFanyi.getUrl(SoGouFanyi.url,keyWords))))
      }
     ;调用搜狗翻译
      getUrl(url,keyWords)
      {
           language:="en"
           trans_url=%url%%keyWords%&transfrom=auto&transto=%language%&model=general
           return trans_url
      }
      ;请求
      request(encode_url)
      {
;         encode_url:="https://fanyi.sogou.com/text?keyword=%E5%B7%B2%E7%94%A8%E4%BB%A5%E4%B8%8B%E5%86%85%E5%AE%B9%E7%BF%BB%E8%AF%91%EF%BC%9A%0A%E5%8F%91%E9%A1%BA%E4%B8%B0%0A%0A%E4%BB%8D%E8%A6%81%E7%BF%BB%E8%AF%91%EF%BC%9A%0A%E5%8F%91%E9%A1%BA%E4%B8%B0...&transfrom=auto&transto=en&model=general"
         req := ComObjCreate("WinHttp.WinHttpRequest.5.1")
         req.Open("get",encode_url)
         req.setRequestHeader("User-Agent",SoGouFanyi.User_Agent) ;在open之后
         req.send()
         req.WaitForResponse()
         result:=req.ResponseText
         ObjRelease(req) ;释放内存
         return result
      }
      ;处理返回的结果
      dealResult(result)
      {
            label_start=<div class="output ;筛选结果的开始元素,从html中获取
            labels:=SoGouFanyi.getHtmlElement(result,label_start)
            label_start=<p id="trans-result ;筛选结果的开始元素,从html中获取
            labels2:=SoGouFanyi.getHtmlElement(labels,label_start)
            labels:= labels2?labels2:labels
            return   SoGouFanyi.getInnerHtml(labels)
      }
      ;获取 标签中的文本
      getInnerHtml(html)
      {
          res:="" ,char_arr:=[]
          loop ,parse ,html
          {
             char_arr.push(A_loopField)
             if(A_loopField=">")
             {
                while(char_arr.pop()!="<")
                    continue
                if char_arr.length()>0
                    char_arr.push(delim)
             }
          }
          loop ,% char_arr.length()
            res:=res char_arr[A_index]
          return res
      }

      ;获取html的某些元素,带有标签,传入的是html(完整页面) ,label 起始标签可以不闭合
      getHtmlElement(html,label)
      {
          pre_char:=""
          counter_list :=[]  ;xx.length()
          p:=SubStr(label ,2,inStr(trim(label)," ")-2) ;获取标签
          index:=inStr(html,label)
          if !index
            return ;
          frag_html:=SubStr(html,index )
          loop ,parse ,frag_html
          {
              to_char:= pre_char A_LoopField
              if(A_LoopField ="<")
                  counter_list.push(A_index)
              if(to_char="</")
              {
                  counter_list.pop()
                  counter_list.pop()
                  if(counter_list.length()<=0)
                     return SubStr(frag_html,1,A_index) . p . ">"
              }
              if(to_char="/>")
              {
                  counter_list.pop()
                  if(counter_list.length()<=0)
                     return ;
              }
              pre_char:=A_LoopField
          }
      }
}
;----------------------------------------------------------------------------------------------------------搜狗翻译类2
;----------------------------------------------------------------------------------------------------------util类
class Util
{
       ;利用js对url编码
       urlEncode(url)
       {
           oSC := ComObjCreate("ScriptControl")
           oSC.Language := "JScript"
           Script := "var Encoded = encodeURI(""" . url . """)"
           oSC.ExecuteStatement(Script)
           Return, oSC.Eval("Encoded")
       }
       ;对utf-8解码
       decodeUtf8(value)
       {
             i := 0
             while (i := InStr(value, "\",, i+1)) {
               if !(SubStr(value, i+1, 1) == "u")
                 this.ParseError("\", text, pos - StrLen(SubStr(value, i+1)))
               uffff := Abs("0x" . SubStr(value, i+2, 4))
               if (A_IsUnicode || uffff < 0x100)
                 value := SubStr(value, 1, i-1) . Chr(uffff) . SubStr(value, i+6)
             }
           Return,value
        }
        ;关闭运行窗口
      closeRunWd()
      {
          IfWinExist ahk_exe explorer.exe
          {
              RunWdName_Utf8:="\u8FD0\u884C" ;运行
              RunWdName:=Util.decodeUtf8(RunWdName_Utf8)
              WinClose ,%RunWdName% ; 使用前面找到的窗口
          }
      }
}
;----------------------------------------------------------------------------------------------------------util类

 

给TA捐赠
共{{data.count}}人
人已捐赠
其他

选中字符串中的字母全部大写和全部小写

2023-2-1 22:47:30

其他

按快捷键 ` 显示或者隐藏 Total Commander 10.52 主窗口 2023-2-5 (快捷键 ` 即波浪键~,位于Esc键正下方,位于Tab键正上方)

2023-2-5 21:46:16

2 条回复 A文章作者 M管理员
  1. AHK中文社区

    网页翻译有限制,还是使用接口好一点

    • Tmz

      嗯,接口确实稳定,这个相对简单点、

个人中心
购物车
优惠劵
有新私信 私信列表
搜索