下面是我的代码,哪里有错吗??? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using mshtml;namespace WindowsApplication7 { [ComVisible(true)] public partial class Form1 : Form { public Form1() { InitializeComponent(); this.webBrowser1.ObjectForScripting = this; } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { IHTMLWindow2 ihtmlWin = this.webBrowser1.Document.Window.DomWindow as IHTMLWindow2; StringBuilder strAlertBuilder = new StringBuilder(); strAlertBuilder.Append("var _alert=window.alert;\n"); strAlertBuilder.Append("window.alert=function(s){\n"); strAlertBuilder.Append("window.external.alertMessage(s);}"); ihtmlWin.execScript(strAlertBuilder.ToString(), "Javascript"); } private void alertMessage(string s) { this.textbox1.Text = s; } } }
private void alertMessage(string s) 改成 public void alertMessage(string s)
下面那段代码到是成功了。可是,当我把webBrowser1的URL换成别的网站时,还是捕获不到弹出窗口的内容啊。 我要访问的网页不是我自己写的。能捕获吗?using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;namespace WindowsApplication7 { [ComVisible(true)] public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { webBrowser1.Navigate(@"javascript: function alert(str) { window.external.alertMessage(str); }"); webBrowser1.ObjectForScripting = this; } public void alertMessage(string s) { this.textBox1.Text = s; } private void Form1_Load(object sender, EventArgs e) { webBrowser1.DocumentText = @" <html> <input type=""button"" value=""测试"" onclick=""alert('Zswang 路过');""> </html> "; } } }
②添加一个webBrowser;在构造函数中添加webBrowser.ObjectForScripting = this;
在类外写上[ComVisible(true)]
③写入webBrowser的DocumentCompleted事件,然后判断你用webBrowser转载的网页是不是弹消息框的网页如果是则执行
IHTMLWindow2 ihtmlWin = htmlDoc.Window.DomWindow as IHTMLWindow2;
StringBuilder strAlertBuilder = new StringBuilder();
strAlertBuilder.Append("var _alert=window.alert;\n");
strAlertBuilder.Append("window.alert=function(s){\n");
strAlertBuilder.Append("window.external.alertMessage(s);}");
④在你的类中加入一个public void alertMessage(string s)
{MessageBox.Show(s);}
试试那个s就是消息的内容;
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = @"
<html>
<input type=""button"" value=""测试"" onclick=""alert('Zswang 路过');"">
</html>
";
while (webBrowser1.IsBusy) Application.DoEvents();
} public void alertMessage(string s)
{
MessageBox.Show(s, "囧");
} private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate(@"javascript:
function alert(str)
{
window.external.alertMessage(str);
}");
webBrowser1.ObjectForScripting = this;
}
}
还要在第三步最后加上:ihtmlWin .execScript(strAlertBuilder.ToString(), "Javascript");
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using mshtml;namespace WindowsApplication7
{
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.webBrowser1.ObjectForScripting = this;
} private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
IHTMLWindow2 ihtmlWin = this.webBrowser1.Document.Window.DomWindow as IHTMLWindow2;
StringBuilder strAlertBuilder = new StringBuilder();
strAlertBuilder.Append("var _alert=window.alert;\n");
strAlertBuilder.Append("window.alert=function(s){\n");
strAlertBuilder.Append("window.external.alertMessage(s);}");
ihtmlWin.execScript(strAlertBuilder.ToString(), "Javascript");
} private void alertMessage(string s)
{
this.textbox1.Text = s;
}
}
}
改成
public void alertMessage(string s)
我要访问的网页不是我自己写的。能捕获吗?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsApplication7
{
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate(@"javascript:
function alert(str)
{
window.external.alertMessage(str);
}");
webBrowser1.ObjectForScripting = this;
} public void alertMessage(string s)
{
this.textBox1.Text = s;
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = @"
<html>
<input type=""button"" value=""测试"" onclick=""alert('Zswang 路过');"">
</html>
";
}
}
}