I obtain the window object in DWebBrowserEvents2_Event.NavigateComplete2 event. Following is some stripped down code snippets:
global is the window object.
contentPort is a private host object.
The private host object callback works but the window callbacks do not.
public void OnNavigateComplete(object pDisp, ref object URL)
{
var browser = pDisp as IWebBrowser2;
var document2 = browser?.Document as IHTMLDocument2;
var window = document2?.parentWindow as IHTMLWindow2;
var engine = new JScriptEngine("{16d51579-a30b-4c8b-a276-0ff4dc41e755}", null, WindowsScriptEngineFlags.EnableStandardsMode | WindowsScriptEngineFlags.MarshalNullAsDispatch | WindowsScriptEngineFlags.MarshalArraysByValue);
engine.AddHostObject("window", HostItemFlags.GlobalMembers, window);
}
And the JavaScript snippet:global is the window object.
contentPort is a private host object.
contentPort.addMessageListener(function (msg) {
if (global.document.readyState !== "complete") {
global.addEventListener('load', function () {
console.log('message document load');
}, false);
console.log("Message received - waiting for document load");
} else {
setTimeout(function () {
console.log('message timeout');
});
console.log("Message received");
}
}
The console output displays either "Message received" or "Message received - waiting for document load" but never "message document load" or "message timeout"The private host object callback works but the window callbacks do not.