C# WebBrowser open new window and keep javascript object - c#

I'm trying to design a multiple tab browser with webbrowser-control,
when my webrowser invoke the following javascript.
function myFunction() {
var myWindow = window.open("", "_blank");
// The website need do something to get url
myWindow.location.href = "https://www.google.com";
}
my webbrowser fire newWindow event first,
then i will open a new webbrowser,
in this case the url is "about:blank",
but the problem is how to bring "myWindow" to the new webbrowser and navigate to the correct url.
thanks a lot.

Use _parent instead of _blank in window.open.

Related

Open chrome and Auto fill the form fields windows form C#

I want to Open chrome and Auto fill the form fields windows form C#.
automatically open a new google chrome web browser to a predefined URL
automatically complete required fields with predefined data
I don't want to open web within Form control WebBrowser.
If I try the below code it's not working.
System.Windows.Forms.WebBrowser webBrowser = new WebBrowser();
HtmlDocument document = null;
document=webBrowser.Document;
System.Diagnostics.Process.Start("http://www.google.co.in");
You can do it the approach you have already started (without opening chrome and using the WebBrowser control) :
System.Windows.Forms.WebBrowser webBrowser = new WebBrowser();
webBrowser.DocumentCompleted += (s, e)=>{
//add auto fill here ie:
HtmlDocument doc = ((WebBrwoser)s).Document;
var element = doc.GetElementById("email");
if(element!=null) element.SetAttribute("value", "AutoFillValue");
};
webBrowser.Navigate(someUrl);
If you insist on using Chrome, you should use Chrome WebDrive
if you know the form field names, they can be prefilled by passing them as query string parameters
Process.Start("http://www.google.co.in?q=test");

How to inject a Javascript script into webpage using WebBrowser control?

In my application I need to use a WebBrowser control to process user login. I use WPF and it's WebBrowser control. The problem is that I want to display a part of web page that is under certain div. I found a solution to this, I need to inject an javascript script into a loaded html page, but I have no clue how to do it :(
This is the script that I would like to inject into web.
function showHide() {
$('body >').hide();
$('#div').show().prependTo('body');
}
So i could later call it webbrowser1.InvokeScript("showHide");
I read a lot of posts on stack, but all of them refer to WindowsForms WebBrowser and it is not working with WPF one.
EDIT: I tried:
private void PageLoaded (object sender, NavigationEventArgs e)
{
var webBrowser = sender as WebBrowser;
webBrowser.Document.InvokeScript("execScript",
new object[] {"$('body >').hide(); $('#" + _div + "').show().prependTo('body');"});
}
But webBrowser.Document is type object and I cannot call InvokeScript on it, have no clue to what I should cast it.
Thanks for help.
Add a reference to mshtml
In whatever event you want to inject the JavaScript:
var doc = (mshtml.HTMLDocument)webBrowser1.Document;
var head = doc.getElementsByTagName("head").Cast<mshtml.HTMLHeadElement>().First();
var script = (mshtml.IHTMLScriptElement)doc.createElement("script");
script.text = "function myFunction() { alert(\"Hello!\");}";
head.appendChild((mshtml.IHTMLDOMNode)script);
In whatever event you want to invoke the JavaScript from:
webBrowser1.InvokeScript("myFunction");
Result:

WPF C# CefSharp Navigate URL

I want to navigate new link like VS in webbrowser control but i can't this with wpf.
This for ie;
WebBrowser.Navigate("http://www.bing.com");
How is it done with CefSharp in WPF?
In cefsharp, you can do it like
string navigateUrl = "http://www.bing.com";
ChromiumBrowser.Address = navigateUrl; //navigates to the URL
//or use Reload method if you want to reload it
ChromiumBrowser.Reload(true);

How to download source code of website after having emulated a button click?

With WebClient I can download the source code.
WebClient.DownloadString(url);
With WebBrowser I can emulate mouse click, getting the HTML element by Id and invoking it.
WebBrowser.Document.GetElementById("commit").InvokeMember("click");
My question is: How can I mix these to:
Press the button (one time or multiple times, the id remains unchanged).
Download updated source code (after button click).
P.S. As you can guess the button is a "View more" button that loads new elements, and the url stays the same all the time, there is no page 2. That's why I have a problem.
P.S. This looks like my problem, but in my case it's the same page, not a new window
Let me tell you that it makes no sense to use the WebBrowser and WebClient in conjunction like that. The WebBrowser is not an abstraction of the WebClient or anything like that, they are completely different and they are completely separate.
Assuming you want to persue this problem using the WebBrowser, you could download the page source using the WebBrowser component like so:
webBrowser1.Document.GetElementById("commit").InvokeMember("click");
webBrowser1.DocumentCompleted += (o, args) =>
{
string pageSource = webBrowser1.DocumentText;
};

Save current window URL and move back to that URL after couple of navigation

I have a site and in my site when user click on a specific div then a new child window open . Suppose the user is standing at xyz.com/category , now if you user clicks on that div then parent windows goes to another url suppose google.com and a popup window will get open and there are some more navigation in popup windows . But every page that will open in popup has a button called Go Back To Site . I want that when user clicks on several links in popup and then click on Go Back To Site button then popup window get closed and parent window move back to xyz.com/category.
You can try PHP Superglobal array
$_SERVER['HTTP_REFERER']
If you can use javascript for this, there are simple functions such as:
window.history.back();
window.history.forward();
You can refer all of them in following link:
https://developer.mozilla.org/en/docs/DOM/Manipulating_the_browser_history
For Jquery:
$(document).ready(function() {
var referrer = document.referrer;
});
For C# :
WebBrowser1.Url = new Uri("http://maps.google.com");
- document.referrer does not work for IE
For this issue refer following code:
<script type="text/javascript" >
function redirect(url) {
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
var referLink = document.createElement('a');
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
} else {
location.href = url;
}
}
</script>
This code can also be refered over here
The way I'd do it is to pass the current URL as a parameter into your popup. So you would change your anchor link tags to this:
Click
So when your popup pops up, you just grab the value of prevPage and then when they click "Back to previous page", you just redirect to the value of prevPage.

Categories

Resources