2012-10-18 4 views
2

죄송합니다. 당신은 나에게 물건을 설명해야합니다, 나는 새로운 물을 밟고 있어요. 나는 자바에서 배경을 가지고 있지만 대부분 PHP, 자바 스크립트. 최상위 프레임 감지하기 BHO

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 페이지로드시 여러 웹 요청과 팝업으로 끝납니다.

CComQIPtr<IServiceProvider> pServiceProvider(pUnkSite); 
if (!pServiceProvider) { 
    return E_FAIL; 
} 

pServiceProvider->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (LPVOID*)&m_WebBrowser.p); 
if (!m_WebBrowser) { 
    return E_FAIL; 
} 

이이 저장됩니다 :

답변

0

가장 쉬운 방법은 (C 번호로 변환하는 C의 예를 ++하지만 간단합니다)를 SetSite 방법에서 개체 속성에 웹 브라우저 객체 (IWebBrowser2)를 저장하는 것입니다 개체 구성원 m_WebBrowser의 브라우저 포인터. 그럼 당신은 OnDocumentCompletepDisp 매개 변수를 비교할 수 있습니다 응답 마태 복음에 대한

CComQIPtr<IWebBrowser2> webBrowser(pDisp); 
if (webBrowser == m_WebBrowser) { 
    // This is the top-level page. 
} 
+0

감사합니다. CComQIPtr을 어떻게 사용합니까? 어떤 어셈블리를 참조해야합니까? – thefoyer

+0

http://msdn.microsoft.com/en-us/library/wc177dxw(v=vs.80).aspx를 참조하십시오. atlcomcli.h를 포함하면됩니다. –

+0

아,하지만 C#을 사용하고 있다면 그냥 정상적인 캐스트를 사용할 수 있습니다. –