WinBatch Technical Support Forum

Archived Boards => COM Automation and dotNet => Topic started by: JTaylor on August 06, 2015, 08:59:08 am

Title: InvokeScript
Post by: JTaylor on August 06, 2015, 08:59:08 am
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

           jarr = ArrDimension(1)
           jarr[0] = "alert('HI');"
           jscript = ObjectType("ARRAY", jarr)
           obrowser.Document.InvokeScript("eval", jscript);
 
Title: Re: InvokeScript
Post by: DAG_P6 on August 06, 2015, 11:29:37 pm
What browser and security zone?
Title: Re: InvokeScript
Post by: JTaylor on August 07, 2015, 07:44:28 am
WebBrowser Control.  Not sure on Security Zone.   I assume Internet.

Jim
Title: Re: InvokeScript
Post by: stanl on August 10, 2015, 04:21:44 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?
Title: Re: InvokeScript
Post by: stanl on August 10, 2015, 08:03:53 am
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");
Title: Re: InvokeScript
Post by: JTaylor on August 10, 2015, 08:53:37 am
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
Title: Re: InvokeScript
Post by: 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
Title: Re: InvokeScript
Post by: DAG_P6 on August 10, 2015, 10:28:01 am
WebBrowser Control.  Not sure on Security Zone.   I assume Internet.

Jim

I think you need to be in the Local Intranet zone.
Title: Re: InvokeScript
Post by: stanl on August 10, 2015, 11:49:51 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
Code: [Select]
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)
Title: Re: InvokeScript
Post by: stanl on August 10, 2015, 12:06:32 pm
I just tested this in IE 11 and it worked. Of course, not an embedded browser:
Code: Winbatch
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
 
Title: Re: InvokeScript
Post by: JTaylor on August 10, 2015, 12:13:10 pm
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
Title: Re: InvokeScript
Post by: stanl on August 10, 2015, 01:30:42 pm
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.
Title: Re: InvokeScript
Post by: JTaylor on August 10, 2015, 02:23:52 pm
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