reciveing window.postMessage

Add-in Express™ Support Service
That's what is more important than anything else

reciveing window.postMessage
window3.attachEvent("onmessage", new EventHandler(processMessage)) does not work 
Aleksandra Grosset




Posts: 8
Joined: 2016-06-01
How to recive messages sended by window.postMessage ?

my code:


private void IEModule_DocumentComplete2(object pDisp, string url, bool rootDocLoaded)
{
(...)
if (rootDocLoaded)
{
//IHTMLWindow3 window3 = (IHTMLWindow3)(this.HTMLDocument.documentElement.parentElement);
IHTMLWindow3 window3 = (IHTMLWindow3)(this.HTMLDocument.parentWindow);
this.alert.viewError(window3.ToString());
window3.attachEvent("onmessage", new EventHandler(processMessage));
//window3.print();
}
}

private void processMessage(Object sender, EventArgs e)
{

//throw new Exception("message!!!");
this.alert.viewError("message!!!");
}


window3.ToString() prints IHTMLWindow2
window3.print() works just fine...
Posted 22 Jun, 2016 06:39:01 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Aleksandra,

Please try the code below:


        public IE.WebBrowser IEApp
        {
            get
            {
                return (this.IEObj as IE.WebBrowser);
            }
        }
 
        public mshtml.HTMLDocument HTMLDocument
        {
            get
            {
                return (this.HTMLDocumentObj as mshtml.HTMLDocument);
            }
        }

        Window3OnScroll_EventListener onscroll;
        Window3OnMessage_EventListener onmessage;

        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.AutoDispatch)]
        public class Window3OnScroll_EventListener
        {
            IEModule module;

            internal Window3OnScroll_EventListener(IEModule module)
            {
                this.module = module;
            }

            [DispId(0)]
            public void HandleEvent(object pEvtObj)
            {
                try
                {
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (pEvtObj != null)
                        Marshal.ReleaseComObject(pEvtObj);
                }
            }
        }

        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.AutoDispatch)]
        public class Window3OnMessage_EventListener
        {
            IEModule module;

            internal Window3OnMessage_EventListener(IEModule module)
            {
                this.module = module;
            }

            [DispId(0)]
            public void HandleEvent(object pEvtObj)
            {
                try
                {
                    mshtml.IHTMLEventObj2 event_ = pEvtObj as mshtml.IHTMLEventObj2;
                    if (event_ != null)
                        MessageBox.Show(this.module, "OnMessage handler");
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (pEvtObj != null)
                        Marshal.ReleaseComObject(pEvtObj);
                }
            }
        }

        private void IEModule_OnConnect(object sender, int threadId)
        {
            this.onscroll = new Window3OnScroll_EventListener(this);
            this.onmessage = new Window3OnMessage_EventListener(this);
        }

        private void adxieContextMenuCommandItem1_OnClick(object sender, object htmlElement)
        {
            HTMLDocument.parentWindow.GetType().InvokeMember("postMessage", BindingFlags.InvokeMethod, null, HTMLDocument.parentWindow, new object[] { "Message", "*" });
        }

        private void IEModule_DocumentComplete2(object pDisp, string url, bool rootDocLoaded)
        {
            if (rootDocLoaded)
            {
                mshtml.IHTMLWindow3 hWnd3 = HTMLDocument.parentWindow as mshtml.IHTMLWindow3;
                if (hWnd3 != null)
                {
                    try
                    {
                        hWnd3.attachEvent("onscroll", this.onscroll);
                        hWnd3.attachEvent("onmessage", this.onmessage);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
Posted 22 Jun, 2016 17:02:16 Top
Aleksandra Grosset




Posts: 8
Joined: 2016-06-01
Should that code work also with AddinExpress.IE.ADXIEToolbarModule ?

My plugin is Tooolbar, and this code does not work with it...
Posted 23 Jun, 2016 06:23:36 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Aleksandra,

Yes, it should work in all modules. What exactly doesn't work? Do you get any exceptions?
Posted 23 Jun, 2016 09:57:40 Top
Aleksandra Grosset




Posts: 8
Joined: 2016-06-01
No, just does not work at all...

I get no Exceptions on hWnd3.attachEvent("onscroll", this.onscroll); (or onmessage attache)
but also no "HandleEvent scroll" Exception, when i'm scrolling (etc):

public class Window3OnScroll_EventListener
{
IEToolbarModule module;

internal Window3OnScroll_EventListener(IEToolbarModule module)
{
this.module = module;
}

[DispId(0)]
public void HandleEvent(object pEvtObj)
{
throw new Exception("HandleEvent scroll");
(...)
Posted 23 Jun, 2016 10:40:13 Top
Aleksandra Grosset




Posts: 8
Joined: 2016-06-01
Now it works!

mshtml.IHTMLWindow2 hWnd2 = HTMLDocument.parentWindow as mshtml.IHTMLWindow2;
mshtml.IHTMLWindow3 hWnd3 = hWnd2.window as mshtml.IHTMLWindow3;
Posted 24 Jun, 2016 05:00:54 Top
Andrei Smolin


Add-in Express team


Posts: 14140
Joined: 2006-05-11
Thank you for letting us know!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 24 Jun, 2016 05:03:18 Top
Aleksandra Grosset




Posts: 8
Joined: 2016-06-01
Do You know, how to recive the pstMessage data?

window.postMessage({ type: "to-page-bgp", action: "get-process-list"}, "*");


how can I get action, or type of message?
Posted 24 Jun, 2016 06:43:05 Top
Aleksandra Grosset




Posts: 8
Joined: 2016-06-01
I can recive window.postMessage("get-process-list", "*");


mshtml.IHTMLEventObj5 event_5 = event_ as mshtml.IHTMLEventObj5;
MessageBox.Show(this.module, "OnMessage handler" + event_5.data);

when event_5.data is a string value, what to do if its a object (string object Object)
Posted 24 Jun, 2016 07:11:48 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Aleksandra,

If an object implements the IDispatch interface, you can use the later binding to invoke public methods of the object.

E.g.
obj.GetType().InvokeMember("Method name", System.Reflection.BindingFlags.InvokeMethod, null, object, new object[] { arg1,... agrX} );

Or just serialize the object to a string and pass it via the postMessage function.
Posted 24 Jun, 2016 10:11:48 Top