Windows UI Automation from a Web Page - javascript

I have VB.NET code that handles automation of various application installs. I want to move this essentially out of a VB generated EXE package, and be able to execute the same code (or the equivalent result) from an HTML page on a server. Is this possible? It looks like javascript can't cross the web application/desktop application barrier. Perhaps I can execute the VB.NET code/application that handles the automation (stored server side) from code in the HTML? This is a fairly broad question(s) so ideas are welcome. Please post examples with your ideas!
Thanks.

Ian's comment made me read the question again. If you really plan to execute code on the client through a web browser, please ignore this answer.
You can use any server-side language that you want to do this. Since you already wrote it in VB, you can use ASP.NET or PHP's exec(). Example:
<?php
if($_GET["dosomething"]) {
exec("mycommand");
}
will execute mycommand when you pass dosomething as a paramter to the file.

Related

Does C# code contained in Javascript still need to be compiled?

We have a web application built with C# and ASP.NET. I have to reference a few .NET session variables in my JavaScript code, so I use the following line to do so:
var layoutID = '<%=Server.UrlEncode(string.Format("{0}", Session["layout_id"]))%>';
This works just fine. The web application is built and published to our web server. If I want to make a change, a .NET change would require me to compile the changes and publish it to the web server again. If it's a JavaScript change, I can simply edit the js in the .ASPX page and the fix is immediately picked up...no compiling needed.
My question - what if I make a change to this inline C# code contained in JavaScript? Say, for example, I meant to access Session["layoutid"] without the underscore. Could I just edit the .ASPX page on the web server, or will this require compiling just as if it were in a .CS file? I'm guessing since it's C#, I will still need to compile for the change to be picked up. And yes I could just try it and see for myself, but I was hoping someone could give a little insight into what goes on behind the scenes with this inline c# code. And while we're on the topic, is the technique I'm using to access .NET sessions variables in JavaScript the most efficient way to do it?
Bonus question - what is this technique called? I tried searching for an answer but I wasn't sure what to search for other than "in-line c# code JavaScript".
Thanks!
No, changes to ASPX/CSHTML files don't require re-compilation.
Note: whether it is good idea to jump on production server and change random code bypassing source control/testing is question to discuss and answer by your team.

Using vb.net code in Javascript

