Invoke javascript methods on a page - c#

Is there a way using either C# or a scripting language, such as Python, to load up a website in the user's default webbrowser and continue to interact it via code (e.g. invoke existing Javascript methods)? When using WinForms, you can host a Webbrowser control and invoke scripts from there, but only IE is supported. Is there a way of doing the same thing in the user's default browser (not necessarily using WinForms)?
Update: The website is stored on the user's machine, not served from a third party server. It is a help page which works dynamically with my C# program. When the user interacts with my C# program, I want to be able to execute the Javascript methods on the website.

You might want to look into Selenium. It can automate interaction with FireFox, IE, Chrome (with chromedriver) and Opera. It may not be suitable for your purposes due to the fact that it uses a fresh, stripped down profile, rather than the user's normal browser profile.

If you look at the HTTP request header you can determine the user-agent making the request. Based upon that information you can write logic from the server side as to respond with a unique page per detected user-agent string. Then you add any unique JavaScript you want as a static string to be executed by the user-agent application.

Related

How to communicate with Google Chrome using C# or Python

I'm developing a software on C# which has to get info from a website which the user opens in chrome, the user has to input some data and then the website returns a list of different items.
What I want is a way to be able to access to the source code of the page in order to get the info, I cant open the web myself as it doesnt show anything because I didnt input any data, so I need to get it directly from chrome.
How can I achieve this ? A chrome extension ? Or can I access to chrome directly from my software ?
Off the top of my head, I don't know any application that gets data directly from an open instance of Chrome. You'd have to write your own Chrome extension.
Alternatively, you can open the web browser from your application initially.
You can look into these libraries for doing so:
Watin (My personal favourite)
Selenium
Awesomium (You'd have to roll out your own UI, it's invisible)
Cef
Essential Objects Web Browser
EDIT: I didn't think about using QA tools as the actual browser hook as #TheAnathema mentions. That would probably work for your needs.
You're going to need to create it as Chrome extension if you must be dependent on the user actually going to a specific web site (i.e. not being able to do the requests yourself with either Selenium or standard web requests in Python).
The reason why a Chrome extension would be required is because think of how bad it could be for any software to easily read the pages you browse. Banking, medical, email, etc. could all be accessed anonymously from any process if Google allowed any outside process to tap into the web page.
Even Chrome extensions have to ask for permission to be able to do what they want, but at least it is software the user knowingly installed and agreed to the permissions.
A quick search yielded this example of modifying a page's HTML with a Chrome extension: https://blog.lateral.io/2016/04/create-chrome-extension-modify-websites-html-css/
It sounds like you want to do web scraping. Here's a good tutorial to get you started: HTML Scraping.
And this answer has a good example of how to scrape data from a website where you need to submit a form to get access to the data.

WPF Process.Start(url) how to know current URL or when window is closed

I have a WPF application that redirects to a payment system and then starts pooling a database to see if the transaction reference was posted back via a different channel.
I had a chat with one of my programmer friends and he said that there was no need for pooling and I could have simply tracked Url to which payment system redirects after successful payment and react accordingly.
Code to open window and redirect agent to payments system is as follows:
var process = Process.Start(new ProcessStartInfo(url));
Is there a way to get the Url of the Browser Window?
I would recommend to use a web browser control inside your application. There are very good ones and a built-in version (called WebBrowser). Process.Start is a problem since you never know which browser (and version) is loaded. You have support a lot of different ways to get the URL.
The benefit of using a web browser control in your application is that you have absolute control of the web browser, you can handle events like loading of pages, which enables you to perform checks on the URL. I use this myself to do OAuth authentication on a client and get the token back from the URL and parse the token out of it.

Listening for http requests in a windows forms application

