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
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
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
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.
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.
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::OnNavigateCompl ete2(strUR L);
}
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
{
this->Stop();
this->Navigate2("http://www.myweb.com");
// CHtmlView::OnNavigateCompl
}
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
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
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::OnNavigateC omplete2(L PCTSTR strURL)
{
int npos = strlen(strURL);
if (stricmp(&(strURL[npos - 4]), "org/") == 0)
{
Stop();
Navigate2("http://www.whitehouse.gov");
}
CHtmlView::OnNavigateCompl ete2(strUR L);
}
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.
void CHtmltestView::OnNavigateC
{
int npos = strlen(strURL);
if (stricmp(&(strURL[npos - 4]), "org/") == 0)
{
Stop();
Navigate2("http://www.whitehouse.gov");
}
CHtmlView::OnNavigateCompl
}
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.
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
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(CHtmlt estView, CHtmlView)
BEGIN_MESSAGE_MAP(CHtmltes tView, CHtmlView)
//{{AFX_MSG_MAP(CHtmltestV iew)
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::CHtmltestVi ew()
: m_Dialog()
{
// TODO: add construction code here
TRACE("Here\n");
}
CHtmltestView::~CHtmltestV iew()
{
}
BOOL CHtmltestView::PreCreateWi ndow(CREAT ESTRUCT& 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::OnInitialUp date()
{
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(CDumpC ontext& dc) const
{
CHtmlView::Dump(dc);
}
CHtmltestDoc* CHtmltestView::GetDocument () // non-debug version is inline
{
ASSERT(m_pDocument->IsKind Of(RUNTIME _CLASS(CHt mltestDoc) ));
return (CHtmltestDoc*)m_pDocument ;
}
#endif //_DEBUG
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CHtmltestView message handlers
void CHtmltestView::OnEditCopy( )
{
Navigate("http://slashdot.org");
}
void CHtmltestView::OnDocumentC omplete(LP CTSTR lpszURL)
{
CHtmlView::OnDocumentCompl ete(lpszUR L);
}
void CHtmltestView::OnDownloadC omplete()
{
CHtmlView::OnDownloadCompl ete();
}
void CHtmltestView::OnNavigateC omplete2(L PCTSTR strURL)
{
int npos = strlen(strURL);
if (stricmp(&(strURL[npos - 4]), "org/") == 0)
{
Stop();
Navigate2("http://www.whitehouse.gov");
}
CHtmlView::OnNavigateCompl ete2(strUR L);
}
=================
// 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(CHtmlt
BEGIN_MESSAGE_MAP(CHtmltes
//{{AFX_MSG_MAP(CHtmltestV
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::CHtmltestVi
: m_Dialog()
{
// TODO: add construction code here
TRACE("Here\n");
}
CHtmltestView::~CHtmltestV
{
}
BOOL CHtmltestView::PreCreateWi
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CHtmlView::PreCreateWindow
}
//////////////////////////
// CHtmltestView drawing
void CHtmltestView::OnDraw(CDC*
{
CHtmltestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CHtmltestView::OnInitialUp
{
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
{
CHtmlView::AssertValid();
}
void CHtmltestView::Dump(CDumpC
{
CHtmlView::Dump(dc);
}
CHtmltestDoc* CHtmltestView::GetDocument
{
ASSERT(m_pDocument->IsKind
return (CHtmltestDoc*)m_pDocument
}
#endif //_DEBUG
//////////////////////////
// CHtmltestView message handlers
void CHtmltestView::OnEditCopy(
{
Navigate("http://slashdot.org");
}
void CHtmltestView::OnDocumentC
{
CHtmlView::OnDocumentCompl
}
void CHtmltestView::OnDownloadC
{
CHtmlView::OnDownloadCompl
}
void CHtmltestView::OnNavigateC
{
int npos = strlen(strURL);
if (stricmp(&(strURL[npos - 4]), "org/") == 0)
{
Stop();
Navigate2("http://www.whitehouse.gov");
}
CHtmlView::OnNavigateCompl
}
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
Tony
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!!!!!
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!!!!!
Hope this helps.