サーバレス練習帳

着眼大局着手小局

【python】URLチェッカー

開いている全てのIEタブのURLをIHTMLDocument2を使ってチェックします。
urlChecker_v0.0.1.0.exe

### iedetection.py ###
__author__ = "w.y"
__version__ = "0.0.1.0"

from win32gui import GetClassName,EnumChildWindows,FindWindow
from comtypes.gen.MSHTML import IHTMLWindow2,IHTMLDocument2,IHTMLDocument3,IHTMLElement,IHTMLInputElement,IHTMLFrameElement3,IDispatch
from ctypes import windll,c_ulong,byref,POINTER
from time import sleep

class clsIETabs(object):

	lsHandle = []
	lsTitle = []
	lsUrl = []
	pDoc2 = POINTER(IHTMLDocument2)()

	
	def __init__(self) : 
		self.lsHandle.clear()
		self.lsTitle.clear()
		self.lsUrl.clear()

	def setHandle(self, value) :
		self.lsHandle.append(value)
		return True

	def setTitle(self, value) :
		self.lsTitle.append(value)
		return True

	def setUrl(self, value) :
		self.lsUrl.append(value)
		return True

	def resetIETabs(self) :
		self.lsHandle.clear()
		self.lsTitle.clear()
		self.lsUrl.clear()
		return True

	def getDoc2FromHandle(self,hWnd) :
		lRes2 = c_ulong(0)
		pLRes2 = byref(lRes2)
		uMsg = windll.user32.RegisterWindowMessageW(u'WM_HTML_GETOBJECT')
		windll.user32.SendMessageTimeoutW(hWnd, uMsg, 0, 0,2, 1000, pLRes2) # 2 == SMTO_ABORTIFHUNG 
		windll.oleacc.ObjectFromLresult(lRes2, IHTMLDocument2._iid_, 0, byref(self.pDoc2))
		return self.pDoc2

	def getDoc3FromHandle(self,hWnd) :
		lRes3 = c_ulong(0)
		pLRes3 = byref(lRes3)
		uMsg = windll.user32.RegisterWindowMessageW(u'WM_HTML_GETOBJECT')
		windll.user32.SendMessageTimeoutW(hWnd, uMsg, 0, 0,2, 1000, pLRes3) # 2 == SMTO_ABORTIFHUNG 
		windll.oleacc.ObjectFromLresult(lRes3, IHTMLDocument3._iid_, 0, byref(self.pDoc3))
		return self.pDoc3


	def __checkIETab(self,hWnd) :
		if (GetClassName(hWnd) == 'Internet Explorer_Server'):
			#print(str(hWnd)+' : '+GetClassName(hWnd)+' : '+SGetWindowText(hWnd))
			self.setHandle(hWnd);

	def iedetectstub(self) :
		return 'OK'

	def iedetect(self) :
		#初期化:IEタブのクラスを生成、無限ループフラグをセット
		user32 = windll.user32
		strResult = ''
		#無限ループIEタブを監視
		#古いIEタブリストの削除
		self.resetIETabs()
		
		#IEタブリスト(ウインドウハンドル)の取得
		EnumChildWindows(FindWindow(u'IEFrame',None),lambda hWnd, _: self.__checkIETab(hWnd), None)
		#IEタブリスト(タイトルとUrl)の取得
		for hWnd in self.lsHandle:
			try : 
				self.getDoc2FromHandle(hWnd)
				strResult=strResult+IETabs.pDoc2.title+' ; '+IETabs.pDoc2.url+'\n\n'
			except :
				pass
		return strResult

if __name__ == '__main__':
	IETabs = clsIETabs()
	windll.user32.MessageBoxW(None, IETabs.iedetect(), 'URL Checker', 0x00000040)