html - Getting actions from WebBrowser control in C# -
is possible - example, have form webbrowser control , local html page has elements in it. when user clicks on button in web page, form (for example, close application). there way connect dom actions form events , how?
yes, can use webbrowser.objectforscripting property set object exposed javascript window.external
. within javascript can call methods on object. if need first inject javascript page hook stuff in html page didn't write, webbrowser.documentcompleted
event, can inject javascript webbrowser.document
so:
private void webbrowser_documentcompleted(object sender, webbrowserdocumentcompletedeventargs e) { try { maindoc = webbrowser.document; if (maindoc != null) { htmlelementcollection heads = maindoc.getelementsbytagname("head"); htmlelement scriptel = maindoc.createelement("script"); ihtmlscriptelement el = (ihtmlscriptelement)scriptel.domelement; el.text = "alert('hello world')"; heads[0].appendchild(scriptel); } } catch (exception ex) { debug.writeline(ex.tostring()); } }
edit: neglected mention ihtmlscriptelement comes com , you'll need code somewhere:
[comimport, comvisible(true), guid(@"3050f28b-98b5-11cf-bb82-00aa00bdce0b")] [interfacetypeattribute(cominterfacetype.interfaceisidispatch)] [typelibtype(typelibtypeflags.fdispatchable)] public interface ihtmlscriptelement { [dispid(1006)] string text { set; [return: marshalas(unmanagedtype.bstr)] get; } }
Comments
Post a Comment