سؤال

On web button there is a button executes JS windows.postmessage and send some data. In C# bho I want to capture this data and call some local service. I need clues in how to do this.

  1. Is there ay we can directly handle this message event in C# ? How would it be?
  2. Can we call BHO method from javascript. So we can inject this event handler in web page which will call bho function. This is something if we cannot capture windows post message event in C#.
هل كانت مفيدة؟

المحلول

  1. You cannot directly work with java script function form C#. However you could intercept button click call and execute some of your code
  2. Yes, you need to publish your own COM object to the page. Then injected scripts can use this object to make callback to the BHO. Not sure how to exactly make it with C# though, no code samples exist. C++ sample function to inject COM object is below, hope it helps

.

HRESULT AddProperty(SHDocVw::IWebBrowser2* pBrowser, _bstr_t bstrName, _variant_t varValue)
{
    CComQIPtr<IHTMLDocument> spDoc = pBrowser->Document;
    CComPtr<IDispatch> spScript;
    spDoc->get_Script(&spScript);

    _variant_t varResult;
    HRESULT hr = spScript.GetPropertyByName(OLESTR("window"), varResult.GetAddress());
    CComQIPtr<IDispatchEx> spScriptEx = varResult;

    DISPID id;
    hr = spScriptEx->GetDispID(bstrName), fdexNameEnsure, &id);
    if (SUCCEEDED(hr))
    {
        DISPID propid = DISPID_PROPERTYPUT;
        DISPPARAMS dp = {NULL, NULL, 1, 1};
        dp.rgdispidNamedArgs = &propid;
        dp.rgvarg = &varValue;

        hr = spScriptEx->InvokeEx(id, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, &dp, NULL, NULL, NULL);
    }
    return hr;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top