Opening a hyperlink in a default browser instead of MSIE - c#

Using SpiceLogic WinForms HTML Editor .NET component which is no longer supported by developer.
Here's the code to what I need:
Opening a hyperlink in a default browser instead of MSIE
https://www.spicelogic.com/docs/WinHTMLEditorControl/Programming/459
It cancels WebBrowserNavigatingEventArgs in the webBrowser_Navigating event.
But not working for me, because I use the editor in Preview Only Mode (read-only mode)
In this mode, browser navigation is disabled and we fix all href links to open in new windows (mimic email client behavior):
HTMLEditor.GetThePreviewWebBrowser.AllowNavigation = False
So that even is never fired, but since it's based on MSIE (Microsoft.mshtml.dll) I though maybe someone familiar with mshtml can suggest a workaround to use it to open links in the default browser rather than IE?
Thanks for help :)
Tried their sample code!

Related

Download js generated html with C#

There is a reports website which content I want to parse in C#. I tried downloading the html with WebClient but then I don't get the complete source since most of it is generated via js when I visit the website.
I tried using WebBrowser but could't get it to work in a console app, even after using Application.Run() and SetApartmentState(ApartmentState.STA).
Is there another way to access this generated html? I also took a look into mshtml but couldn't figure it out.
Thanks
The Javascript is executed by the browser. If your console app gets the JS, then it is working as expected, and what you really need is for your console app to execute the JS code that was downloaded.
You can use a headless browser - XBrowser may server.
If not, try HtmlUnit as described in this blog post.
Just a comment here. There shouldn't be any difference between performing an HTTP request with some C# code and the request generated by a browser. If the target web page is getting confused and not generating the correct markup because it can't make heads or tails of from the type of browser it thinks it's serving then maybe all you have to do is set the user agent like so:
((HttpWebRequest)myWebClientRequest).UserAgent = "<a valid user agent>";
For example, my current user agent is:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Maybe once you do that the page will work correctly. There may be other factors at work here, such as the referrer and so on, but I would try this first and see if it works.
Your best bet is to abandon the console app route and build a Windows Forms application. In that case the WebBrowser will work without any work needed.

.NET 4.0 Web browser control and msIsSiteMode javascript error

I have an application that connects to www.Jango.com and using a web browser control. However when using the .NET browser control I constantly receive a script error of “Unable to get value of the property 'msIsSideMode': object is null or undefined”, and therefore the rest of the site does not load.
This can be reproduced by creating a simple windows forms application, adding a webbrowser control and navigating to jango.com
As far as I can tell, the web browser control renders a website based on the version of IE installed on your machine. On my machine I have IE9 installed. The method msIsSiteMode appears to be an IE method that tells weather the current page was launched as a pinned site. Since jango.com is very JavaScript based, this null value causes the website to stop functioning correctly. However navigating to jango.com in Internet Explorer works just fine and no errors are produced.
Is anyone aware of a way to work around this, such as having my application set the value? Have the web browser control render as a different version. Any suggestions at all, I am open to anything, except changing the version of IE on my machine as this is not an option.
Set the ScriptErrorsSuppressed property to True on the WebBrowser control.
EDIT: One more alternative
Try this on the Form that hosts the web user control:
using System.Runtime.InteropServices;
using System.Security.Permissions;
[ComVisibleAttribute(true)]
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.ObjectForScripting = this;
}
...
}
Read here for more detailed information.
I see similar problem in WebBrower Control with IE9 installed on my machine. In my case, I have two windows forms with WebBrowser Controls. One of those is used to load the pop up from the other.I get the script error on pop up one.its only with IE9

How do I determine the correct browser version when compatability mode is turned on

