#ifndef _BROWSER_H
#define _BROWSER_H
class CBrowser
{
public:
CBrowser( void );
~CBrowser( void );
public:
void ReadHTMLTable( void );
void InitInstance( void );
void EditForm( void );
protected:
void EnumFrame( IHTMLDocument2 *spIHTMLDocument2 );
private:
CComQIPtr < IHTMLDocument2 > m_spDocument2;
IHTMLFormElement *SelectedForm( IHTMLElementCollection *spElementCollectionForm, long lFormIndex );
void EnumForm( IHTMLDocument2 *spIHTMLDocument2 );
void PutElement( IHTMLFormElement *spFormElement, long lIndex, const char* pszBuffer );
void EnumTable( IHTMLDocument2 *spIHTMLDocument2 );
void SelectedRow(IHTMLElementCollection *spElementCollectionTable, long lRowIndex);
void SelectedTable( IHTMLElementCollection *spTableRow, long lTableIndex );
};
void CBrowser::ReadHTMLTable( void )
{
EnumTable( m_spDocument2 );
}
void CBrowser::EnumTable( IHTMLDocument2 *spIHTMLDocument2 )
{
if( !spIHTMLDocument2 ){ return; }
EnumFrame( spIHTMLDocument2 );
HRESULT hr;
long lRow = 0;
long lElementCursor = 0;
long lTableRow = 0;
CComBSTR bstrTitle;
spIHTMLDocument2->get_title( &bstrTitle );
USES_CONVERSION;
cout << OLE2CT( bstrTitle ) << endl;
CComQIPtr< IHTMLElementCollection > spElementCollectionTable;
hr = spIHTMLDocument2->get_all( &spElementCollectionTable );
if( FAILED(hr) ){ return; }
long lAllElementCount = 0;
hr = spElementCollectionTable->get_length( &lAllElementCount );
for( int lIndex=0; lIndex<lAllElementCount; lIndex++ ) {
SelectedTable( spElementCollectionTable, lIndex);
}
}
void CBrowser::SelectedTable( IHTMLElementCollection *spElementCollectionTable, long lTableIndex )
{
HRESULT hr;
CComPtr< IDispatch > spDisp;
hr = spElementCollectionTable->item( CComVariant(lTableIndex),CComVariant(), &spDisp );
if( FAILED(hr) ){ return; }
CComQIPtr< IHTMLTable > spTable = spDisp;
if( !spTable ){ return; }
CComPtr< IHTMLElementCollection > spTableRow;
hr = spTable->get_rows( &spTableRow );
if( !spTableRow ){ return; }
long lRowCount = 0;
spTableRow->get_length( &lRowCount );
for( long lIndex=0; lIndex<lRowCount; lIndex++)
{
SelectedRow( spTableRow, lIndex );
}
}
void CBrowser::SelectedRow( IHTMLElementCollection *spTableRow, long lRowIndex )
{
long lRowCount = 0;
spTableRow->get_length( &lRowCount );
// cout << "Row Count: " << lRowCount << endl;
if( lRowIndex < 0 || lRowIndex > lRowCount - 1) { return; }
HRESULT hr;
VARIANT vIndexRow;
vIndexRow.vt = VT_UINT;
vIndexRow.lVal = lRowIndex;
VARIANT var0 = { 0 };
CComPtr< IDispatch > spDisp;
if( SUCCEEDED( hr = spTableRow->item( vIndexRow, var0, &spDisp ) ))
{
CComPtr<IHTMLElement> spVerboseElement;
if( SUCCEEDED( hr = spDisp->QueryInterface( IID_IHTMLElement, (LPVOID*)&spVerboseElement) ))
{
BSTR bstrTag;
long lIndex;
CComPtr< IHTMLElement > spRow = spVerboseElement;
spVerboseElement->get_innerText( &bstrTag );
spVerboseElement->get_sourceIndex( &lIndex );
CString csTemp(bstrTag);
cout << (LPSTR)(LPCTSTR)csTemp << endl;
}
}
}
void CBrowser::InitInstance( void )
{
CComPtr< IShellWindows > spShellWin;
HRESULT hr = spShellWin.CoCreateInstance( CLSID_ShellWindows );
if( FAILED(hr) ){ return; }
long lCount = 0;
spShellWin->get_Count( &lCount );
cout << lCount << endl;
if( 0 == lCount ){ return; }
for( long lIndex=0; lIndex<lCount; lIndex++ )
{
CComPtr< IDispatch > spDispIE;
hr = spShellWin->Item( CComVariant(lIndex), &spDispIE);
if( FAILED(hr) ){ continue; }
CComQIPtr< IWebBrowser2 > spBrowser = spDispIE;
if( !spBrowser ){ continue; }
CComPtr< IDispatch > spDispDoc;
hr = spBrowser->get_Document( &spDispDoc );
if( FAILED(hr) ){ continue; }
CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc;
if( !spDocument2 ){ continue; }
m_spDocument2 = spDocument2;
}
}
void CBrowser::EnumFrame( IHTMLDocument2 *spIHTMLDocument2 )
{
if( !spIHTMLDocument2 ){ return; }
HRESULT hr;
CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
spIHTMLDocument2->get_frames( &spFramesCollection2 );
long lFrameCount = 0;
hr = spFramesCollection2->get_length( &lFrameCount );
if( FAILED( hr ) || 0 == lFrameCount ){ return; }
for( long lIndex=0; lIndex<lFrameCount; lIndex++)
{
CComVariant vDispWin2;
hr = spFramesCollection2->item( &CComVariant( lIndex ), &vDispWin2 );
if( FAILED(hr) ){ continue; }
CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal;
if( !spWin2 ){ continue; }
CComPtr< IHTMLDocument2 > spDoc2;
spWin2->get_document( &spDoc2 );
EnumForm( spDoc2 );
}
}
void CBrowser::EnumForm( IHTMLDocument2 *spIHTMLDocument2 )
{
if( !spIHTMLDocument2 ){ return; }
EnumFrame( spIHTMLDocument2 );
HRESULT hr;
USES_CONVERSION;
CComQIPtr< IHTMLElementCollection > spElementCollectionForm;
hr = spIHTMLDocument2->get_forms( &spElementCollectionForm );
if( FAILED(hr) ){ return; }
long lFormCount = 0;
hr = spElementCollectionForm->get_length( &lFormCount );
if( FAILED(hr) ){ return; }
SelectedForm( spElementCollectionForm, 0);
}
IHTMLFormElement* CBrowser::SelectedForm( IHTMLElementCollection *spElementCollectionForm, long lFormIndex )
{
HRESULT hr;
CComPtr< IDispatch > spDisp;
hr = spElementCollectionForm->item(CComVariant(0), CComVariant(), &spDisp);
if( FAILED(hr) ){ return NULL; }
CComQIPtr< IHTMLFormElement > spFormElement = spDisp;
long lElemCount = 0;
hr = spFormElement->get_length( &lElemCount );
if( FAILED(hr) ){ return NULL; }
PutElement( spFormElement, 9, "miomio");
return spFormElement;
}
void CBrowser::PutElement( IHTMLFormElement *spFormElement, long lIndex, const char* pszBuffer )
{
HRESULT hr;
CComDispatchDriver spInputElement;
hr = spFormElement->item( CComVariant( lIndex ), CComVariant(), &spInputElement );
if( FAILED(hr) ){ return; }
CComVariant vName;
CComVariant vVal;
CComVariant vType;
hr = spInputElement.GetPropertyByName(L"name", &vName);
if( FAILED(hr) ){ return; }
hr = spInputElement.GetPropertyByName(L"value", &vVal);
if( FAILED(hr) ){ return; }
hr = spInputElement.GetPropertyByName(L"type", &vType);
if( FAILED(hr) ){ return; }
const char* pszTempString = pszBuffer;
cout<< pszTempString << endl;
CComVariant vTestString( pszTempString );
hr = spInputElement.PutPropertyByName(L"value", &vTestString);
if( FAILED(hr) ){ return; }
}
void CBrowser::EditForm( void )
{
EnumForm( m_spDocument2 );
}
CBrowser::~CBrowser( void )
{
}
CBrowser::CBrowser( void )
{
}
#endif //_BROWSER_H
#define _BROWSER_H
class CBrowser
{
public:
CBrowser( void );
~CBrowser( void );
public:
void ReadHTMLTable( void );
void InitInstance( void );
void EditForm( void );
protected:
void EnumFrame( IHTMLDocument2 *spIHTMLDocument2 );
private:
CComQIPtr < IHTMLDocument2 > m_spDocument2;
IHTMLFormElement *SelectedForm( IHTMLElementCollection *spElementCollectionForm, long lFormIndex );
void EnumForm( IHTMLDocument2 *spIHTMLDocument2 );
void PutElement( IHTMLFormElement *spFormElement, long lIndex, const char* pszBuffer );
void EnumTable( IHTMLDocument2 *spIHTMLDocument2 );
void SelectedRow(IHTMLElementCollection *spElementCollectionTable, long lRowIndex);
void SelectedTable( IHTMLElementCollection *spTableRow, long lTableIndex );
};
void CBrowser::ReadHTMLTable( void )
{
EnumTable( m_spDocument2 );
}
void CBrowser::EnumTable( IHTMLDocument2 *spIHTMLDocument2 )
{
if( !spIHTMLDocument2 ){ return; }
EnumFrame( spIHTMLDocument2 );
HRESULT hr;
long lRow = 0;
long lElementCursor = 0;
long lTableRow = 0;
CComBSTR bstrTitle;
spIHTMLDocument2->get_title( &bstrTitle );
USES_CONVERSION;
cout << OLE2CT( bstrTitle ) << endl;
CComQIPtr< IHTMLElementCollection > spElementCollectionTable;
hr = spIHTMLDocument2->get_all( &spElementCollectionTable );
if( FAILED(hr) ){ return; }
long lAllElementCount = 0;
hr = spElementCollectionTable->get_length( &lAllElementCount );
for( int lIndex=0; lIndex<lAllElementCount; lIndex++ ) {
SelectedTable( spElementCollectionTable, lIndex);
}
}
void CBrowser::SelectedTable( IHTMLElementCollection *spElementCollectionTable, long lTableIndex )
{
HRESULT hr;
CComPtr< IDispatch > spDisp;
hr = spElementCollectionTable->item( CComVariant(lTableIndex),CComVariant(), &spDisp );
if( FAILED(hr) ){ return; }
CComQIPtr< IHTMLTable > spTable = spDisp;
if( !spTable ){ return; }
CComPtr< IHTMLElementCollection > spTableRow;
hr = spTable->get_rows( &spTableRow );
if( !spTableRow ){ return; }
long lRowCount = 0;
spTableRow->get_length( &lRowCount );
for( long lIndex=0; lIndex<lRowCount; lIndex++)
{
SelectedRow( spTableRow, lIndex );
}
}
void CBrowser::SelectedRow( IHTMLElementCollection *spTableRow, long lRowIndex )
{
long lRowCount = 0;
spTableRow->get_length( &lRowCount );
// cout << "Row Count: " << lRowCount << endl;
if( lRowIndex < 0 || lRowIndex > lRowCount - 1) { return; }
HRESULT hr;
VARIANT vIndexRow;
vIndexRow.vt = VT_UINT;
vIndexRow.lVal = lRowIndex;
VARIANT var0 = { 0 };
CComPtr< IDispatch > spDisp;
if( SUCCEEDED( hr = spTableRow->item( vIndexRow, var0, &spDisp ) ))
{
CComPtr<IHTMLElement> spVerboseElement;
if( SUCCEEDED( hr = spDisp->QueryInterface( IID_IHTMLElement, (LPVOID*)&spVerboseElement) ))
{
BSTR bstrTag;
long lIndex;
CComPtr< IHTMLElement > spRow = spVerboseElement;
spVerboseElement->get_innerText( &bstrTag );
spVerboseElement->get_sourceIndex( &lIndex );
CString csTemp(bstrTag);
cout << (LPSTR)(LPCTSTR)csTemp << endl;
}
}
}
void CBrowser::InitInstance( void )
{
CComPtr< IShellWindows > spShellWin;
HRESULT hr = spShellWin.CoCreateInstance( CLSID_ShellWindows );
if( FAILED(hr) ){ return; }
long lCount = 0;
spShellWin->get_Count( &lCount );
cout << lCount << endl;
if( 0 == lCount ){ return; }
for( long lIndex=0; lIndex<lCount; lIndex++ )
{
CComPtr< IDispatch > spDispIE;
hr = spShellWin->Item( CComVariant(lIndex), &spDispIE);
if( FAILED(hr) ){ continue; }
CComQIPtr< IWebBrowser2 > spBrowser = spDispIE;
if( !spBrowser ){ continue; }
CComPtr< IDispatch > spDispDoc;
hr = spBrowser->get_Document( &spDispDoc );
if( FAILED(hr) ){ continue; }
CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc;
if( !spDocument2 ){ continue; }
m_spDocument2 = spDocument2;
}
}
void CBrowser::EnumFrame( IHTMLDocument2 *spIHTMLDocument2 )
{
if( !spIHTMLDocument2 ){ return; }
HRESULT hr;
CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
spIHTMLDocument2->get_frames( &spFramesCollection2 );
long lFrameCount = 0;
hr = spFramesCollection2->get_length( &lFrameCount );
if( FAILED( hr ) || 0 == lFrameCount ){ return; }
for( long lIndex=0; lIndex<lFrameCount; lIndex++)
{
CComVariant vDispWin2;
hr = spFramesCollection2->item( &CComVariant( lIndex ), &vDispWin2 );
if( FAILED(hr) ){ continue; }
CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal;
if( !spWin2 ){ continue; }
CComPtr< IHTMLDocument2 > spDoc2;
spWin2->get_document( &spDoc2 );
EnumForm( spDoc2 );
}
}
void CBrowser::EnumForm( IHTMLDocument2 *spIHTMLDocument2 )
{
if( !spIHTMLDocument2 ){ return; }
EnumFrame( spIHTMLDocument2 );
HRESULT hr;
USES_CONVERSION;
CComQIPtr< IHTMLElementCollection > spElementCollectionForm;
hr = spIHTMLDocument2->get_forms( &spElementCollectionForm );
if( FAILED(hr) ){ return; }
long lFormCount = 0;
hr = spElementCollectionForm->get_length( &lFormCount );
if( FAILED(hr) ){ return; }
SelectedForm( spElementCollectionForm, 0);
}
IHTMLFormElement* CBrowser::SelectedForm( IHTMLElementCollection *spElementCollectionForm, long lFormIndex )
{
HRESULT hr;
CComPtr< IDispatch > spDisp;
hr = spElementCollectionForm->item(CComVariant(0), CComVariant(), &spDisp);
if( FAILED(hr) ){ return NULL; }
CComQIPtr< IHTMLFormElement > spFormElement = spDisp;
long lElemCount = 0;
hr = spFormElement->get_length( &lElemCount );
if( FAILED(hr) ){ return NULL; }
PutElement( spFormElement, 9, "miomio");
return spFormElement;
}
void CBrowser::PutElement( IHTMLFormElement *spFormElement, long lIndex, const char* pszBuffer )
{
HRESULT hr;
CComDispatchDriver spInputElement;
hr = spFormElement->item( CComVariant( lIndex ), CComVariant(), &spInputElement );
if( FAILED(hr) ){ return; }
CComVariant vName;
CComVariant vVal;
CComVariant vType;
hr = spInputElement.GetPropertyByName(L"name", &vName);
if( FAILED(hr) ){ return; }
hr = spInputElement.GetPropertyByName(L"value", &vVal);
if( FAILED(hr) ){ return; }
hr = spInputElement.GetPropertyByName(L"type", &vType);
if( FAILED(hr) ){ return; }
const char* pszTempString = pszBuffer;
cout<< pszTempString << endl;
CComVariant vTestString( pszTempString );
hr = spInputElement.PutPropertyByName(L"value", &vTestString);
if( FAILED(hr) ){ return; }
}
void CBrowser::EditForm( void )
{
EnumForm( m_spDocument2 );
}
CBrowser::~CBrowser( void )
{
}
CBrowser::CBrowser( void )
{
}
#endif //_BROWSER_H