Code: Select all
#NoEnv
#SingleInstance force
SetBatchLines, -1
; 托盘菜单
Menu, Tray, NoStandard
Menu, Tray, Add, 退出, GuiClose
; 命令行方式启动
if 1 = /NoGui
{
Gosub, Button同步时间
Sleep, 2000
ExitApp
}
; 界面
Gui, Font, s12
Gui, Add, ActiveX, w320 h230 vWB Hidden Disabled, Shell.Explorer
Gui, Add, Text, xp wp hp Center 0x200 vPrompt cGreen, 载入中...
Gui, Add, Button, x70 w200 h40 Default Disabled vbtn, 同步时间
Gui, Show
; 载入网页
WB.Navigate("http://open.baidu.com/special/time/")
While, WB.readystate != 4 || WB.busy
Sleep 10
; 修改 html
html := wb.document.DocumentElement.innerHTML
html := RegExReplace(html, "si)<div id=head>.*(<DIV id=bjtime class=main>)", "$1")
html := RegExReplace(html, "i)<div class=shouye>.*")
StringReplace, html, html, <p><span class=t4>时间校对</span></p>
html := RegExReplace(html, "si)<p>标准北京时间.*\+8小时</p>")
html := RegExReplace(html, ".*内容系百度根据您的指令自动搜索的.*")
WB.Document.Write(html)
WB.Document.GetElementsByTagName("body")[0].style.overflow := "hidden" ; 不显示滚动条
; 修改控件状态
GuiControl, Hide, Prompt
GuiControl, Show, WB
GuiControl, Enable, btn
Return
Button同步时间:
GuiControl, Disable, btn
GuiControl,, btn, 同步中...
TrayTip, %A_ScriptName%, 时间同步中...
While, !UrlDownloadToVar("http://open.baidu.com/special/time/", html)
Sleep, 1000
if !RegExMatch( html, "i)window\.baidu_time\(\K\d+", unixTimestamp )
{
MsgBox2("获取百度时间失败", "错误", 48)
GuiControl, Enable, btn
GuiControl,, btn, 同步时间
Return
}
unixTimestamp := SubStr(unixTimestamp, 1, 10)
unixTimestamp := unix2Human(unixTimestamp)
FormatTime, _date, %unixTimestamp%, yyyy-MM-dd
FormatTime, _time, %unixTimestamp%, HH:mm:ss
RunWait, %ComSpec% /c date %_date% & time %_time%,, Hide
GuiControl, Enable, btn
GuiControl,, btn, 同步时间
TrayTip, %A_ScriptName%, 同步完成!
SetTimer, RemoveTrayTip, -5000
Return
RemoveTrayTip:
TrayTip
Return
GuiClose:
ExitApp
; ==============================================================================
UrlDownloadToVar(Url, ByRef Result, Timeout = -1)
{
ComObjError(0)
static WinHttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttpReq.Open("GET", Url, True)
WinHttpReq.Send()
ReturnCode := WinHttpReq.WaitForResponse(Timeout) ; Success = -1, Timeout = 0, No response = Empty String
Result := WinHttpReq.ResponseText
Return ReturnCode
}
unix2Human(unixTimestamp)
{
_date = 1970010108
_date += unixTimestamp, s
return _date
}
MsgBox2(Text, Title = "", Options = "")
{
Gui, +OwnDialogs
MsgBox, % Options, %Title%, %Text%
}