Controling Modal Dialogs

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi All,

Sorry for the lengthy post. I am trying to explain the scenario as well as
give
all the details in order to minimize the need for questions and repostings...

I have a WinForms application that hosts the WebBrowser control. This
application
needs to work stand alone on a server (without enduser intervention).
This means that it needs to automatically handle all new windows that are
opend by
the web site (web application). The web sites are predefined intranet web
applications. However, I have no control over their implementation.

In order to trap new windows created by script calls to window.open, I
impleneted
DWebBrowserEvents2 and sinked events by overriding CreateSink.
My implementation of the NewWindow3 method creates a new tab in my
Form and a new browser control in that tab. This works fine.

I am now trying to solve the problem of controling dialogs created by
window.showModalDialog. My application needs to be able to control wether
they are created or not, and if they are created to manipulate their DOM.
After reading in several places that this cannot be done I went ahead and
tried to
solve the problem. I have managed a partial solution so far. I managed to
get
a callback function called in my application whenever a script calls
window.showModalDialog. See implementation details below.

My question is, now that I have captured the call what to do with it.
I have three possible options, and my application will know at runtime which
of
these options is required for a particular dialog.
1. Don't show the dialog and just return a fabricated return value as if the
dialog
was shown. This is not a problem.
2. Let the dialog show and don't interact with it. This is usfull in a server
application only if the dialog closes itself after a while. I know how
to do this too.
I just call the IHTMLWindow2.showModalDialog myself.
3. Let the dialog show and interact with its DOM. This is my problem. The
IHTMLWindow2.showModalDialog doesn't give me access to the
dialog's DOM (as far as I know). I thought about the following
workarounds.

Any other suggestions would be most welcome.

A. Use IHostDialogHelper.ShowHTMLDialog. This may be good if the last
parameter to the function (IUnknown *punkHost) gives me access to the
DOM somehow. I was unable to find any documentation about this
parameter.

B. Host MSHTML myself for the dialog's HTML. I am not sure this would
work if
the script in the dialog would access members of the IHTMLDialog
interface
that is implemented by the window object created by
IHTMLWindow2.showModalDialog.

C. Create a new tab and new CustomWebBrowser, as I do for window.open call,
and have it navigate to the dialog's URL.
About this solution I am not sure how to handle the modality
considerations.
Other tabs in my application need to be disabled. My implementation
of
showModalDialog needs to return only when the created browser exits.
Again, what would happen if (when) the script calls items in the
IHTMLDialog
interface, like IHTMLDialog.returnValue.

Any help would be greatly appreciated.

Finally, here are the details of my implementation.

In my derived
class CustomWebBrowser : System.Windows.Forms.WebBrowser
In the constructor I set the ObjectForScripting as below. This object has an
implementation of a method named showModalDialog, after assigning
the ObjectForScripting property this method is available to script through
the window.external.showModalDialog reference. Now all that remains in
order to hook this method is to replace the script's window.showModalDialog.
This is done in the implementation of DWebBrowserEvents2.DocumentComplete
method by executing javascript code that replaces the window.showModalDialog
with 'function(dialog, varArgIn, varOptions){ return
window.external.showModalDialog(dialog, varArgIn, varOptions); }
See the main implementation details below.

public class CustomWebBrowser : System.Windows.Forms.WebBrowser
{
public CustomWebBrowser()
: base()
{
this.ObjectForScripting = new ScriptingCallableObject(this);
}
....
protected override void CreateSink()
{
base.CreateSink();
m_eventsImplementation =
new DWebBrowserEvents2Implementation(this);
eventsCookie =
new AxHost.ConnectionPointCookie(this.ActiveXInstance,
m_eventsImplementation,
typeof(DWebBrowserEvents2));
}
....

public class DWebBrowserEvents2Implementation : DWebBrowserEvents2
{
public DWebBrowserEvents2Implementation(CustomWebBrowser browser)
{
m_Browser = browser;
}
....
[DispId(259)]
public void DocumentComplete(object pDisp, ref object URL)
{
IHTMLDocument2 doc2 = m_Browser.IE.Document as IHTMLDocument2;
doc2.parentWindow.execScript("if
(typeof(gpfn_OrigShowModalDialog) == \"undefined\") {
gpfn_OrigShowModalDialog = window.showModalDialog; window.showModalDialog =
function(dialog, varArgIn, varOptions){ return
window.external.showModalDialog(dialog, varArgIn, varOptions); }; }",
"javascript");
}
....
}


....
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class ScriptingCallableObject
{
public ScriptingCallableObject(CustomWebBrowser browser)
{
m_WebBrowser = browser;
}

public object showModalDialog(string dialog, object varArgIn,
string varOptions)
{
// This implementation preserves the default behavior.
// but I can implenet whatever I want here.
IHTMLDocument2 doc2 = m_WebBrowser.IE.Document as
IHTMLDocument2;
object options = varOptions;
return doc2.parentWindow.showModalDialog(dialog, ref
varArgIn, ref options);
}

private CustomWebBrowser m_WebBrowser;
}
}


---
Adar Wesley

.



Relevant Pages

  • Sencha Touch--Support 2 browsers in just 228K!
    ... It is advertised as the first "HTML5 framework" based ... very little of the script relates to HTML5. ... several of its key features rely on UA-based browser sniffing. ... iPhone/iPod/iPad devices account for 90% of the mobile market. ...
    (comp.lang.javascript)
  • Re: Sencha Touch--Support 2 browsers in just 228K!
    ... Normalizes currentStyle and computedStyle. ... display style of "none" or any number of possibilities in IE). ... It is not set anywhere in this script. ... browser designs based on retrieving accurate computed style values are ...
    (comp.lang.javascript)
  • Re: Which Is The Better Approach To Working With Javascript?
    ... implementation has no interprocess communication capability, or ability to interface with a script interpreter. ... Java SCRIPT runs in the browser exclusively. ... No language is written just for a single environment. ...
    (comp.lang.php)
  • Re: Absolute element offsets--exercise in futility
    ... browser sniffing in lieu of feature testing. ... implemented in the latest rewrite of jQuery purports only to support ... it is dynamic script injection. ...
    (comp.lang.javascript)
  • running a script from a function
    ... script file when GUI opens, ... function mygui_OpeningFcn(hObject, eventdata, handles, varargin) ... variable not come to the workspace. ...
    (comp.soft-sys.matlab)