Skip to content

Instantly share code, notes, and snippets.

@kumatti1
Last active December 15, 2015 19:59
Show Gist options
  • Save kumatti1/96e89ff9e4100381d4d8 to your computer and use it in GitHub Desktop.
Save kumatti1/96e89ff9e4100381d4d8 to your computer and use it in GitHub Desktop.
HTML解析ヘルパー(仮)

HTML解析ヘルパー(仮)

勿体ぶるものでもないので公開。

・GetDocumentFromIE はこちらのを使っています。 そのままでコンパイルを通らないので直しが必要ですが、 (SendMessageTimeout のキャスト)HWND取得は、WindowFromAccessibleObject に変更しています。 http://eternalwindows.jp/browser/mshtml/mshtml02.html

・AutoWrap は、こちら。 タグごとにQueryInterfaceが必要になるのでコードを簡潔にする都合上、実行時バインドは必須かなと。 http://support.microsoft.com/kb/216686/ja

・UI Automation の要素を蛍光ペンで囲む機能を実装予定。 (当分やらないけど) http://blogs.msdn.com/b/japan_platform_sdkwindows_sdk_support_team_blog/archive/2011/05/26/ui-automation.aspx http://msdn.microsoft.com/ja-jp/library/vstudio/aa358508(v=vs.100).aspx

#define UNICODE
//#include <afxwin.h> //MFC
#include <Shlwapi.h>
#include <windows.h>
#import <mshtml.tlb>
#include <Oleacc.h>
#include <OleAuto.h>
#include <comdef.h>
//#include <atlcomcli.h>
#include <stdio.h>
#include <Mshtml.h>
#include <Exdisp.h>
//#include <afxwin.h> //MFC
//_COM_SMARTPTR_TYPEDEF(IMyInterface, __uuidof(IMyInterface));
_COM_SMARTPTR_TYPEDEF(IAccessible , __uuidof(IAccessible ));
HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...);
VOID CALLBACK TimerProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ UINT_PTR idEvent,
_In_ DWORD dwTime
);
BOOL GetDocumentFromIE(MSHTML::IHTMLDocument8Ptr *pp, IAccessiblePtr pAcc);
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
){
CoInitialize(NULL);
SetTimer(0,0,2000,&TimerProc);
MessageBox(nullptr,nullptr,nullptr,MB_OK);
CoUninitialize();
return 0;
}
VOID CALLBACK TimerProc(
HWND hwnd, // ウィンドウのハンドル
UINT uMsg, // WM_TIMER メッセージ
UINT_PTR idEvent, // タイマの識別子
DWORD dwTime // 現在のシステム時刻
)
{
POINT point;
_variant_t varItem;
IAccessiblePtr pAcc;
KillTimer(0,idEvent);
GetCursorPos(&point);
HRESULT hr = AccessibleObjectFromPoint(point, &pAcc, &varItem);
if ((hr == S_OK))
{
MSHTML::IHTMLElementPtr pElements;
IUnknown_QueryService(pAcc,
__uuidof(pElements),
IID_PPV_ARGS(&pElements));
if(pElements)
{
_bstr_t bstr;
_variant_t VarArrg;
MSHTML::IHTMLInputElementPtr pInput( pElements );
if( pInput )
{
MSHTML::IHTMLFormElementPtr pForm2;
pInput->get_form( & pForm2 );
AutoWrap(DISPATCH_PROPERTYGET, &VarArrg,
pForm2, L"name",0);
MessageBox(0,(LPCWSTR)VarArrg.bstrVal,0,MB_OK);
}
MSHTML::IHTMLElementCollectionPtr pCol;
MSHTML::IHTMLDocument8Ptr pDoc;
GetDocumentFromIE((MSHTML::IHTMLDocument8Ptr*)&pDoc, pAcc);
if(pDoc){
MSHTML::IHTMLDocument3Ptr pDoc3( pDoc );
#define Twitter_name L"session[username_or_email]"
MSHTML::IHTMLElementCollectionPtr pCol2;
pCol2 = pDoc3->getElementsByName( _bstr_t( Twitter_name ));
if( pCol2 )
{
long len2;
pCol2->get_length( &len2 );
for( long j = 0;j < len2;j++)
{
_variant_t Index2( j );
IDispatchPtr pDisp3;
pDisp3 = pCol2->item( Index2, Index2 );
MSHTML::IHTMLInputTextElementPtr pText_dummy( pElements );
MSHTML::IHTMLInputTextElementPtr pText( pDisp3 );
if( pText == pText_dummy )
{
char szTmp2[50];
sprintf( szTmp2, "%d", j );
MessageBoxA( 0, szTmp2, 0, MB_OK );
}
}
}
MSHTML::IHTMLDocument2Ptr pDoc2( pDoc );
pDoc2->get_forms(&pCol);
long lnglength;
pCol->get_length(&lnglength);
for(long i = 0;i < lnglength;i++){
_variant_t varIndex(i);
IDispatchPtr pDisp( pCol->item(varIndex, varIndex) );
MSHTML::IHTMLFormElementPtr pForm( pDisp );
long len;
pForm->get_length(&len );
for(long l = 0; l < len;l++)
{
_variant_t VarIndex( l );
IDispatchPtr pDisp2( pForm->item( VarIndex, VarIndex) );
MSHTML::IHTMLElementPtr pdummy( pDisp2 );
if( pdummy == pElements)
{
char szTmp[50];
sprintf(szTmp, "%d", i );
MessageBoxA( 0,szTmp,0, MB_OK);
AutoWrap(DISPATCH_PROPERTYGET, &VarArrg,
pForm, L"name",0);
MessageBox(0,(LPCWSTR)VarArrg.bstrVal,0,MB_OK);
}
}
}
}
AutoWrap(DISPATCH_PROPERTYGET, &VarArrg,
pElements, L"tagName",0);
MessageBox(0,(LPCWSTR)VarArrg.bstrVal,0,MB_OK);
AutoWrap(DISPATCH_PROPERTYGET, &VarArrg,
pElements, L"id",0);
MessageBox(0,(LPCWSTR)VarArrg.bstrVal,0,MB_OK);
AutoWrap(DISPATCH_PROPERTYGET, &VarArrg,
pElements, L"className",0);
MessageBox(0,(LPCWSTR)VarArrg.bstrVal,0,MB_OK);
AutoWrap(DISPATCH_PROPERTYGET, &VarArrg,
pElements, L"name",0);
MessageBox(0,(LPCWSTR)VarArrg.bstrVal,0,MB_OK);
AutoWrap(DISPATCH_PROPERTYGET, &VarArrg,
pElements, L"value",0);
MessageBox(0,(LPCWSTR)VarArrg.bstrVal,0,MB_OK);
AutoWrap(DISPATCH_PROPERTYGET, &VarArrg,
pElements, L"type",0);
MessageBox(0,(LPCWSTR)VarArrg.bstrVal,0,MB_OK);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment