Change contents without redirecting to about:blank - c#

I'm currently working on an application that modifies a specific web page to hide irrelevant information and the displays it in a WebBrowser control in the application window. Unfortunately as soon as i set the DocumentText Property of the WebBrowser, it navigates to about:blank and the displays the HTML content. However, because it redirects to about:blank, all relative element in the web page become invalid, creating a very odd looking web page with no stylesheet what so ever.
Is there a way i can modify what the WebBrowser control displays, without having it redirect to about:blank and therefore ruining all relative elements?

This should work for injecting a HTML Element to your page, without resetting the rest of the DOM:
private void button1_Click(object sender, EventArgs e)
{
HtmlElement myElem = webBrowser1.Document.CreateElement("input");
dynamic element = myElem.DomElement;
element.SetAttribute("value", "Hello, World!");
(webBrowser1.Document.GetElementsByTagName("body")[0]).AppendChild(myElem);
}

Related

how to display full Url in web browser control properly in C#?

I can display the Url in the searchbar. However, it picks up javascript and other loading processes as URL and displays them and doesn't display a conventional Url (e.g https://stackoverflow.com/questions) that we would see in any common browser. So if I search http://www.stackoverflow.com, I get https://ssum-sec.casalemedia.com/usermatch?s=183712&cb=https://engine.adzerk.net/udb/22/sync/i.gif?partnerId=1&userId=. Any help would be appreciated.
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
player.SoundLocation = "safepage.wav";
player.Play();
if (SearchBar.Text != e.Url.ToString()) //Displays the full Webpage address within the textbox
{
SearchBar.Text = e.Url.ToString();
}
}
Navigation events of web browser control will raise for iframes of the page too. So in this case, what you are getting as result is address of an iframe in the page.
You can use webBrowser1.Url.ToString() instead of e.Url.ToString().
Note: As far as I know, there should not be an iframe in stackoverflow questions page, so itt seems your browser has been infected.
In the _Navigated event, you can use the WebBrowser.Url property. It will get updated each time but not for the redirects like for those tracking/ad scripts.

Wait till the GeckoFX Webbrowser has loaded

I want to automate a few tasks on my website with GeckoFX for some testing.
That should happen when I click a button and everything should be automated after that button click.
This includes clicking buttons where the page refreshes so the code has to wait till the page has loaded and that's where my problem is.
If I do it like that:
geckoWebBrowser1.Navigate("http://mywebsite.com");
GeckoInputElement searchText = new GeckoInputElement(geckoWebBrowser1.Document.GetElementsByName("searchbox")[0].DomObject);
searchText.Value = "GeckoFx";
I get an error, so how can I put it that the code after .Navigate waits till the webbrowser has fully loaded the page?
You can use DocumentCompleted Method to perform your automatic operations.
private void geckoWebBrowser1_DocumentCompleted(object sender,EventArgs e)
{
// Here you can add the coding to perform after document loaded
}
For example : First initiate the browser to google page by geckoWebBrowser1.Navigate("https://www.google.com");
After google page loaded you can do the following in document_completed method:
GeckoInputElement search =new GeckoInputElement(geckoWebBrowser2.Document.GetElementsByName("q")[0].DomObject);
GeckoInputElement button = new GeckoInputElement(geckoWebBrowser2.Document.GetElementsByName("btnG")[0].DomObject);
search.focus();
search.Value = "Master Blaster Sachin";
button.Click();
so it will search the value you given automatically after the google page loaded. Like that you can modify the program as per your logic. Hope it helps..
I would go an use a product like Selenium http://seleniumhq.org/. It's free open source web testing which is scriptable.

Page is cut while printing with the Windows.Forms.WebBrowser control

