#Persistent
#NoTrayIcon
#SingleInstance, Force
; #Include <JSON>
DetectHiddenWindows On
SetTitleMatchMode 2
If (A_Args[1]="")
	ExitApp
Else
	SetTimer, scriptexit, 10000
global CloudInput:=SubStr(A_Args[2]?A_Args[2]:"BD",1,2) "CloudInput"
CopyOfData:="", ReceivePID:=0
OnMessage(10000, "ReceiveMsg")
Return

scriptexit:
	EmptyMem()
	Process, Exist, % A_Args[1]
	If !ErrorLevel
		ExitApp
Return
EmptyMem(PID="AHK Rocks"){
	pid:=(pid="AHK Rocks") ? DllCall("GetCurrentProcessId") : pid
	h:=DllCall("OpenProcess", "UInt", 0x001F0FFF, "Int", 0, "Int", pid)
	DllCall("SetProcessWorkingSetSize", "UInt", h, "Int", -1, "Int", -1)
	DllCall("CloseHandle", "Int", h)
}
ReceiveMsg(wParam, lParam){
	global ReceivePID, Texthwnd
	ReceivePID:=wParam, Texthwnd:=lParam
	SetTimer, CloudInput, -50
}

Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle)  ; 在这种情况中使用 ByRef 能节约一些内存.
; 此函数发送指定的字符串到指定的窗口然后返回收到的回复.
; 如果目标窗口处理了消息则回复为 1, 而消息被忽略了则为 0.
{
	VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0)  ; 分配结构的内存区域.
	; 首先设置结构的 cbData 成员为字符串的大小, 包括它的零终止符:
	SizeInBytes := (StrLen(StringToSend) + 1) * (A_IsUnicode ? 2 : 1)
	NumPut(SizeInBytes, CopyDataStruct, A_PtrSize)  ; 操作系统要求这个需要完成.
	NumPut(&StringToSend, CopyDataStruct, 2*A_PtrSize)  ; 设置 lpData 为到字符串自身的指针.
	; 必须使用发送 SendMessage 而不是投递 PostMessage.
	SendMessage, 0x4a, % DllCall("GetCurrentProcessId"), &CopyDataStruct,, %TargetScriptTitle%  ; 0x4a 为 WM_COPYDAT
	return ErrorLevel  ; 返回 SendMessage 的回复给我们的调用者.
}
SendRequest(url, Method="GET", postData=""){ 
	WebRequest:=ComObjCreate("WinHttp.WinHttpRequest.5.1"), WebRequest.Open(Method, url, (Method="POST" ? 1 : 0))
	WebRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
	WebRequest.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
	WebRequest.Send(postData), WebRequest.WaitForResponse(-1)
	Return WebRequest.ResponseText
}
JScript(Script:=""){
	static doc
	ComObjError(1)
	Try {
		doc:=ComObjCreate("htmlfile"), doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'><body><script>" Script "</script></body>")
		Return ObjBindMethod(doc.parentWindow, "eval")
	} Catch {
		sc:=ComObjCreate("ScriptControl"), sc.Language:="JScript", sc.ExecuteStatement(Script)
		Return ObjBindMethod(sc, "Eval")
	}
}
GGCloudInput(input){
	Try {
		obj:=JScript().("(" (t:=SendRequest("http://ggcloud.autoahk.com/apikey=eindkidj783989h22uhdjh/ggpy_all/" input,"Get")) ")")
		Return StrReplace(obj[1][0][3]["annotation"][0], " ", "'") "`n" obj[1][0][1][0] "`n" input
	} Catch
		Return 0
}
BDCloudInput(input){
	static hObject
	If (!hObject){
		hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
		hObject.SetTimeouts(100, 100, 100, 100)
	}
	js:=JScript()
	hObject.Open("GET", "http://olime.baidu.com/py?input=" input "&inputtype=py&bg=0&ed=20&result=hanzi&resultcoding=utf-8&ch_en=0&clientinfo=web&version=1", 0)  
	Try {
		hObject.Send()
		obj:=js.("(" hObject.ResponseText ")"), obj:=obj["result"][0][0]
		If obj[0]
			Return obj[2]["pinyin"] "`n" obj[0] "`n" input
	}
	Return 0
}
CloudInput:
	Loop 2 {
		If (Msg:=GetText(Texthwnd)){
			result:=%CloudInput%(Msg)
		} Else
			Break
		If (result=0&&Msg:=GetText(Texthwnd))
			Method:=(CloudInput="BDCloudInput"?"GGCloudInput":"BDCloudInput"), result:=%Method%(Msg)
		If (result&&Msg=GetText(Texthwnd)){
			Send_WM_COPYDATA("<<CloudResult>>" result, "ahk_class AutoHotkey ahk_pid" ReceivePID)
			Return
		}
	}
	Send_WM_COPYDATA("<<CloudResult>>`n`ntimeout", "ahk_class AutoHotkey ahk_pid" ReceivePID)
Return

GetText(Hwnd){
	ControlGetText, Msg, , ahk_id%Hwnd%
	Return Msg
}