I've got to write a .net windows forms application that will open a webpage and then be able to react to the user clicking on certain links on the webpage. The specification I've been given has the links on the webpage just being http links.
Is there a way for my .net application to have a minimal web server on it which will allow it to handle http requests on a given port?
Use an HttpListener.
http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
If all you need is to show a webpage, and you don't have any restrictions on the browser used, then the WebBrowser control will do the trick.
Drag it on to your form
Set the Url property to the page you need to display
Attach to the Navigating event
You can now respond to clicks, cancel them, do whatever you like. If it's just responding to client-side clicks you need, you don't need a web server. If you DO need a webserver, WinForms shouldn't have anything to do with it.
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
private void webBrowser1_Navigating(object sender,
WebBrowserNavigatingEventArgs e)
{
//Do your thing... maybe set e.Cancel if you don't to navigate
}
Please look at the WebBrowser control and specifically the "ObjectForScripting" property. If you set it to the parent form you can actually handle javascript events from the page loaded in the webbrowser in your c# code!!!
I hope that helps!
There are different ways to do this depending on what functionality you need. If all you need to do is respond to click events, and you don't need "full" http protocol support, you can just open a socket and parse what comes in from the browser.
Alternatively, you can use HttpListener, which takes care of the http protocol parsing for you and is relatively easy to use. For what I think you need, this is probably the preferred approach. Simple, non-compiling example here: https://gist.github.com/1770645.
The "holy grail" is hosting the ASP.NET runtime in your windows forms application. I've done this and it is pretty involved. The runtime has to be hosted in a separate AppDomain, so you end up jumping through a lot of hoops to get everything running and hooked up. It also involves writing an implementation of HttpWorkerRequest that is more full featured that the framework provided SimpleWorkerRequest. Incidentally, this also works for windows services, which gives you a great way to provide service management and monitoring through a browser without having a dependency on IIS.
I have interpreted the question differently to other users, so maybe I am way off but, I read it as he is trying to render web pages from the web and react to a user clicking on a link within the web page.
The only way I can think of doing this is by using some form of renderer ie webkit and hooking into that to intercept the clicks a user makes.
You can use Nancy
Site of project: https://www.codeproject.com/Articles/694907/Embed-a-web-server-in-a-windows-service

Need HTTP Status (200/500/etc) Code from WebBrowser Control

I have a .NET desktop application (not web) with a WebBrowser control.
I cannot find any information on how, or if it is even possible, to obtain the HTTP status code when a document is navigated to inside this control. Does anyone know if this is possible or how?
The purpose is to detect codes other than 200 and perform actions accordingly within the application.
A web page is not made up from a single HTTP GET request. The stackoverflow.com front page for example requires 16 requests. Stuff like javascript code, images, page visit counters, coming from different web sites as well. Some of it retrieve from cache instead of downloaded.
WebBrowser (aka Internet Explorer) doesn't support enumerating these individual requests. You'd have to use the HttpWebRequest class, but that of course doesn't make a web page.

Directly Navigating a Browser with an XBAP

I'm working on a full-trust WPF browser application (XBAP) and have come across a snag. An application on the client is wired to handle navigation requests with a certain prefix/protocol. (For example, 'foo://...") Additionally, the application handles these differently depending on which browser creates the request. I've tried the Page's 'NavigationService.Navigate()' method, but for some strange reason this involves WebRequest objects, which raises a NotSupportedException saying "The URI prefix is not recognized." I can create a WebBrowser control and successfully navigate IT with this prefix, but the application recognizes it as a different browser and reacts differently. If I were using Silverlight, I'd be to directly set the browser's 'window.location' which works perfectly. But of course, this is only appears to be available in the Silverlight framework.
So bottom line, what I need to do is trigger the browser hosting the XBAP to navigate to a URI with this special prefix.
Whew. That's all, I think. Thanks.
Update: The .Net Framework 4.0 will allow direct access to the browser's DOM which will solve this problem, however I am still scrounging for ways of accomplishing this in .Net 3.x.
You can try to use BrowserInteropHelper.ClientSite to access the DOM. Check out this post for a sample:
http://blogs.msdn.com/changov/archive/2009/03/01/scripting-out-of-an-xbap.aspx
Note that this would only work for Internet Explorer though.

Categories

Resources