WebBrowser - invoking a JS function like it was called from an inputelement

O

ofiras

Hi,
I've tried my luck invoking a JS function like it was called by an
input HtmlElement that is in the document in the WebBrowser from C#. I
tried "InvokeScript" to the document with the function I want, but it
didn't work (probably because it should be called by the input
element), I've tried changing the OuterHtml of the element inserting a
"onfocus" event to it and calling it with "RaiseEvent" and also
HtmlElement.Focus but it also didn't work (On debug mode I found out
that the OuterHtml didn't even change...) and I also tried changing it
from the WebBrowser.document.Body.InnerHtml and it didn't change too.
I've also tried using HtmlElement.InvokeMember calling the method I
want with the argument I want but it didn't work either (I have no
idea why).
Is there a way doing it?
I want to do it since there is an "onclick" event to this input
element, and it dose the function I want, but before that he calls
another function that opens a confirm messagebox, and I want the
process to be fully automatic. I've tried changing the function that
calls the confirm messagebox (I posted here a question) and I also
tried pressing the OK button automatically by C# (also posted here...)
but both didn't work.
I almost succeeded doing it, when I changed the function that calls
the confirm messagebox, but it only works when I press the input
manually, and when I invoke the event with "RaiseEvent" from C# it
works like I didn't change a thing. I noticed that when I did it, the
WebBrowser.DocumentText didn't change while the
WebBrowser.Document.Body.InnerHtml did change. When I try changing the
WebBrowser.DocumentText it doesn't work since in reloads without the
URL path, therefore all of the links and script loadings don't work.
I am desperate, please save me!
I would be very grateful for it, I'm stuck on this problem for
weeks... There must be a way doing it! Any solution for one of the
things I've tried will help!
Please help me,
Ofir.
 
M

Moty Michaely

Hi,
I've tried my luck invoking a JS function like it was called by an
input HtmlElement that is in the document in the WebBrowser from C#. I
tried "InvokeScript" to the document with the function I want, but it
didn't work (probably because it should be called by the input
element), I've tried changing the OuterHtml of the element inserting a
"onfocus" event to it and calling it with "RaiseEvent" and also
HtmlElement.Focus but it also didn't work (On debug mode I found out
that the OuterHtml didn't even change...) and I also tried changing it
from the WebBrowser.document.Body.InnerHtml and it didn't change too.
I've also tried using HtmlElement.InvokeMember calling the method I
want with the argument I want but it didn't work either (I have no
idea why).
Is there a way doing it?
I want to do it since there is an "onclick" event to this input
element, and it dose the function I want, but before that he calls
another function that opens a confirm messagebox, and I want the
process to be fully automatic. I've tried changing the function that
calls the confirm messagebox (I posted here a question) and I also
tried pressing the OK button automatically by C# (also posted here...)
but both didn't work.
I almost succeeded doing it, when I changed the function that calls
the confirm messagebox, but it only works when I press the input
manually, and when I invoke the event with "RaiseEvent" from C# it
works like I didn't change a thing. I noticed that when I did it, the
WebBrowser.DocumentText didn't change while the
WebBrowser.Document.Body.InnerHtml did change. When I try changing the
WebBrowser.DocumentText it doesn't work since in reloads without the
URL path, therefore all of the links and script loadings don't work.
I am desperate, please save me!
I would be very grateful for it, I'm stuck on this problem for
weeks... There must be a way doing it! Any solution for one of the
things I've tried will help!
Please help me,
Ofir.

Hey Ofir,

Since JS runs on client side, you must handle the execution of the
script through client side only.

I still didn't fully undestand your needs, so it would be nice if
you'd type in some samples of what exactly you are trying to achieve.

Regards,
M
 
J

Jesse Houwing

Hello Moty,
Hey Ofir,

Since JS runs on client side, you must handle the execution of the
script through client side only.

I still didn't fully undestand your needs, so it would be nice if
you'd type in some samples of what exactly you are trying to achieve.

He is hosting an instance of IE in his appliaction. So this is running 'clientside'.
There seems to be no control over the pages displayed, and some things need
to be automated.

to the OP: Have you tried showing the Messagebox and then using the SendKeys
API to just press the Yes button?
 
O

ofiras

Hello Moty,






He is hosting an instance of IE in his appliaction. So this is running 'clientside'.
There seems to be no control over the pages displayed, and some things need
to be automated.

to the OP: Have you tried showing the Messagebox and then using the SendKeys
API to just press the Yes button?

You are right about what I need. As you said, I am working from
clientside therefore I am sure that it can be done.
About the solution you suggested - after the confirm messagebox
appears (I call it from C# using InvokeScript) it stops the program
from running until I press OK or Cancel (as I saw in debug mode) so I
can't use SensKey to press the yes button (unless you know how can I
invoke this function right after the messagebox appears). It is very
frustrating that I know that it can be done (since it is clientside
and I control everything) but still I can't change this JS function!
Thanks you Jesse.
I hope someone have a solution for me...
Please help,
Ofir.
 
M

Moty Michaely

You are right about what I need. As you said, I am working from
clientside therefore I am sure that it can be done.
About the solution you suggested - after the confirm messagebox
appears (I call it from C# using InvokeScript) it stops the program
from running until I press OK or Cancel (as I saw in debug mode) so I
can't use SensKey to press the yes button (unless you know how can I
invoke this function right after the messagebox appears). It is very
frustrating that I know that it can be done (since it is clientside
and I control everything) but still I can't change this JS function!
Thanks you Jesse.
I hope someone have a solution for me...
Please help,
Ofir.

Hey,

Jess: Thanks for the comment, I now understand what you and the OP
meant :).

What about injecting a JS code that disables window.alert function?

window.alert = function(message) { // do something else... }

It might work.

M
 
J

Jesse Houwing

Hello ofiras,
You are right about what I need. As you said, I am working from
clientside therefore I am sure that it can be done.
About the solution you suggested - after the confirm messagebox
appears (I call it from C# using InvokeScript) it stops the program
from running until I press OK or Cancel (as I saw in debug mode) so I
can't use SensKey to press the yes button (unless you know how can I
invoke this function right after the messagebox appears). It is very
frustrating that I know that it can be done (since it is clientside
and I control everything) but still I can't change this JS function!
Thanks you Jesse.
I hope someone have a solution for me...
Please help,
Ofir.

You could start a new thread and use the SendKeys API from there after x
time has elapsed.
 
O

ofiras

Hey,

Jess: Thanks for the comment, I now understand what you and the OP
meant :).

What about injecting a JS code that disables window.alert function?

window.alert = function(message) { // do something else... }

It might work.

M

It works! Thank you so much!
Ofir.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top