InvokeScript

Started by JTaylor, August 06, 2015, 08:59:08 AM

Previous topic - Next topic

JTaylor

Trying to make InvokeScript work with an embedded browser but going nowhere fast.  Here is one form of what I have tried.  Suggestions?

Jim

Code (winbatch) Select


           jarr = ArrDimension(1)
           jarr[0] = "alert('HI');"
           jscript = ObjectType("ARRAY", jarr)
           obrowser.Document.InvokeScript("eval", jscript);

DAG_P6

What browser and security zone?
David A. Gray
You are more important than any technology.

JTaylor

WebBrowser Control.  Not sure on Security Zone.   I assume Internet.

Jim

stanl

Quote from: JTaylor on August 06, 2015, 08:59:08 AM
Trying to make InvokeScript work with an embedded browser

Jim;

By embedded browser do you mean MSHTML, or are you using WPF? Also, are you building the HTML in memory or looking at a URL?

You probably have already done the requisite searches, but I did come across a couple of stackoverflow threads that suggested using jscripts eval() instead of invokescript().

Also, why are using Array, rather than string with full function code?

stanl

Jim;

I was confusing execscript() with invokescript() and the former is now deprecated from browser controls starting with IE 11

obrowser.Document.execscript("alert('HI');", "javascript");

JTaylor

I am using Shell.Explorer.2


All I can get is "  Exception scode 438 (0x800A01B6)" as an error response.

Thought this was part of the webbrowser control methods but today I am only finding references to C#.  Not sure if this is viable or not.

Jim

JTaylor

In response to the question about array....The InvokeScript() documentation indicates that the parameters must be an object and I thought I found documentation that said it should be an array.  Not seeing that now.  But, not sure what is needed now.   Surely there is some way to do this???

Jim

DAG_P6

Quote from: JTaylor on August 07, 2015, 07:44:28 AM
WebBrowser Control.  Not sure on Security Zone.   I assume Internet.

Jim

I think you need to be in the Local Intranet zone.
David A. Gray
You are more important than any technology.

stanl

Quote from: JTaylor on August 10, 2015, 09:12:00 AM
In response to the question about array....The InvokeScript() documentation indicates that the parameters must be an object and I thought I found documentation that said it should be an array.  Not seeing that now.  But, not sure what is needed now.   Surely there is some way to do this???

Jim

I think you are on the right path, but you haven't made it clear if you are using C# in your script:  I found this

When you call InvokeScript, it is intended to execute a function that is already defined in the page's JavaScript. In your case, you are intending to provide new JavaScript, and then invoke it. In order for this to work you either need to define the JavaScript as a function in the page, and then invoke it, or use a built-in function (eval) to execute the code for you. This is called "script injection"


on stackoverflow, and you appear right using an array, but then there is the part of defining a full function so perhaps your array need function .... {  }. 

an example was provided

string scriptToRun = "$.ajax({type: 'GET', async:false,url: 'http://localhost/test2.html', success: function (data) { $('body').html(data); }, complete: function () {} }); $('body').html();"; //ajax
var d = browser.Document.InvokeScript("eval", new Object[] { scriptToRun});


which is where I am confused as new Object is an array but the javascript is a string.

I see where there are a couple of powershell examples, and if I find time tonight will try to out one into the CLR. 

Interesting thread. (and execscript may still work)

stanl

I just tested this in IE 11 and it worked. Of course, not an embedded browser:
Code (WINBATCH) Select

oIE = CreateObject("InternetExplorer.Application")
oIE.visible = @true
url = "www.google.com"
oIE.navigate(url)
while oIE.readystate <> 4
timedelay(0.5)
endwhile

oIE.Document.parentWindow.execscript("alert('HI');","javascript")
oIE=0
Exit

JTaylor

It works from the parentWindow in an embedded browser.  I was going from Document.   Hadn't seen that path in any of the stuff I had looked at or maybe I'm just blind.

Okay.  Now to see if I can make this do what I want now that you've helped me jump that hurdle.

Thanks.


Jim

stanl

I wish you success.  As an aside, seems that Microsoft deprecated execscript() in favor of invokescript() - which has a powershell/C# focus, not (as I have seen) COM-enabled. I remember an earlier exchange I had with Tony about deprecated and "not supported" - to summarize, he said it may work until it breaks.

JTaylor

I appreciate the help.  Until this point all I was capable of was generating an obscure error.   At least now I have a starting point.  Thanks again.

Jim