Altering JavaScript function via webbrowser

O

ofiras

Hi,
I was trying to change a JavaScript function in a cretin page that was
loaded in webbrowser, but it didn't work. I actually changed the
webBrowser.Body.InnerHtml altering the function code to what I want,
but when I checked it worked like I didn't change anything.
I need it since when I automatically press a button in this page, it
calls a function that needs permission to proceed, therefore the
script looks like:
"function ConfirmAction()
{
if(confirm('Confirm?'))
{
return true;
}
else
{
return false;
}
}"

And I don't want this to happen, so I want to change it to:
" function ConfirmAction() { return true; } "

I will also welcome another solution, like automatically pressing OK
in the confirm window or something...
Please help,
Ofir.
 
O

ofiras

You're in the wrong newsgroup - this one is for C#.

For ASP.NET, please post in microsoft.public.dotnet.framework.aspnet

It's not ASP.NET, I am using webbrowser in C# to load a page. I am not
creating a web page.
Please help,
Ofir.
 
O

ofiras

You have not provided any C# code - only JavaScript...

Yes, this is the code that I want to change... I'm sorry if I wasn't
clear enough, I will try to explain myself better:
I have a C# program that loads a certain web page into a webBrowser.
After loading the page, I want to change a JavaScript code that is
found in this page (as stated above). I've tried changing the
webBrowser.Document.Body.InnerHtml replacing the original code found
there with the one stated above. This didn't work - the page acted
like i did not change anything. Actually, what I want to do is to
eliminate the need of pressing OK in a confirm window that papers
after the page is loaded. Therefore it will also help if there is
another solution to this problem.
Does anyone know how can i change the JavaScript code of this page?
Please help,
Ofir.
 
F

Family Tree Mike

Yes, this is the code that I want to change... I'm sorry if I wasn't
clear enough, I will try to explain myself better:
I have a C# program that loads a certain web page into a webBrowser.
After loading the page, I want to change a JavaScript code that is
found in this page (as stated above). I've tried changing the
webBrowser.Document.Body.InnerHtml replacing the original code found
there with the one stated above. This didn't work - the page acted
like i did not change anything. Actually, what I want to do is to
eliminate the need of pressing OK in a confirm window that papers
after the page is loaded. Therefore it will also help if there is
another solution to this problem.
Does anyone know how can i change the JavaScript code of this page?
Please help,
Ofir.


What Mark is trying to say, I believe, is that whatever the JavaScript is,
is irrelavant. The C# code is where the problem lies, and we need to see
that to offer help.
 
O

ofiras

Yes, this is the code that I want to change... I'm sorry if I wasn't
clear enough, I will try to explain myself better:
I have a C# program that loads a certain web page into a webBrowser.
After loading the page, I want to change a JavaScript code that is
found in this page (as stated above). I've tried changing the
webBrowser.Document.Body.InnerHtml replacing the original code found
there with the one stated above. This didn't work - the page acted
like i did not change anything. Actually, what I want to do is to
eliminate the need of pressing OK in a confirm window that papers
after the page is loaded. Therefore it will also help if there is
another solution to this problem.
Does anyone know how can i change the JavaScript code of this page?
Please help,
Ofir.

What Mark is trying to say, I believe, is that whatever the JavaScript is,
is irrelavant.  The C# code is where the problem lies, and we need to see
that to offer help.

This is the code:

