这个方法简直太棒了,我们可以用网站自己的脚本来完成我们想要完成的事情。。。
这里主要是说对脚本变量值的获取和设置,元素设置太简单了,不多说,这个可以用来改变脚本流程,比如里面可能有个计时器,我们要改变这个计时器的值就可以使用这中方法了
转自http://topic.youkuaiyun.com/u/20080117/23/27d0ec6e-ffed-4441-95c4-bae521659321.html
/项目中添加Micrsoft.mshtml引用
<html>
<head>
<title>demo</title>
<mce:script language="JavaScript" type="text/javascript"><!--
var testText = "Zswang";
function ShowMessage(AText)
{
alert(testText);
alert(AText);
}
// --></mce:script>
</head>
</html>
using mshtml;
using System.Reflection;
private void button1_Click(object sender, EventArgs e)
{
IHTMLDocument2 vDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow;
Type vWindowType = vWindow.GetType();
object testText = vWindowType.InvokeMember("testText",
BindingFlags.GetProperty, null, vWindow, new object[] { }); // 读取
Console.WriteLine(testText);
vWindowType.InvokeMember("testText",
BindingFlags.SetProperty, null, vWindow, new object[] { "Zswang 路过" }); // 设置
vWindowType.InvokeMember("ShowMessage",
BindingFlags.InvokeMethod, null, vWindow, new object[] { 12345 }); // 执行方法
}
private void button2_Click(object sender, EventArgs e)
{
IHTMLDocument2 vDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLWindow2 vWindow = (IHTMLWindow2)vDocument.parentWindow;
vWindow.execScript("ShowMessage(67890);", "JavaScript"); // 执行脚本
}