usually when I programming a web application with java I use DWR library to enables Java on the server and JavaScript in a browser to interact and call each other.
for example, I can call a java routine to access the DB directly from a javascript code, and this is really useful.
Now I'm trying to create a web application with visual studio 2012 and I don't know if I can do the same thing with asp.net.
I've tried some "googling" but i don't had success.
Someone can help me?
Thanks and sorry for my bad english >.<
You can access the server side code by using javaScript.
There are some good links on this site.
Based on my experience, i found this web service to be a very useful tool.
1. You must have a web service or asmx. (check how to create here).
2. Use jQuery or other method of Ajax POST to communicate with the web service.
$.ajax({
url: "/location_of_your_webservice/ASMX_FILE.asmx/backend_method_or_function",
...
Or if you just have to fire a button click that triggers the backend event then you might want to check this.

Writing to a text file using Javascript

I want to write to a text file using javascript. I know that it is possible but there are some problems.
I am running a javascript program that calculates the location of an object (its latitude & longitude) which changes every 5 seconds; i want to write this information to a text file. The javascript program will soon run on a server and I'll use the information written to the text file to communicate with an Android app on my phone.
So, my question really is:
How can it be done properly?
I know that there may be some permission issues but considering it won't be online and available to others will it be a problem and, if it is, should I go with PHP to do what I want? I know ASP is more Microsoft orientated and I work with a Mac so PHP would be the preference here.
When writing a file, is it possible to write to an existing file or does the process simply destroy and recreate the same file?
I would use PHP
http://www.php.net/
This has a good code example:
http://www.tizag.com/phpT/filewrite.php
Also, you can make the request using jQuery's AJAX function, this will allow to effectively run this code from javascript:
http://api.jquery.com/jQuery.ajax/
Using the latest HTML5 Javascript, you can use the FileSystem APIs to read/write/append to text files. This is a good tutorial here : Exploring the FileSystem APIs.

Is it possible to use a web page as a user interface to a program written in python, running locally, without a web server?

I have a program written in python, and I would like to make it easy to enter parameter values for this program through a GUI. I realise that I could create a GUI using python tools, but I am interested in using a html / javascript page and have the javascript code call my python script when the user clicks a button to run. Something like;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "../scripts/python_script.py", true);
xmlhttp.send();
Currently, when I do that, I just get back the text in the python script, but it doesn't actually run. Ideally the python script would run in the background without blocking further input to the web page, and as the script produces it's different result files (png images), these would be displayed in the browser. Clearly, I could do this using a web server (and I may end up doing this eventually anyway, hence the html interface), but I am wondering if it is possible to do so without one. That way I could package the html page and the python script together and give them to someone who could then go and run the program on their computer without needing to start a web server. Is this possible?
If it is not, is there an alternative way do achieve a similar result? Could I embed a small server into a python script that displays the html page when it starts up, and then responds to an XMLHttpRequest to start the python script? If I did this, would the user have to start the script, and then go to the specified address in their browser as a separate action?
EDIT: I got a quick solution working using SimpleHTTPServer, but I had a look at bottle and I'll probably try something using that as well. Thanks for your help.
First of all, using something like bottle it is pretty simple to make a web server to run your script. Look at http://bottlepy.org/docs/dev/
A good starting point is the code at http://bottlepy.org/docs/dev/tutorial.html#http-request-methods but you would put up a form asking for parameters rather than a login form. Then just run your Python script, capture the output and send it back in the return statement.
This question Capture subprocess output shows you two ways to run your main script depending on whether you want to show the output progressively or all at the end.
You'd need to bundle some kind of webserver with the application. If it is not intended for deployment I would go for something like bottle.py. It's a micro web framework that has its own development server. Other micro/mini frameworks probably pack their own webserver with them for development purposes (web2py, flask, ..).
If you want something more serious you'd probably need some better web server. If that's the case - have a look at this reddit discussion.

How to simulate JavaScript in a client C# Applications

I'm writing a web crawler (web spider) that crawl all links in a website.
My application is a Win32 App, written in C# with .Net framework 3.5.
Now I'm using HttpWebRequest an HttpWebResponse to communicate with the web server.
I also built my own Http Parser that can parse anything I want.
I found all link like "href", "src", "action"... in the parse.
But I can not solve one problem: Simulate Client Script in the page (like JS and VBS)
For example, if a link like:
a href = "javascript:buildLink(1)"
... with buildLink(parameter) is a Javascript function that will make a custom link due to the parameter.
Please help me to solve this problem. How to simulate JavaScript in this app? I can parse the HTML source code and take all JavaScript code to another file, but how to simulate a function of it?
Thanks.
Your only real option is to automate a browser. As other answers have said, you cannot reliably simulate browser javascript without having a complete DOM.
There are fortunately ways to automate the browser, check out Selenium.
It has a C# API, so you can control the browser from C#.
Use your .NET web crawler code to crawl the site. Whenever you encounter a href="javascript:... link, handle the page containing the link in Selenium:
Use the Selenium API to tell the browser to load the page.
Use the Selenium API to find all links on the page.
This way, your spider only uses Selenium when necessary (pages without javascript links can be handled by the browser-less spider code you already got). And since this is an embarrassingly parallel workload, you could easily have multiple Selenium processes running at the same time (either on one computer or on other computers).
But remember that href="javascript is hardly the only way a page can have dynamic links. The more common case is probably that a onload or $(document).ready() script manipulates the DOM and adds links that way.
To catch that case (and others), the spider probably will have to use Selenium for all pages that have a <script> tag.
You are basically pretending to be a browser, except that HttpWebRequest only does the networking stuff for you.
I would recommend using the ie web browser control and interop'ing into that from your c# application. That will allow you to run JavaScript, set variables, post, etc etc.
Here's some basic links I found after a search for "ie web browser control":
http://www.c-sharpcorner.com/UploadFile/mahesh/WebBrowserInCSMDB12022005001524AM/WebBrowserInCSMDB.aspx
http://support.microsoft.com/kb/313068
This is a problem which is not easily solved. You could consider taking one of the existing JavaScript implementations and porting or interfacing with it somehow.
If I were tackling this problem, I'd probably build a small side application in Java on top of Rhino, with some sort of RPC framework layered on top of that so that I could communicate with it from my primary application.
Unfortunately, without having a complete DOM implementation on top of that, you would be limited to only very simple javascript.
You could execute the javascript by using the MS JScript engine or something similar.
MSDN Reference
Eric Lippert's blog on using Eval (part 1) (part 2) (part 3)
This isn't guaranteed to work, especially if the javascript tries to access the DOM, or somesuch... But for simple scripts, it might be enough.

Categories