当前位置: 移动技术网 > IT编程>开发语言>c# > 使用C# 的webBrowser写模拟器时的javascript脚本调用问题

使用C# 的webBrowser写模拟器时的javascript脚本调用问题

2019年07月18日  | 移动技术网IT编程  | 我要评论

感觉很久不写模拟器代码了,昨天调试的时候碰了点壁,记录下来,避免大家再跟我犯同样的错误。

加入javascript脚本的地方:

htmlelement jselement = webbrowser1.document.createelement("script");
jselement.setattribute("type", "text/javascript");
jselement.setattribute("text", "showmeaction = function(e) { window.alert(e);}");
webbrowser1.document.body.appendchild(jselement);

调用的地方:

string[] args = new string[1];
args[0] = "hello element!";
webbrowser1.document.invokescript("showmeaction", args);

大家特别注意的是后面脚本调用的时候,只能出现函数名与参数值列表,不能增加其他内容,否则调用就不会成功。

使用的脚本代码:(这里的脚本代码模拟了鼠标移动的基础需求,通过js直接发鼠标事件的方式来实现自动机器人)

function createevent(eventname, ofsx, ofsy)
{ 
  var evt = document.createevent('mouseevents'); 
  evt.initmouseevent(eventname, true, false, null, 0, 0, 0, ofsx, ofsy, false, false, false, false, 0, null); 
  return evt; 
}
function moveelement(pxtomove)
{
var sliderknob = document.getelementsbyclassname("gt_slider_knob")[0]; 
var boxrect = sliderknob.getboundingclientrect();
var move = createevent('mousemove', boxrect.left + sliderknob.offsetleft + pxtomove, boxrect.top + sliderknob.offsettop); 
var down = createevent('mousedown', boxrect.left + sliderknob.offsetleft, boxrect.top + sliderknob.offsettop);
var up = createevent('mouseup'); 
sliderknob.dispatchevent(down); 
document.dispatchevent(move); 
sliderknob.dispatchevent(up); 
}

以上所述是小编给大家介绍的使用c# 的webbrowser写模拟器时的javascript脚本调用问题,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网