Does the .NET WebBrowser control support console.log? [duplicate] - c#

So I'm writing a Javascript coding UI using C# Windows Forms. This is my code for when the "Run" button is pressed, in case it helps:
//If the button in the upper-right corner is clicked
private void run_Click(object sender, EventArgs e)
{
//If the program isn't running
if (!running)
{
//Show the web browser
webBrowser1.Visible = true;
richTextBox1.Visible = false;
//Set the label to Running
status.Text = "Running";
//Set the html text to this below
webBrowser1.DocumentText = "<!DOCTYPE html><html><body><script>\n" + richTextBox1.Text + "\n</script></body></html>";
//Set the "run" button to "stop"
run.Text = "Stop";
//Set the status to running
running = true;
}
//otherwise
else
{
//Show the text box
webBrowser1.Visible = false;
richTextBox1.Visible = true;
//Set the label to Ready
status.Text = "Ready";
//Go to nothingness
webBrowser1.Navigate("about:blank");
//Set the "stop" button to "run"
run.Text = "Run";
//Set the status to not running
running = false;
}
}
I run the program, and for the most part, everything works fine. However, when I try to use the console.log() command, the following error appears:
'console' is undefined
I also try Console.Log (I actually don't know Javascript; just trying my best) but that returns the same error, that 'Console' is undefined.
Also, once I get console.log working, how do I open the console on the WebBrowser control? I've tried searching the internet, but nothing has come up on either of these questions.

You can get JavaScript console output from within Visual Studio.
By default the webBrowser1 control uses IE7 to render it's output. IE7 does not have a console.log() function. In order to get the console.log() function to work, you need to add the following meta tag:
<meta http-equiv="X-UA-Compatible" content="IE=11">
'IE=8' or greater should make the console.log() available to you.
When you debug a Windows Forms application it debugs using the .NET Managed Code debugger. In order to debug differently, instead of pressing 'Play' to debug, try selecting "Debug" > "Start without Debugging". Now once your application is running, go to "Debug" > "Attach to Process" and find your WindowsFormsApplication.exe, attach to it using the Script Code Debugger instead of the .NET Managed Code debugger.
Now, in Visual Studio:
You can open "Debug" > "Windows" > "JavaScript Console"
You can also open "Debug" > "Windows" > "DOM Explorer"

Yes, Console class is not available in your browser control, but you can create a logger class like this
[ComVisible(true)]
public class Logger
{
public void log(string s)
{
Console.WriteLine(s);
}
}
and use it in your browser control
webBrowser1.ObjectForScripting = new Logger();
webBrowser1.DocumentText = "<script>external.log('TEST');</script>";

Related

Detect if WPF application is running or in Visual Studio windows visualizer

I have a WPF application which opens a popup when the main window is loaded. The problem is when I select the .xaml file in the solution explorer in Visual Studio 2013, the popup "pops" even when the application is not running. I suppose it is an intended behavior since the visualizer needs to execute the code in order to render the page layout, but for now I need to close it every time I load the page... I cannot temporarily disable this popup since it has some start logic for the application (selection of a location,...).
Here is the code of the popup trigger
public GeneralProcess() //usercontrol
{
InitializeComponent();
Loaded += GeneralProcess_Loaded;
}
void GeneralProcess_Loaded(object sender, RoutedEventArgs e)
{
var popup = new StationSelect();
popup.Owner = Window.GetWindow(this);
popup.ShowDialog();
}
Is there a way to know if the application is running or if I am in the visualizer, or is there a way to disable the Loadedevent just for visual studio ? The goal is to still be able to see the page for easy editing.
EDIT : this question is a duplicate. However this answer worked for me.
void GeneralProcess_Loaded(object sender, RoutedEventArgs e)
{
if (DesignerProperties.GetIsInDesignMode(this))
return;
var popup = new StationSelect();
popup.Owner = Window.GetWindow(this);
popup.ShowDialog();
}

Alert or Open login window while switching between application using wpf application

Let me first explain the scenario:
If in case , I have more than two windows open say for example notepad++,paint,chrome,Skype etc. Now any of the these windows come into focus using ALT+TAB or on left click of mouse. The alert or login window created using wpf should always open.In short it should get alert on each of window come into focus in my computer whether it is notepad++, chrome, vlc or any application listed as open(running application) in task manager.
I tried below code but it throws alert only when any of application is opened for the first time and not while switching between already opened applications so far.
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children,
(sender, e) =>
{
var element = sender as AutomationElement;
this.Dispatcher.Invoke(() =>
{
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
this.Topmost = true;
this.WindowState = System.Windows.WindowState.Normal;
MessageBox.Show("window opened");
});
Hope the point is clear. I m stuck here for switching between the windows scenario. Thanks

Hiding browser popups - Watin

I'm using Watin library in a windows forms app. In order to hide the browser I use this instruction :
Settings.Instance.MakeNewIeInstanceVisible = false;
However, it doesn't hide the popups (when simulating a click on an element that opens a popup).
Is there a way to hide them?
You can do it programmatically by running some javascript code and make window.open function to do nothing!
Example
Here is a test page I made that has a very simple form and when the user clicks the Sum button, it sums up numA + numB and it displays the result inside a <span id="result"></span> element. After the result text update, it opens a popup window by calling window.open. In order to make this popup window disappear, we need to eliminate the window.open function:
window['open'] = function() { return false; }
To do that using Watin, we have to use the Eval function and inject the javascript code like this:
browser.Eval("window['open'] = function() { return false; }");
Then all popups are gone for that page load only and we have the wanted result.
Sample C# code
private void buttonPopupdEnabled_Click(object sender, EventArgs e)
{
WatiN.Core.Settings.Instance.MakeNewIeInstanceVisible = false;
IE ie = new IE();
ie.GoTo("http://zikro.gr/dbg/html/watin-disable-popups/");
ie.Eval("window['open'] = function() { return false; }");
ie.TextField(Find.ById("numA")).TypeText("15");
ie.TextField(Find.ById("numB")).TypeText("21");
ie.Button(Find.ById("sum")).Click();
string result = ie.Span(Find.ById("result")).Text;
ie.Close();
labelResult.Text = String.Format("The result is {0}", result);
}
Program running before javascript injection (Popup shows up)
Program running after javascript injection (Popup is gone)
I've checked released notes and found this:
By default WatiN tests make the created Internet Explorer instances
visible to the user. You can run your test invisible by changing the
following setting. Be aware that HTMLDialogs and any popup windows
will be shown even if you set this setting to false (this is default
behavior of Internet Explorer which currently can't be suppressed).
IE.Settings.MakeNewIeInstanceVisible = false; // default is true
Since WatIN haven't updated since 2011, I think you wouldn't expect any new feature support what you want.
I don't know if this could be a workaround but If those popups are not important to you why just don't block all popups?
How to turn off popup blocker through code in Watin?
HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WEBOC_POPUPMANAGEMENT
Value = 0 for Off
Value = 1 for On

c# winform above chrome in kiosk mode

I'm having trouble getting a winform to appear above chrome in kiosk mode.
My goal is to have a transparent window display certain system information on top of all other windows. I have set this.TopMost = true and have even added the script:
private void keepWindowOnTop()
{
while (true)
{
if (!this.TopMost || !this.TopLevel)
{
this.TopMost = true;
this.TopLevel = true;
}
Thread.Sleep(1);
}
}
Still, when my winform launches chrome kiosk is on top.
Any suggestions would be greatly appreciated!
Edit: I need the transparent window to always be in the foreground even when other windows are selected (clicked). This feature works fine when running from Visual Studios, but after the application is installed on a separate system it breaks.
Could it be because I'm importing the .dll at run time? Does that have any effect on TopLevel?
cheers

WebBrowser Control continue script execution without script error warning box

I have to call 3rd party CRM system. There are some issues in their javascript code, but webpage functions correctly in IE9.
If I have ScriptErrorsSuppressed set to false I will get error message - "An error has occurred in this script on this page" and at bottom - "Do you want to continue running scripts on this page?"
If I click "Yes" web page will work correctly, "No" on other hand will not bind jquery correctly to objects.
If I set ScriptErrorsSuppressed to true, there will be no error message, however this will stop executing script and web page will not work correctly.
IE9 can display script errors, but keep on executing script. Is there a way to emulate same behaviour?
I have tried all solutions propoused in stack overflow, but they stop script executing
Web browser control runs in IE9 mode, I set registry value to force IE9
Thank you
-Maigais
We have almost similar tasks. I tried everything including WebBrowser.ScriptErrorsSuppressed = true. And this was not what I was looking for because all JavaScripts stopped working after an exception has appeared.
But the next one worked for me:
public PageWithWebBrowser()
{
WebBrowser.DocumentCompleted += OnWebBrowserDocumentCompleted;
WebBrowser.Navigate("http://www.example.com");
}
private void OnWebBrowserDocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser.Document.Window.Error += OnWebBrowserDocumentWindowError;
}
private void OnWebBrowserDocumentWindowError(object sender, System.Windows.Forms.HtmlElementErrorEventArgs e)
{
//Suppresses a dialog and continues running scripts on the page
e.Handled = true;
}
As you can see I use System.Windows.Forms.WebBrowser control for that, may be it is going to work with WPF WebBrowser control.
Hope this will help.

Categories

Resources