// 为窗口设置新的尺寸 func win_handler_sizeTo(browser *cef.Browser, args []cef.V8Value) (result interface{}) { fmt.Println("win_handler_sizeTo") width := cef.V8ValueToInt32(args[0]) height := cef.V8ValueToInt32(args[1]) h := win.HWND(browser.GetWindowHandle()) var rect win.RECT win.GetWindowRect(h, &rect) fmt.Printf("win_handler_sizeTo Left=%v,Right=%v,Width=%v,Height=%v\n", rect.Left, rect.Top, width, height) win.MoveWindow(h, rect.Left, rect.Top, width, height, true) //result = 1 return }
// 最大化窗口 func win_handler_maximize(browser *cef.Browser, args []cef.V8Value) (result interface{}) { fmt.Println("win_handler_maximize") h := win.HWND(browser.GetWindowHandle()) win.ShowWindow(h, win.SW_MAXIMIZE) return }
// 为窗口设置标题 func win_handler_setTitle(browser *cef.Browser, args []cef.V8Value) (result interface{}) { title := cef.V8ValueToString(args[0]) h := win.HWND(browser.GetWindowHandle()) win.SetWindowText(h, title) return }
// 恢复窗口 func win_handler_restore(browser *cef.Browser, args []cef.V8Value) (result interface{}) { fmt.Println("win_handler_restore") h := win.HWND(browser.GetWindowHandle()) win.ShowWindow(h, win.SW_RESTORE) return }
// 退出程序 func win_handler_quit(browser *cef.Browser, args []cef.V8Value) (result interface{}) { h := win.HWND(browser.GetWindowHandle()) win.SendMessage(h, win.WM_CLOSE, 0, 0) //win.PostQuitMessage(0); os.Exit(1) return }
// 模拟输入字符串 func InjectKeyPress(b *cef.Browser, text string) { h := b.GetHost() ss := strings.Split(text, "") for i := 0; i < len(ss); i++ { c := ss[i] InjectKey(h, c) } }
// 模拟鼠标点击 func InjectMouseClick(b *cef.Browser, x, y /*, buttonType int, mouseUp bool, clickCount*/ int) { var event cef.CefMouseEvent event.Modifiers = 0 event.X = x event.Y = y h := b.GetHost() h.SendMouseClickEvent(&event, 0, false, 1) h.SendMouseClickEvent(&event, 0, true, 1) }
// 移动窗口 func win_handler_startDrag(browser *cef.Browser, args []cef.V8Value) (result interface{}) { fmt.Println("win_handler_startDrag") h := win.HWND(browser.GetWindowHandle()) var pt win.POINT win.GetCursorPos(&pt) isDrag = true win.PostMessage(h, win.WM_LBUTTONDOWN, win.HTCAPTION, uintptr(win.MAKELONG(uint16(pt.X), uint16(pt.Y)))) return }
// 为窗口设置新的位置 func win_handler_moveTo(browser *cef.Browser, args []cef.V8Value) (result interface{}) { fmt.Println("win_handler_moveTo") left := cef.V8ValueToInt32(args[0]) top := cef.V8ValueToInt32(args[1]) fmt.Printf("win_handler_moveTo left=%v,top=%v\n", left, top) h := win.HWND(browser.GetWindowHandle()) var rect win.RECT win.GetWindowRect(h, &rect) width := int32(rect.Right - rect.Left) height := int32(rect.Bottom - rect.Top) fmt.Printf("win_handler_moveTo Left=%v,Right=%v,Width=%v,Height=%v\n", left, top, width, height) win.MoveWindow(h, left, top, width, height, true) return }
func win_start_browser(browser *cef.Browser, message *cef.CefProcessMessage) interface{} { fmt.Printf("win_start_browser\n") handle := browser.GetWindowHandle() openerHandle := browser.GetOpenerWindowHandle() rootHandle := browser.GetRootWindowHandle() fmt.Printf("win_start_browser handle=%v openerHandle=%v rootHandle=%v\n", handle, openerHandle, rootHandle) win.ShowWindow(win.HWND(rootHandle), win.SW_MAXIMIZE) return 0 }
// 关闭窗口 func win_handler_close(browser *cef.Browser, args []cef.V8Value) (result interface{}) { h := win.HWND(browser.GetWindowHandle()) win.SendMessage(h, win.WM_CLOSE, 0, 0) return }
// 设置焦点 func InjectFocus(b *cef.Browser, focus bool) { h := b.GetHost() h.SetFocus(focus) }
// 模拟点击 func win_emuClick(browser *cef.Browser, message *cef.CefProcessMessage) interface{} { fmt.Printf("win_emuClick\n") handle := browser.GetWindowHandle() openerHandle := browser.GetOpenerWindowHandle() rootHandle := browser.GetRootWindowHandle() fmt.Printf("win_start_browser handle=%v openerHandle=%v rootHandle=%v\n", handle, openerHandle, rootHandle) //win.ShowWindow(win.HWND(rootHandle), win.SW_MAXIMIZE) // 模拟点击 // 查找窗口 url := message.GetArgumentList().GetString(1) fmt.Printf("url=%v\n", url) w, ok := windowHolders[url] if ok { fmt.Printf("找到窗口\n") b, o := cef.BrowserByHandle(unsafe.Pointer(w)) if o { x := message.GetArgumentList().GetInt(2) y := message.GetArgumentList().GetInt(3) buttonType := message.GetArgumentList().GetBool(4) fmt.Printf("X=%v Y=%v ButtonType=%v\n", x, y, buttonType) rootHandle = b.GetRootWindowHandle() //win.ShowWindow(win.HWND(rootHandle), win.SW_MAXIMIZE) // 模拟鼠标 var pt win.POINT pt.X = int32(x) // This is your click coordinates pt.Y = int32(y) hWnd := win.HWND(rootHandle) go func() { win.SetForegroundWindow(hWnd) time.Sleep(3 * time.Second) win.ClientToScreen(hWnd, &pt) fmt.Printf("ClientToScreen X=%v Y=%v\n", pt.X, pt.Y) cx_screen := win.GetSystemMetrics(win.SM_CXSCREEN) //屏幕 宽 cy_screen := win.GetSystemMetrics(win.SM_CYSCREEN) // 高 real_x := 65535 * pt.X / cx_screen //转换后的 x real_y := 65535 * pt.Y / cy_screen // y var input win.MOUSE_INPUT input.Type = win.INPUT_MOUSE input.Mi.Dx = real_x input.Mi.Dy = real_y if buttonType { input.Mi.DwFlags = (win.MOUSEEVENTF_ABSOLUTE | win.MOUSEEVENTF_MOVE | win.MOUSEEVENTF_RIGHTDOWN | win.MOUSEEVENTF_RIGHTUP) } else { input.Mi.DwFlags = (win.MOUSEEVENTF_ABSOLUTE | win.MOUSEEVENTF_MOVE | win.MOUSEEVENTF_LEFTDOWN | win.MOUSEEVENTF_LEFTUP) } input.Mi.MouseData = 0 input.Mi.DwExtraInfo = 0 input.Mi.Time = 0 win.SendInput(2, unsafe.Pointer(&input), int32(unsafe.Sizeof(input))) fmt.Printf("点击") }() //hWnd = win.WindowFromPoint(pt) } } else { fmt.Printf("找不到窗口\n") } return 0 }
func emuInput2(browser *cef.Browser) { fmt.Printf("emuInput\n") // 查找 frame := browser.GetMainFrame() //frameCount := browser.GetFrameCount() //fmt.Printf("frameCount=%v\n", frameCount) //frameIdentifiers := browser.GetFrameIdentifiers() //fmt.Printf("frameIdentifiers=%v\n", frameIdentifiers) // 计算frame坐标 //mainFrame := browser.GetMainFrame() //login_frame_left, login_frame_top := navigator.GetHtmlElementOffset(mainFrame, "#loginIframe") //fmt.Printf("#loginIframe坐标:left=%v,top=%v\n", login_frame_left, login_frame_top) // 获取frameElement id //for i := 0; i < len(frameIdentifiers); i++ { //identifier := frameIdentifiers[i] //fmt.Printf("frame-identifier=%v\n", identifier) // id=loginIframe //frame := browser.GetFrameByIdent(identifier) //if !frame.IsValid() { // fmt.Printf("IsValid fail.\n") // continue //} /*c := ` var id_ = ""; var frame = window.frameElement; //Get <iframe> element of the window if (frame) { //if (typeof frameElement_.id !== "undefined" && frameElement_.id !== null) { // some code here if (typeof frame.id !== "undefined" && frame.id !== null) { id_ = frame.id; } } app.cefResult(id_); `*/ //frame_id := frame.ExecuteJavaScriptWithResult(c) //fmt.Printf("frame_id=%v\n", frame_id) //if frame_id == "loginIframe" { fmt.Printf("找到登录界面") navigator.InjectFocus(browser, true) fmt.Printf("开始登录支付宝...\n") fmt.Printf("获取账号输入框按钮坐标\n") left, top := navigator.GetHtmlElementOffset(frame, "#J-input-user") fmt.Printf("输入框坐标:left=%v,top=%v\n", left, top) x := left + 10 y := top + 10 fmt.Printf("点击账号输入框 x=%v,y=%v\n", x, y) hWnd := win.HWND(browser.GetWindowHandle()) fmt.Printf("hWnd=%v\n", hWnd) navigator.MouseClick(hWnd, x, y) fmt.Printf("点击账号输入框\n") time.Sleep(2 * time.Second) navigator.InjectKeyPress(browser, "*****@*****.**") fmt.Printf("输入完成.\n") time.Sleep(5 * time.Second) fmt.Printf("点击密码输入框\n") left, top = navigator.GetHtmlElementOffset(frame, "#password_input") x = left + 10 + 40 y = top + 10 fmt.Printf("输入框坐标:left=%v,top=%v\n", x, y) navigator.InjectMouseClick(browser, x, y) fmt.Printf("点击账号输入框\n") time.Sleep(3 * time.Second) navigator.InjectKeyPress(browser, "1") fmt.Printf("输入完成.\n") time.Sleep(5 * time.Second) fmt.Printf("失去焦点1.\n") navigator.InjectFocus(browser, false) fmt.Printf("失去焦点2.\n") //} //} }
// 模拟输入 func win_emuInput(browser *cef.Browser, message *cef.CefProcessMessage) interface{} { rootHandle := browser.GetRootWindowHandle() url := message.GetArgumentList().GetString(1) fmt.Printf("url=%v\n", url) w, ok := windowHolders[url] if ok { fmt.Printf("找到窗口\n") b, o := cef.BrowserByHandle(unsafe.Pointer(w)) if o { inputText := message.GetArgumentList().GetString(2) fmt.Printf("inputText=%v\n", inputText) rootHandle = b.GetRootWindowHandle() //win.ShowWindow(win.HWND(rootHandle), win.SW_MAXIMIZE) // 模拟鼠标 var pt win.POINT pt.X = int32(0) // This is your click coordinates pt.Y = int32(0) hWnd := win.HWND(rootHandle) go func() { win.SetForegroundWindow(hWnd) time.Sleep(5 * time.Second) win.ClientToScreen(hWnd, &pt) fmt.Printf("ClientToScreen X=%v Y=%v\n", pt.X, pt.Y) //sendinput.SendString(inputText) ss := strings.Split(inputText, "") for i := 0; i < len(ss); i++ { c := ss[i] cc := []rune(c)[0] keyCode := *syscall.StringToUTF16Ptr(c) shift := false if cc >= 'a' && cc <= 'z' { fmt.Printf("小写\n") c = strings.ToUpper(c) keyCode = *syscall.StringToUTF16Ptr(c) } if cc >= 'A' && cc <= 'Z' { fmt.Printf("大写\n") shift = true } index := strings.Index(spec_chars, c) if index >= 0 { fmt.Printf("特殊字符1 c=%v index=%v\n", c, index) ss := strings.Split(base_chars, "") fmt.Printf("ss=%v\n", ss) c = ss[index] fmt.Printf("特殊字符2 c=%v\n", c) keyCode = *syscall.StringToUTF16Ptr(c) shift = true } switch { case c == ";": keyCode = win.VK_OEM_1 case c == ":": keyCode = win.VK_OEM_1 shift = true case c == "=": keyCode = win.VK_OEM_PLUS case c == "+": keyCode = win.VK_OEM_PLUS shift = true case c == "-": keyCode = win.VK_OEM_MINUS case c == "_": keyCode = win.VK_OEM_MINUS shift = true case c == ",": keyCode = win.VK_OEM_COMMA case c == "<": keyCode = win.VK_OEM_COMMA shift = true case c == ".": keyCode = win.VK_OEM_PERIOD case c == ">": keyCode = win.VK_OEM_PERIOD shift = true case c == "/": keyCode = win.VK_OEM_2 case c == "?": keyCode = win.VK_OEM_2 shift = true case c == "`": keyCode = win.VK_OEM_3 case c == "~": keyCode = win.VK_OEM_3 shift = true case c == "[": keyCode = win.VK_OEM_4 case c == "{": keyCode = win.VK_OEM_4 shift = true case c == `\`: keyCode = win.VK_OEM_5 case c == "|": keyCode = win.VK_OEM_5 shift = true case c == "]": keyCode = win.VK_OEM_6 case c == "}": keyCode = win.VK_OEM_6 shift = true case c == "'": keyCode = win.VK_OEM_7 case c == `"`: keyCode = win.VK_OEM_7 shift = true } fmt.Printf("输入:%v %v\n", keyCode, shift) sendinput.KeyPress(keyCode, shift) } //hWnd = win.WindowFromPoint(pt) //PostMessage(edit, WM_KEYDOWN, ...); //PostMessage(edit, WM_CHAR, ...); //PostMessage(edit, WM_KEYUP, ...); //win.SendMessage(hWnd, win.WM_KEYDOWN, 0, 0) /* lParam := uintptr(win.MAKELONG(uint16(pt.X), uint16(pt.Y))) win.SendMessage(hWnd, win.WM_LBUTTONDOWN, 0, lParam) //WM_LBUTTONDOWN //time.Sleep(50 * time.Millisecond) win.SendMessage(hWnd, win.WM_LBUTTONUP, 0, lParam) */ fmt.Printf("输入") }() //hWnd = win.WindowFromPoint(pt) } } else { fmt.Printf("找不到窗口\n") } return 0 }
func doIt(browser *cef.Browser) { // 1.加载百度首页 browser.LoadURL("http://www.baidu.com") time.Sleep(5 * time.Second) url := browser.GetURL() fmt.Printf("Url=%v\n", url) src := browser.GetSource() ioutil.WriteFile("a.txt", []byte(src), 0644) fmt.Printf("src=%v\n", len(src)) time.Sleep(200 * time.Second) //js := `function() { return "1"; }();` //browser.Eval(js) // 执行Js //browser.ExecuteJavaScript(`app.cefResult("a");`, "", 1) result := browser.ExecuteJavaScriptWithResult(`(function() { cef.setResult("13"); })();`) fmt.Printf("Eval Js. result=%v\n", result) //browser.InjectJs("js/jquery.min.js") // http://stackoverflow.com/questions/12605315/html5-getimagedata-without-canvas // http://blog.csdn.net/hursing/article/details/12868109 //browser.InjectJs("js/imageutil.js") //browser.InjectJs("js/html2canvas.js") /*filename := browser.ExecuteJavaScriptWithResult(` (function() { var img = $("#lg img")[0]; var imageData = captureImage(img); var filename = app.renderImage(imageData.data, imageData.width, imageData.height); return filename; })();`) fmt.Printf("filename: %v\n", filename) */ /* filename := browser.ExecuteJavaScriptWithResult(` html2canvas(document.body, { onrendered: function(canvas) { var imageData = canvasToData(canvas); var filename = app.renderImage(imageData.data, imageData.width, imageData.height); app.cefResult(filename); } }); `) fmt.Printf("filename: %v\n", filename) */ /*c := `var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); var img = $("#lg img")[0]; //var img = document.getElementById('tulip'); canvas.width = img.width; canvas.height = img.height; context.drawImage(img, 0, 0);//, img.width, img.height var imageData = context.getImageData(0, 0, img.width, img.height); //alert('3:' + img.width + '/' + img.height + '/' + imageData.data.length); var txtFile = ''; //console.log('image.data.length=' + imageData.data.length); var dataArray = new Array(imageData.data.length); for (var i = 0; i < dataArray.length; i++) { dataArray[i] = imageData.data[i]; } var strImageData = dataArray.toString(); //console.log("strImageData=" + strImageData); var filename = app.set_image_data(strImageData, img.width, img.height); alert(filename); //alert('4');` //c := `var canvas = $("#lg img");var context = canvas[0].getContext("2d");alert(context);` */ // 获取图片 //browser.ExecuteJavaScript(c, "", 1) // 发送进程消息 //browser.SendProcessMessageTest() // 模拟点击 code := ` function getOffset( el ) { var _x = 0; var _y = 0; while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) { _x += el.offsetLeft - el.scrollLeft; _y += el.offsetTop - el.scrollTop; // chrome/safari //if ($.browser.webkit) { el = el.parentNode; //} else { // firefox/IE //el = el.offsetParent; //} } return { left: _x, top: _y }; } var e = document.querySelector("input[name=wd]"); var offset = getOffset(e); cef.setResult(offset.left + "," + offset.top); ` strOffset := browser.ExecuteJavaScriptWithResult(code) fmt.Printf("strOffset:%v\n", strOffset) ss := strings.Split(strOffset, ",") //h := browser.GetHost() if len(ss) == 2 { x, _ := strconv.Atoi(ss[0]) y, _ := strconv.Atoi(ss[1]) // 执行点击 fmt.Printf("x=%v,y=%v\n", x, y) navigator.InjectMouseClick(browser, x+4, y+4) } fmt.Printf("点击完毕\n") time.Sleep(3 * time.Second) //h.SetFocus(true) // 模拟输入 fmt.Printf("模拟输入\n") var text = "Abcdd@ffaff" navigator.InjectKeyPress(browser, text) time.Sleep(10 * time.Second) }