# encoding: latin-1 # PyWin32 Internet Explorer Button # # written by Leonard Ritter (paniq@gmx.net) # and Robert Förtsch (info@robert-foertsch.com) """ This sample implements a simple IE Button COM server with access to the IWebBrowser2 interface. """ # imports section import sys, os, time from win32com import universal from win32com.client import gencache, DispatchWithEvents, Dispatch from win32com.client import constants, getevents import win32com.server.register import win32com import pythoncom import ponx from win32com.axcontrol import axcontrol import array, struct # ensure we know the ms internet controls typelib so we have access to IWebBrowser2 later on win32com.client.gencache.EnsureModule('{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}',0,1,1) # IObjectWithSite_methods = ['SetSite','GetSite'] IOleCommandTarget_methods = ['Exec','QueryStatus'] _iebutton_methods_ = IOleCommandTarget_methods + IObjectWithSite_methods _iebutton_com_interfaces_ = [ axcontrol.IID_IOleCommandTarget, axcontrol.IID_IObjectWithSite, # IObjectWithSite ] class Stub: """ this class serves as a method stub, outputting debug info whenever the object is being called. """ def __init__(self,name): self.name = name def __call__(self,*args): print 'STUB: ',self.name,args class IEButton: """ The actual COM server class """ _com_interfaces_ = _iebutton_com_interfaces_ _public_methods_ = _iebutton_methods_ _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER _button_text_ = 'IE Button2' _tool_tip_ = 'An example implementation for an IE Button.' _icon_ = 'C:\\ponx\\ponx.ico' _hot_icon_ = '' def __init__( self ): # put stubs for non-implemented methods for method in self._public_methods_: if not hasattr(self,method): print 'providing default stub for %s' % method setattr(self,method,Stub(method)) def QueryStatus (self, pguidCmdGroup, prgCmds, cmdtextf): # 'cmdtextf' is the 'cmdtextf' element from the OLECMDTEXT structure, # or None if a NULL pointer was passed. result = [] for id, flags in prgCmds: flags |= axcontrol.OLECMDF_SUPPORTED | axcontrol.OLECMDF_ENABLED result.append((id, flags)) if cmdtextf is None: cmdtext = None # must return None if nothing requested. # IE never seems to want any text - this code is here for # demo purposes only elif cmdtextf == axcontrol.OLECMDTEXTF_NAME: cmdtext = "IEButton Name" else: cmdtext = "IEButton State" ponx.msginfo("AAAAAAA"+str(cmdtest)+" ") return result, cmdtext def Exec(self, pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn): #print pguidCmdGroup, nCmdID, nCmdExecOpt, pvaIn ie=self.webbrowser window=ie.Document.parentWindow document=ie.Document body=ie.Document.Body ponx.msginfo("Left : ",str(ie.Left)) """ ie.Left=30 #ne marche que si un seul onglet ie.moveTo(30,10) #ne marche que si un seul onglet window.resizeTo(1024,720) #ne marche que si un seul onglet window.moveTo(30,10) #ne marche que si un seul onglet """ try: hobj=document.getElementById("PONX") html=hobj.innerHTML ponx.msginfo("PONX", html) except: pass ponx.msginfo("body",ponx.u1252(body.innerHTML)) tempurl=ie.LocationURL ponx.msginfo("Adresse : ",tempurl) # pour fermer les onglets, sauf celui actif import win32com,win32com.client clsid= '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}' hie=win32com.client.Dispatch(clsid) iext=None for instance in hie: if instance.LocationURL==tempurl: iext=instance else: instance.Quit() while iext.Busy==True: time.sleep(0.250) time.sleep(0.250) # ------------ fin ... pour fermer... ---------------- """ # si connexion "extérieure" iext.Left=30 iext.Top=10 iext.Width=1024 iext.Height=720 """ window.eval("function toto(txt){ alert('Zzzzzz (envoyé par IEbutton)');}") time.sleep(0.250) window.toto("aaa") time.sleep(0.5) document.write("Vive PONX ! ") time.sleep(1) ie.Left=30 #ne marche que si un seul onglet ie.Top=10 ie.Width=1024 ie.Height=720 window.alert("Aaaallleeerrrttt") ie.Navigate("http://ponx.org") #self.webbrowser.ShowBrowserBar(GUID_IETOOLBAR, not is_ietoolbar_visible()) def SetSite(self,unknown): if unknown: # first get a command target cmdtarget = unknown.QueryInterface(axcontrol.IID_IOleCommandTarget) # then travel over to a service provider serviceprovider = cmdtarget.QueryInterface(pythoncom.IID_IServiceProvider) # finally ask for the internet explorer application, returned as a dispatch object self.webbrowser = win32com.client.Dispatch(serviceprovider.QueryService('{0002DF05-0000-0000-C000-000000000046}',pythoncom.IID_IDispatch)) else: # lose all references self.webbrowser = None def GetClassID(self): return self._reg_clsid_ def register(classobj): import _winreg subKeyCLSID = "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\%38s" % classobj._reg_clsid_ try: hKey = _winreg.CreateKey( _winreg.HKEY_LOCAL_MACHINE, subKeyCLSID ) subKey = _winreg.SetValueEx( hKey, "ButtonText", 0, _winreg.REG_SZ, classobj._button_text_ ) _winreg.SetValueEx( hKey, "ClsidExtension", 0, _winreg.REG_SZ, classobj._reg_clsid_ ) # reg value for calling COM object _winreg.SetValueEx( hKey, "CLSID", 0, _winreg.REG_SZ, "{1FBA04EE-3024-11D2-8F1F-0000F87ABD16}" ) # CLSID for button that sends command to COM object _winreg.SetValueEx( hKey, "Default Visible", 0, _winreg.REG_SZ, "Yes" ) _winreg.SetValueEx( hKey, "ToolTip", 0, _winreg.REG_SZ, classobj._tool_tip_ ) _winreg.SetValueEx( hKey, "Icon", 0, _winreg.REG_SZ, classobj._icon_) _winreg.SetValueEx( hKey, "HotIcon", 0, _winreg.REG_SZ, classobj._hot_icon_) except WindowsError: print "Couldn't set standard toolbar reg keys." else: print "Set standard toolbar reg keys." def unregister(classobj): import _winreg subKeyCLSID = "SOFTWARE\\Microsoft\\Internet Explorer\\Extensions\\%38s" % classobj._reg_clsid_ try: hKey = _winreg.CreateKey( _winreg.HKEY_LOCAL_MACHINE, subKeyCLSID ) subKey = _winreg.DeleteValue( hKey, "ButtonText" ) _winreg.DeleteValue( hKey, "ClsidExtension" ) # for calling COM object _winreg.DeleteValue( hKey, "CLSID" ) _winreg.DeleteValue( hKey, "Default Visible" ) _winreg.DeleteValue( hKey, "ToolTip" ) _winreg.DeleteValue( hKey, "Icon" ) _winreg.DeleteValue( hKey, "HotIcon" ) _winreg.DeleteKey( _winreg.HKEY_LOCAL_MACHINE, subKeyCLSID ) except WindowsError: print "Couldn't delete Standard toolbar regkey." else: print "Deleted Standard toolbar regkey." # # test implementation # class PyWin32InternetExplorerButton(IEButton): _reg_clsid_ = "{104B66A9-9E68-49D1-A3F5-94754BE9E0E6}" _reg_progid_ = "PyWin32.IEButton" _reg_desc_ = 'Test Button' _button_text_ = 'IE Button' _tool_tip_ = 'An example implementation for an IE Button.' _icon_ = 'C:\\ponx\\ponx.ico' _hot_icon_ = _icon_ def DllRegisterServer(): register(PyWin32InternetExplorerButton) def DllUnregisterServer(): unregister(PyWin32InternetExplorerButton) if __name__ == '__main__': win32com.server.register.UseCommandLine(PyWin32InternetExplorerButton, finalize_register = DllRegisterServer, finalize_unregister = DllUnregisterServer)