I need to determine which version of browser the user is using, however compatibility mode is turned on by default for the entire company.
On the server side script, how do I determine the real browser version?
Thanks
Update
I've got my page correctly showing the IE version using document.documentMode, however I can't figure out how to pass this over to the server side so I can use it.
Request.Browser will give you complete browser information, where you can check version, browser name, browser type etc.
Request.Browser.Version // Return complete browser version infor
Request.Browser.Browser // If browser is IE then it will return **IE**
document.documentMode in javascript was the solution.
<script>
alert(document.documentMode);
<script>
Rather than fighting compatibility mode, you can turn it off for your specific web application. We do this on all of our sites because compatibility mode really screws a lot of things up.
Force IE compatibility mode off using tags
Use HttpContext.Current.Request.UserAgent on server if on client then user navigator.userAgent
and then instructions from below ie7 has no trident keyword but ie8 is trident/4 and IE5 is trident/5.
First look for MSIE x.x, if x.x is 7 then look for Trident/y.y. If trident is missing then it IE7 if y.y is 4 then its IE8 and if y.y is 5 then ie9 and if y.y is 6 then ie10
http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx

How to launch a browser and later direct it to a page?

I need to launch a browser, do some work and then make the browser navigate to a URL (in that order).
The first part is of course simple and I have a Process object. I am at a loss as to how to later direct it to the target page?
How do I treat the Process as a browser and make it navigate to the desired page?
Any help, pointers, code snippets appreciated.
Instead of launching the browser & then navigating to the page, just tell the OS that you want to run the URL. Windows will pick the correct browser, and navigate the user to the given URL.
System.Diagnostics.Process.Start("http://www.StackOverflow.com");
If you don't need to do this in production, you could use a testing library such as WatiN to do this:
using WatiN.Core;
//Placeholder page to launch initial browser
IE ie = new IE("http://www.google.com");
DoSomeWork();
//Now navigate to the page you want
ie.GoTo("http://stackoverflow.com");
My first instinct for this question was DDE, but it appears that has been decommissioned in Windows Vista so that is no good. Shame, as it was the only consistent mechanism in Windows for Interprocess Communication (IPC)...oh how I miss Arexx on the Amiga.
Anyhow, I believe the following will work but unfortunately, due to the way it works, it launches Internet Explorer irrespective of the configured browser.
If your application has a Form, then create a WebBrowser control on it. Set this to non-visible as we are only making use of its as a launching device rather than to display the web page.
In code, at the point where you want to show a web page, use the following code:
webBrowser1.DocumentText = "window.open('How to launch a browser and later direct it to a page?', 'BananasAreOhSoYummy');";
What this does is to tell the WebBrowser control, which is just the IE in disguise, to open a new window called 'BananasAreOhSoYummy'. Because we have given the window a name, we can use that line repeatedly, with different URLs, to change the page in that particular browser window. (A new window will be opened if the user has happened to close it.)
I will have a think about an approach that honours the user's default browser choice.
If you don't need the actual instance of IE, you can use the System.Windows.Forms.WebBrowser control.
I think instead of sending the browser a url you could send it javascript that would run and direct the browser to a site.
Not sure if this would work but I see no reason why it wouldn't

How do I open alternative webbrowser (Mozilla or Firefox) and show the specific url?

I know there is built-in Internet explorer, but what I'm looking for is to open Firefox/Mozilla window (run the application) with specified URL. Anyone can tell me how to do that in C# (.nET) ?
You can do this:
System.Diagnostics.Process.Start("firefox.exe", "http://www.google.com");
This will launch the system defined default browser:
string url = "http://stackoverflow.com/";
System.Diagnostics.Process.Start(url);
Remember that Process.Start(url) might throw exceptions if the browser is not configured correctly.
See ProcessInfo.UseShellExecute
Use the Process class (System.Diagnostics) using the URL as the process name. This will use the system default browser to open the URL. If you specify a browser, you run the risk that the browser doesn't exist.
In Visual Studio click the File -> Browse With... on the menus and then select the browser that you want to use. You can also change the browser there. If the Browse With... menu option doesn't appear then you need to select a project from your solution that that can be launched in a browser.
If you explicitly do not want to use the User's default browser, you can run the browser with the URL as the first argument.
C:\Program Files\Mozilla Firefox>firefox.exe http://google.com
launches Firefox with google for me. But as people have said, you run the risk of it not being installed, or being installed to a different place, etc.

Categories

Resources