对不起我的无知。你必须向我解释事情,我正在涉足新领域。我有一些 JAVA 背景,但主要是 php、javascript。
http://www.codeproject.com/Articles/19971/How-to-attach-to-Browser-Helper-Object-BHO-with-C
我对这篇文章进行了一些自己的修改,我的问题是,我如何检测网页的“顶级框架”,即父文档。OnDocumentComplete
当页面上的任何 iframe 也完成时,我执行的任何代码都将运行。
我的功能和我实施的解决方案实际上并没有产生正确的结果。
public class BHO:IObjectWithSite
{
WebBrowser webBrowser;
HTMLDocument document;
public void OnDocumentComplete(object pDisp, ref object URL)
{
document = (HTMLDocument)webBrowser.Document;
string href = document.location.href;
//get top level page
if (href == URL.ToString())
{
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://mysite.com");
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
byte[] buffer = Encoding.ASCII.GetBytes("string");
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
// Prepare web request and send the data.
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
StreamReader streamResponse = new StreamReader(WebResp.GetResponseStream(), true);
string Response = streamResponse.ReadToEnd();
Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(Response);
string active = json["active"].ToString();
//print to screen
System.Windows.Forms.MessageBox.Show(active, "Title");
}
}
检查document.location.href
匹配是否URL
在大多数情况下有效,但不能保证。所以结果是我最终在 1 个页面加载时出现了多个 web 请求和弹出窗口。