Solved

Intercept Web Page before Browser in MFC...

Posted on 2003-11-11
12
735 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:LilMoke
  • 7
  • 5
12 Comments
 
LVL 11

Expert Comment

by:KurtVon
ID: 9732054
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.
0
 

Author Comment

by:LilMoke
ID: 9736703
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


0
 
LVL 11

Expert Comment

by:KurtVon
ID: 9739564
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.
0
 

Author Comment

by:LilMoke
ID: 9739633
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
0
 

Author Comment

by:LilMoke
ID: 9739850
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

0
 
LVL 11

Expert Comment

by:KurtVon
ID: 9739906
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:LilMoke
ID: 9739978
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
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 9740132
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);
}
0
 

Author Comment

by:LilMoke
ID: 9740299
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
0
 

Author Comment

by:LilMoke
ID: 9741684
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
0
 
LVL 11

Accepted Solution

by:
KurtVon earned 250 total points
ID: 9742043
You could try a SetSilent(TRUE) in OnBeforeNavigate2 and SetSilent(FALSE) after checking the redirection.  That doesn't really solve the problem, though, just keeps the dialog from popping up.

What I am surprised by is that IE works with a page like that.  "Bye" is not a built in function (to the best of my knowledge) so I can't see how there wouldn't be an error when the script exits at least.   Hmph.
0
 

Author Comment

by:LilMoke
ID: 9742176
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!!!!!

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now