I have trouble with the printing function of the WebBrowser control.
First i load my page and it is rendered correct.
Then i set the header/footer/margins and the right printer:
webbrowser printing;
How do I programatically change printer settings with the WebBrowser control?
That works so far. Then i use myBrowser.Print();
But my website does not print right. Only the upper left corner is printed, a few centimeters and then there are scrollbars.
I printed my website with the IE9 and everything was alright. Also tried different browsermodes and documentmodes. No problem.
And i thought the control and the IE are technically the same...
Are there any parameters i have forgotten?
The website i want to print is old and has no doctype. But since the control displays it correct, I expect it to print correct, too.
Edit:
Found out that it has to do with javascript on the website, which does not run for printing.
Is there a way to get the HTML from the manipulated DOM?
For the print function no external documents are processed. All documents for a website have to be merged into one printable site. To get the other documents you have to cast the myBrowser.Document.DomDocument to an IHTMLDocument2. From that IHTMLDocument2 you can extract the CSS or JS to put it into the html.
For Example:
void myBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
myBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(myBrowser_DocumentCompleted);
myBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(myBrowser_DocumentPrintable);
String mySource = myBrowser.DocumentText;
// Get the CSS
IHTMLDocument2 doc = (myBrowser.Document.DomDocument) as IHTMLDocument2;
myCSS = doc.styleSheets.item(0).cssText;
mySource = mySource.Replace("<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/style.css\">", "<style type=\"text/css\">"+myCSS+"</style>");
// Reload
myBrowser.DocumentText = mySource;
}
void myBrowser_DocumentPrintable(object sender, WebBrowserDocumentCompletedEventArgs e)
{
myBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(myBrowser_DocumentPrintable);
myBrowser.Print();
}

How can I inject javascript into all pages including iframe/frame when using C# WebBrowser Control?

I know there are many discussions about it, but I cannot find the solution for my own problem.
Currently I want to inject all pages including iframes or frames even iframes' iframes when using C# WebBrowser Control. For example, I want to inject scripts, e.g. alert("hello"), before the pages display.
It is quite easy when there is only a main page.
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
WebBrowser curWebBrowser = (WebBrowser)sender;
HtmlElement head = curWebBrowser.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = curWebBrowser.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text ="alert('hello');";
head.AppendChild(scriptEl);
}
But when it occurs to inject scripts into all pages, I found I do not know which iframe is navigated in Navigated Event.
There is e.URL so I know its main page or not.But there is no further information I can find.
Currently I traverse all frames in the browser filtered by e.URL (sometimes two iframes are with same URL) and try to avoid the pages execute scripts repeatedly. But I think What I do is really stupid and it does not work totally as the pages in iframes alert messages "after" the pages are displayed.
Are there any better suggestions?

Response.Redirect causes download of the swf

I have a flash image slider with a button below each image.
When i press that button, the user is redirected to a new page
where i add that image product to my cart.
The problem is that after doing the adding, i want to redirect the user back to the initial page.
The code:
protected void Page_Load(object sender, EventArgs e)
{
addProductToBasket(getCategoryIdFromUrl(), getProductIdFromUrl());
Response.Redirect(Request.UrlReferrer.ToString());
}
Please note that in Firefox is working fine but in IE or Chrome it is DOWNLOADING the swf...If i comment Response.Redict(...) the user remains on this page so the click button is working well, only the redirect seems to be the problem.
Any suggestions please?
Edit: The problem seems to be that Request.UrlReferrer keeps as link not the initial page containing the swf but the swf itself....
So, instead of doing redirect to:
http://localhost:1336/Site/Index.aspx
if does redirect to the swf contained on the Index.aspx page
http://localhost:1336/carousel/carouse.swf
Solved: with a session variable where i keep the initial page's url
It seems to me that it's a function of the flash player setting the referrer header differently in different browsers.
If that is the case, then you might want to have the flash player get the url of the page it is hosted on, and pass that as a parameter to your page, and then redirect to the contents of the parameter.
What I think you really should be doing though is registering an HttpHandler (IHttpHandler implementation) for the URL which performs the processing and creating a response which returns JSON or XML, which flash can easily parse.
This way, you don't have a page reload and you have a seamless experience.

Categories

Resources