func emuInput(browser *cef.Browser) { fmt.Printf("emuInput\n") navigator.InjectKeyPress(browser, "k") }
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 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) }