Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use <code>window.external</code> to call a C# method when a global function is fired in JavaScript. See <a href="http://msdn.microsoft.com/en-us/library/w290k23d.aspx" rel="nofollow noreferrer">WebBrowser Control Overview</a> for details on window.external.</p> <p>You'll need to set ObjectForScripting: <a href="https://stackoverflow.com/questions/499743/webbrowser-controls-window-external-is-always-null">Webbrowser control's window.external is ALWAYS null.</a> for this to work.</p> <p>Take @Krishna's answer to add the JavaScript (but drop jQuery because it won't be needed):</p> <pre><code>private void addScript(HtmlElement head, string scriptSource) { HtmlElement lhe_script = head.Document.CreateElement("script"); IHTMLScriptElement script = (IHTMLScriptElement)lhe_script.DomElement; script.src = scriptSource; head.AppendChild(lhe_script); } addScript(WebBrowser.Head, @"InjectMonitor.js"); </code></pre> <p>The JavaScript below (InjectMonitor.js) will find all global functions and attach your specified handler:</p> <pre><code>function augment(withFn) { var name, fn; for (name in window) { fn = window[name]; if (typeof fn === 'function') { window[name] = (function(name, fn) { var args = arguments; return function() { withFn.apply(this, args); fn.apply(this, arguments); }; })(name, fn); } } } augment(function(name, fn) { console.log("calling " + name, fn); // window.external.yourC#method }); </code></pre> <p>In this example, taken from <a href="https://stackoverflow.com/questions/5033836/adding-console-log-to-every-function-automatically">Adding Console Log to Every Function</a>, it just logs the call to console; but using window.external you could send some message back to your C# application with details of what function was called from the client.</p> <p>Finally, here's a JS Bin example (run it and don't forget the console): <a href="http://jsbin.com/uvotuc/4/edit" rel="nofollow noreferrer">JS Bin Example</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload