How do I call system code from JScript/VBScript? - c#

I have a C# app that uses a web browser control to display some HTML and do some JScript in the background.
How do I integrate this with other system calls? Do I call other C# code from JScript/VBScript? Do I need to do it from the form itself?

When I need javascript hosted in a webbrowser control to call back to the parent, I have it try to navigate the page to another URL. Then in the webbrowser's "BeforeNavigate" event, I get the URL the page was trying to go to, parse out any arguments, dispatch the request to the other C# code, then cancel the original navigate request.

Related

Is it possible to init a window on the client, server side (ASP.net)

In my C# code behind file I want to init a window on the client?
Ie. Window.open();
This should be fed with DialogTitle and DialogText and sent to the client.
I can set the window in the HTML and hide it until .open(); is called?
Is this task even possible Server side?
You can't call anything on the client from the server. However, you can send code to do so, for example JavaScript code or just plain HTML. If you are using ASP.NET Web Forms, the Page.RegisterClientScriptBlock method can be useful.
When you render the page, or a AJAX callback, put some <script> element in there with the code to open the window. Then you are all set. If it is an AJAX call, you have to execute the script, which is a little harder than just rendering the script element. See here how to do that.

C# Send web request with disabled javascript

I want to download content of one website programatically and it looks like this content is loaded by ajax calls. When I simply disable javascript in my browser, only 1 request is made by this page and all content is loaded without AJAX.
What I need to achieve is to make a web request which will tell web page that I have disabled javascript so it returns me all the content and not just empty body tag with no content at all.
Any suggestions how to do that?
You need to mimic browser.
Steps:
Use Fiddler and see what is sent by browser.
Set the same headers/cookies/user agent via C# code.
If does not work - compare request your code makes with browser's one by using Fiddler as proxy for your C# code (set proxy to http://localhost:8888)

Execute function from a webpage that is activated onclick

I have to make a console application in C# which retrieve some data from webpages.
I have downloaded the HTML code from the main page of a website.
WebClient client = new WebClient();
String htmlCode= client.DownloadString(linkToWebpage);
I have verified the string and it is good.
After this part, I have searched for a specific line in the html code which contains a button and a link.
<a rel="nofollow" class="link" onclick="loadornot()" href="http://aaaaa.com/D?WUrtlC1" target="_blank">Click to read more</a>
Now i am trying to download html code from the ancored link (the one from the href), but I am redirected to the main page and I am not sure why. Even if I copy the link from href and paste it into a webbrowser, I am redirected to the main page.
I believe that this happens because the button call a function onclick="loadornot()". That's why it doesn't work the way I have tried? And if yes, how could I call that function from my c# application to continue my app?
Thank you.
Edit:
I have found out that I need some cookies, more exactly, sessioncode, to make that link work. How can I do that?
You can't run javascript code from web page without browser. So, if you really need to execute that function in downloaded page, use some kind of headless browser, like those: webkitdotnet or awesomium

How to distinguish between a user clicking a link and the page doing an automatic redirect?

Having a C# WebBrowser control inside my WinForms application, and being aware of the Navigating event, I never came up with a nice and elegant solution to the following:
If a user actively navigates to another URL, I want to allow it.
If the page redirects "on its own" to another URL, I want to cancel it.
For case 1 there are some cases I can think of:
User clicks an a tag and the href attribute is evaluated to load another URL.
User clicks on an element with an onclick javascript event handler which calls a function that uses window.location to load another URL.
For case 2 I can imagine of:
The loaded page contains an iframe tag that loads an URL inside the IFrame. This fires the Navigating event.
There is some JavaScript timer that is started on page load and when it fires, it uses window.location to load another URL.
The loaded page contains a meta refresh header tag to load another URL after some seconds.
So my question is:
How to detect inside the Navigating event (or any other mechanism) whether a redirect is triggered explicitly by the user or implicitly by the page?
Some more information
The WebBrowser is being used inside a windows based CMS backend application.
I therefore have full control over the content loaded inside the WebBrowser control.
Meaning that I can manipulate the complete HTML string before being sent to the browser, if required.
If it is more applicable, I also would love to get JavaScript-only solutions which I could inject into the HTML being loaded.
(Please note that I do believe this is not a duplicate of this SO posting)
My take on this is capture user clicks on the web browser control. Have it set a flag that indicates that the user clicked on the web browser. If the flag is true, then allow redirection, if it isn't true don't allow it. Make sure to reset the flag after n number of seconds if no (or after) redirection is made.
It seems you are trying to achieve anti-ads/popup/redirect pattern.
From web browser perspective.. clicking <a href="some.url"> is not different from javascript window.location = "some.url"; or 302 redirect response. there are no explicit signals, no such convenience methods.
The WebBrowser control is just a proxy to IE component. You can't intercept browser's engine or even disable/enable javascript as it's part of internet security option.
You have to create special logic to prevent every possible cases of redirection.
eg.
verify HTML string then restrict some javascript pattern, header or iframe with Regex.Replace before render.
var scriptEx = new Regex("<script (.*?)</script>");
var iframeEx = new Regex("<iframe (.*?)</iframe>");
or intercept Navigating URL and cancel unsafe url, etc.

how can i get the result of JavaScript function of another websites page in C#?

I've application that uses another web sites data so how can i get it because it uses some JavaScript functions to get that data and it not show in page view-source.
Check the NET tab in firebug, XHR and check the resource that is requested, and request the same resource.
Basically you have to render the webpage and ensure the javascript functions are run (evaluated). You could do this by "borrowing" their javascript files (by linking to them from your own page), but this may not work as you don't know what's in those files - they could be accessing DOM elements that you don't have in your page, or calling to other domains which may prevent them from working correctly.
The easiest way to show the same data is to just host the page inside an iframe on your own page. If you are looking to do this from a normal client application (i.e. not a web app) then you will need a browser control that you navigate to the target page. If the browser control is invisible you could then scrape values from it and show them in your app, although this is a very clumsy way to do it, and it's debatable about how ethical it is.
If you want the another web site view source use the HTTPWebRequest to get the response stream in c#.

Categories

Resources