string serchfor = "function ConfirmKaza()";
webBrowser1.Document.InvokeScript("ConfirmKaza");
int ndx_start_function = webBrowser1.Document.Body.InnerHtml.IndexOf
(serchfor);
int ndx_end_function = webBrowser1.Document.Body.InnerHtml.IndexOf("</
SCRIPT>", ndx_start_function);
string to_replace = webBrowser1.Document.Body.InnerHtml.Substring
(ndx_start_function, ndx_end_function - ndx_start_function);
webBrowser1.Document.Body.InnerHtml =
webBrowser1.Document.Body.InnerHtml.Replace(to_replace, "\r\n function
ConfirmKaza() { return true; } \r\n");
 
F

Family Tree Mike

This is the code:

string serchfor = "function ConfirmKaza()";
webBrowser1.Document.InvokeScript("ConfirmKaza");
int ndx_start_function = webBrowser1.Document.Body.InnerHtml.IndexOf
(serchfor);
int ndx_end_function = webBrowser1.Document.Body.InnerHtml.IndexOf("</
SCRIPT>", ndx_start_function);
string to_replace = webBrowser1.Document.Body.InnerHtml.Substring
(ndx_start_function, ndx_end_function - ndx_start_function);
webBrowser1.Document.Body.InnerHtml =
webBrowser1.Document.Body.InnerHtml.Replace(to_replace, "\r\n function
ConfirmKaza() { return true; } \r\n");


It appears that you are running the script (line 2,
InvokeScript("ConfirmKaza")), before the edits (the rest of the code).
 
O

ofiras

It appears that you are running the script (line 2,
InvokeScript("ConfirmKaza")), before the edits (the rest of the code).

I forgot to add the last line... I did it also before I edit the html
code to see if it changes...
Anyway this is how it should be:

string serchfor = "function ConfirmKaza()";
int ndx_start_function = webBrowser1.Document.Body.InnerHtml.IndexOf
(serchfor);
int ndx_end_function = webBrowser1.Document.Body.InnerHtml.IndexOf("</
SCRIPT>", ndx_start_function);
string to_replace = webBrowser1.Document.Body.InnerHtml.Substring
(ndx_start_function, ndx_end_function - ndx_start_function);
webBrowser1.Document.Body.InnerHtml =
webBrowser1.Document.Body.InnerHtml.Replace(to_replace, "\r\n function
ConfirmKaza() { return true; } \r\n");
webBrowser1.Document.InvokeScript("ConfirmKaza");

And this should have worked? After changing the
webBrowser1.Document.Body.InnerHtml I checked to see if it was also
changed in the webBrowser1.DocumentText but it stayed as it was. I
also tried changing the webBrowser1.DocumentText but this also didn't
work.
Mabye chnging Javascript is not good for this, and i should make a
code that will aoutomatically "press" OK at the confirm messagebox?
Please help,
Ofir.
 
O

ofiras

I forgot to add the last line... I did it also before I edit the html
code to see if it changes...
Anyway this is how it should be:

string serchfor = "function ConfirmKaza()";
int ndx_start_function = webBrowser1.Document.Body.InnerHtml.IndexOf
(serchfor);
int ndx_end_function = webBrowser1.Document.Body.InnerHtml.IndexOf("</
SCRIPT>", ndx_start_function);
string to_replace = webBrowser1.Document.Body.InnerHtml.Substring
(ndx_start_function, ndx_end_function - ndx_start_function);
webBrowser1.Document.Body.InnerHtml =
webBrowser1.Document.Body.InnerHtml.Replace(to_replace, "\r\n function
ConfirmKaza() { return true; } \r\n");
webBrowser1.Document.InvokeScript("ConfirmKaza");

And this should have worked? After changing the
webBrowser1.Document.Body.InnerHtml I checked to see if it was also
changed in the webBrowser1.DocumentText but it stayed as it was. I
also tried changing the webBrowser1.DocumentText but this also didn't
work.
Mabye chnging Javascript is not good for this, and i should make a
code that will aoutomatically "press" OK at the confirm messagebox?
Please help,
Ofir.

Just a few minuits ago i've tried changing the
webBrowser1.DocumentText in the documentComplete event and it worked,
but it loaded the page again with the html like it has nothing to do
with the website (like i'm writing my own html) so it didn't load any
of the images or scripts that are on a .js files. is there a way
making it know that the path that he should access when loading files
and images is this website?
Please help,
Ofir.
 
R

Ratnesh Maurya

Just a few minuits ago i've tried changing the
webBrowser1.DocumentText in the documentComplete event and it worked,
but it loaded the page again with the html like it has nothing to do
with the website (like i'm writing my own html) so it didn't load any
of the images or scripts that are on a .js files. is there a way
making it know that the path that he should access when loading files
and images is this website?
Please help,
Ofir.

Hi Ofir,

I think its better to set value of some hidden field and let the
javascript handle the rest.

something like

function doSomething() {
if (document.getElementById("myID").value == 1) {
return true;
}
}
 
O

ofiras

Hi Ofir,

I think its better to set value of some hidden field and let the
javascript handle the rest.

something like

function doSomething() {
if (document.getElementById("myID").value == 1) {
return true;

}
}

I didn't understand what you mean. can you please explain? It looks
like a javascript code, and if so I didn't find a way to change it.
Thanks,
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