如何用golang远程控制浏览器
生活随笔
收集整理的這篇文章主要介紹了
如何用golang远程控制浏览器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如何用golang遠程控制瀏覽器
瀏覽器作為現代桌面UI應用已經成為一個強大的存在,網上有很多關于webview的,但是作為操作系統自帶的瀏覽器也是一個很重要的應用場景,如何控制瀏覽器界面,還有一種思路,采用窗體句柄的方式(內部控制還在摸索中,目前通過子窗體遍歷好像不能獲取URL),獲取窗體句柄后就可以對其發消息,設置窗體顯示屬性,實現控制
func?(_this?*UIWebExtern)?Init(msg?Message)?*UIWebExtern?{OpenWithBrowser(msg.Noticeurl)time.Sleep(1?*?time.Second)_this.c?=?make(chan?int)var?hWnd?win.HWNDfor?{hwnd,?err?:=?GetWindowHwndByName("msedge.exe")if?err?==?nil?{hWnd?=?win.HWND(unsafe.Pointer(hwnd))if?IsMyWindowsHwnd(hWnd)?{_this.Hwnd?=?hWndbreak}}hwnd,?err?=?GetWindowHwndByName("chrome.exe")if?err?==?nil?{hWnd?=?win.HWND(unsafe.Pointer(hwnd))if?IsMyWindowsHwnd(hWnd)?{_this.Hwnd?=?hWndbreak}}hwnd,?err?=?GetWindowHwndByName("msedge.exe")if?err?==?nil?{hWnd?=?win.HWND(unsafe.Pointer(hwnd))if?IsMyWindowsHwnd(hWnd)?{_this.Hwnd?=?hWndbreak}}hwnd,?err?=?GetWindowHwndByName("firfox.exe")if?err?==?nil?{hWnd?=?win.HWND(unsafe.Pointer(hwnd))if?IsMyWindowsHwnd(hWnd)?{_this.Hwnd?=?hWndbreak}}hwnd,?err?=?GetWindowHwndByName("QQBrowser.exe")if?err?==?nil?{hWnd?=?win.HWND(unsafe.Pointer(hwnd))if?IsMyWindowsHwnd(hWnd)?{_this.Hwnd?=?hWndbreak}}hwnd,?err?=?GetWindowHwndByName("360Browser.exe")if?err?==?nil?{hWnd?=?win.HWND(unsafe.Pointer(hwnd))if?IsMyWindowsHwnd(hWnd)?{_this.Hwnd?=?hWndbreak}}time.Sleep(2?*?time.Second)}style?:=?win.GetWindowLong(_this.Hwnd,?win.GWL_STYLE)if?msg.NoBorder?{style?&=?^win.WS_SIZEBOX?&?^win.WS_CAPTION}?else?{style?|=?win.WS_SIZEBOX?|?win.WS_CAPTION}win.MoveWindow(_this.Hwnd,?int32(msg.UIpos.Startx),?int32(msg.UIpos.Starty),?int32(msg.UIpos.Width),?int32(msg.UIpos.Height),?true)if?msg.TopMost?{fmt.Print("TopMost\n")_this.bTopMost?=?truewin.SetWindowPos(_this.Hwnd,?win.HWND_TOPMOST,?0,?0,?0,?0,?win.SWP_NOMOVE|win.SWP_NOSIZE)}?else?{win.SetWindowPos(_this.Hwnd,?win.HWND_NOTOPMOST,?0,?0,?0,?0,?win.SWP_NOMOVE|win.SWP_NOSIZE)}win.SetWindowLong(_this.Hwnd,?win.GWL_STYLE,?style)if?msg.FullScreen?{win.SendMessage(_this.Hwnd,?win.WM_KEYDOWN,?0x0000007A,?0x20380001)//time.Sleep(1?*?time.Second)win.SendMessage(_this.Hwnd,?win.WM_KEYUP,?0x0000007A,?0x003C0001)}GetCurrentUrl(_this.Hwnd)return?_this}func?OpenWithBrowser(url?string)?error?{switch?runtime.GOOS?{case?"linux":return?exec.Command("xdg-open",?url).Start()case?"windows":return?exec.Command("rundll32",?"url.dll,FileProtocolHandler",?url).Start()case?"darwin":return?exec.Command("open",?url).Start()default:return?fmt.Errorf("runtime.GOOS?%s?is?not?supported",?runtime.GOOS)}}func?GetWindowHwndByName(name?string)?(uintptr,?error)?{proc,?err?:=?FindProcessByName(name)if?err?!=?nil?{return?0,?err}hwnd,?err?:=?GetWindowHwndByPID(proc.ProcessID)if?err?!=?nil?{return?0,?err}return?hwnd,?nil}func?GetWindowHwndByPID(dwProcessID?int)?(uintptr,?error)?{//func??PostKey(key?uint8)?(uintptr,?error)?{var?user32?=?syscall.NewLazyDLL("user32.dll")//var?p?*os.ProcessPid?:=?dwProcessIDEnumWindows?:=?func(enumFunc?uintptr,?lparam?uintptr)?{user32.NewProc("EnumWindows").Call(uintptr(enumFunc),?uintptr(lparam))}var?hwnd?uintptrcb?:=?syscall.NewCallback(func(h?uintptr,?prm?uintptr)?uintptr?{var?itr_pid?uint32itr_pid?=?0x0001user32.NewProc("GetWindowThreadProcessId").Call(uintptr(h),?uintptr(unsafe.Pointer(&itr_pid)))if?int(itr_pid)?==?Pid?{hwnd?=?h//return?hwnd//user32.NewProc("PostMessageW").Call(h,?0x0100,?uintptr(key),?0)return?0?//?stop?enumeration?(commented?to?make?sure?all?windows?created?by?our?process?get's?the?message)}return?1?//?continue?enumeration})EnumWindows(cb,?0)if?hwnd?==?0?{return?0,?fmt.Errorf("No?window?with?pid?%d?found",?Pid)}return?hwnd,?nil}?
總結
以上是生活随笔為你收集整理的如何用golang远程控制浏览器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通达信l2接口到底是什么原理?
- 下一篇: 多彩创意黑板粉笔字PPT模板