Link to home
Start Free TrialLog in
Avatar of LilMoke
LilMoke

asked on

Intercept Web Page before Browser in MFC...

Hello,

I have an MFC browser app which uses a CHtmlView.  I would like to intercept the page before it is displayed, check for specific text on the pagem if it exists redirect to a differnt page, if it doesn't display the page like normal.

What is the best way to go about doing this?   Does anyoe have any snippets showing how this can be done?

Your helkp is greatly appreciated.

Thanks,
Tony
Avatar of KurtVon
KurtVon

The OnDocumentComplete notification I beleieve is sent right after the document is downloaded, but before it is displayed.  You can ask what text it contains using GetSource and parse through it that way.  Use Navigate or Navigate2 to open the new location (or use the GetHtmlDocument to get the COM interface and set the text directly.

Hope this helps.
Avatar of LilMoke

ASKER

OnDocumentComplete does not appear to be called before the document is displayed... I have tried this and it appears to display and then get called.   Ahhh, this is why I ask the question... anyway, do you have any thoughts on this?  I have tried many things and cannot sem to find out how to get notified before the document is displayed.

Thanks,
Tony


Hmm, I definately was able to access the data before displaying programmatically.  I could have sworn it was in OnDocumentComplete, but I know it can be done.  I did this in a program that used HTML to define an interface, and after the document was loaded replaced "variables" with the values the program generated.  Unfortunately, I no longer have access to that code since it was at my last job.

Okay, a quick test program shows that the notifications come like this:

OnDownloadComplete
OnNavgateComplete
<show page>
OnDownloadComplete
OnDownloadComplete
OnDownloadComplete
OnDownloadComplete
OnDownloadComplete
OnDownloadComplete
OnDocumentComplete


The extra OnDownloadCompletes probably correspond to the images on the page.  So I'd say either go with OnNavigateComplete, or keep track of the page downloaded and make sure only the first OnDownloadComplete gets processed.

Hope this helps.
Avatar of LilMoke

ASKER

Ok, in my OnNavigateComplete, I put the following code and it did not seem to work:

{
      this->Stop();
      this->Navigate2("http://www.myweb.com");

//      CHtmlView::OnNavigateComplete2(strURL);
}

it brought up the page I entered into the address box first then quickly switched to myweb.com

Hmm, any ideas?  Did you do that in a test app?  Can you sen it to me, or put the above in your OnNavigateCOmplete?

Thanks,
Tony
Avatar of LilMoke

ASKER

Also, this is not exactly what my code is... the stop and navigate are actually conditional... menaing they only happen once based on a condition.

WARNING, if you put that in as is, you will loop forever in OnNavigateComplete

Odd, it works perfectly on my machine.  What version of IE are you using?  Here's the code as I wrote it:

void CHtmltestView::OnNavigateComplete2(LPCTSTR strURL)
{
    int npos = strlen(strURL);
    if (stricmp(&(strURL[npos - 4]), "org/") == 0)
    {
        Stop();
        Navigate2("http://www.whitehouse.gov");
    }
      
    CHtmlView::OnNavigateComplete2(strURL);
}

When I send the browser to a website ending in .org there is not so much as a flicker before the whitehouse page comes up.  I tried putting in some dialog boxes to slow the whole process down (even a Sleep(1000) between teh Stop and Navigate2 calls), but the drawing really doesn't seem to continue while this redirect takes place.
Avatar of LilMoke

ASKER

I have IE6 I must have some other logic wrong, I will check my app further, can you sen me your sample app?

Thanks for your help with this,
Tony
The only modified part from a default application is the CHtmlView, which is subclassed as CHtmltestView.  Here is the entire cpp file, including (in case it really matters) the debugging stuff that doesn't do anything anymore (the Edit|Copy command was used for testing the navigation):

=================
// htmltestView.cpp : implementation of the CHtmltestView class
//

#include "stdafx.h"
#include "htmltest.h"

#include "htmltestDoc.h"
#include "htmltestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHtmltestView

IMPLEMENT_DYNCREATE(CHtmltestView, CHtmlView)

BEGIN_MESSAGE_MAP(CHtmltestView, CHtmlView)
      //{{AFX_MSG_MAP(CHtmltestView)
      ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
      //}}AFX_MSG_MAP
      // Standard printing commands
      ON_COMMAND(ID_FILE_PRINT, CHtmlView::OnFilePrint)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHtmltestView construction/destruction

CHtmltestView::CHtmltestView()
: m_Dialog()
{
      // TODO: add construction code here
    TRACE("Here\n");
}

CHtmltestView::~CHtmltestView()
{
}

BOOL CHtmltestView::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs

    return CHtmlView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHtmltestView drawing

void CHtmltestView::OnDraw(CDC* pDC)
{
    CHtmltestDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
}

void CHtmltestView::OnInitialUpdate()
{
    CHtmlView::OnInitialUpdate();

    // TODO: This code navigates to a popular spot on the web.
    //  change the code to go where you'd like.
    Navigate2(_T("http://www.slashdot.org"),NULL,NULL);
}

/////////////////////////////////////////////////////////////////////////////
// CHtmltestView printing


/////////////////////////////////////////////////////////////////////////////
// CHtmltestView diagnostics

#ifdef _DEBUG
void CHtmltestView::AssertValid() const
{
    CHtmlView::AssertValid();
}

void CHtmltestView::Dump(CDumpContext& dc) const
{
    CHtmlView::Dump(dc);
}

CHtmltestDoc* CHtmltestView::GetDocument() // non-debug version is inline
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHtmltestDoc)));
    return (CHtmltestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHtmltestView message handlers

void CHtmltestView::OnEditCopy()
{
    Navigate("http://slashdot.org");
}


void CHtmltestView::OnDocumentComplete(LPCTSTR lpszURL)
{
    CHtmlView::OnDocumentComplete(lpszURL);
}

void CHtmltestView::OnDownloadComplete()
{
    CHtmlView::OnDownloadComplete();
}

void CHtmltestView::OnNavigateComplete2(LPCTSTR strURL)
{
    int npos = strlen(strURL);
    if (stricmp(&(strURL[npos - 4]), "org/") == 0)
    {
        Stop();
        Navigate2("http://www.whitehouse.gov");
    }
      
    CHtmlView::OnNavigateComplete2(strURL);
}
Avatar of LilMoke

ASKER

ok, my stupid!!!   I have a CHtmlMyView which I subclass and an overridden function in their was my culprit.  Turns out I do not need to make a parent class anyway.  I removed the method and now my code runs fine.  Thanks for your help!!  

Tony
Avatar of LilMoke

ASKER

Hello,

I have one more question about a small side effect from this approach.

I went to a site that had the following line in the html script:

<body bgcolor="#FFFFFF" vlink="#0000FF" onunload=bye()>

it turns out there is no bye() so it generates a scipt error.  When the script error dialog appears the page also display behind it.

In addition, this DOES NOT happen with internet explorer, but it does when I run my app of the MFCIE sample from Microsoft.

So the question:  Is there any way to disable the script errors dialog from appearing?  Or, do you have any ideas what I can do to prevent this from happening?

Thanks,
Tony
ASKER CERTIFIED SOLUTION
Avatar of KurtVon
KurtVon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LilMoke

ASKER

Well, that did work... thanks... but you are right, it is curioous why IE does not error out.  It definetly doesn't though.

In any case, this work around seems to mask the problem... I don't anticipate too many sites like that in my application anyway.

Thanks, and I accept (and welcome) your responses and help!!!!!