Cross-origin calling of Embedded Web browser pages: Embedded Web browser pages

Source: Internet
Author: User

Cross-origin calling of Embedded Web browser pages: Embedded Web browser pages

My problems:

The JS method on worker can be called normally.

Problem Analysis:
From the perspective of JS, this is a cross-domain question. host.htmland iframe.html are not in the same domain;
The JS solution to cross-origin problems is actually there, but this is not the focus of this article. The focus of this article is how to solve this "cross-origin call" problem directly through the WebBrowser control.

The root cause of the problem is:
To call JS on a webpage, you need to get the IHTMLDocument2 interface, and each iframe has its own IHTMLDocument2 interface. Therefore, you only need to get the IHTMLDocument2 corresponding to iframe to solve the problem.

Solution: directly add the code

1/* Example 2 // FileName: calljs_helper.h 3 // Creator: linyehui 5 // Brief: Call the JS function on the embedded page of The WebBrowser control, iframe can also be adjusted to 6 // 7 // $ Id: $8 // outputs */9 # ifndef _ calljs_helper_h000010 # define _ define 12 // wait 13 namespace calljs_helper14 {15 bool CallFunction (16 CComPtr <IWebBrowser2> spIWebBrowser, 17 LPCTSTR lpFuncName, 18 const vector <wstring> & paramArray, 19 CComVariant * pVarResult = NULL, 20 bool bEnumFrame = true ); 21 22} // namespace23 24 // container 25 // $ Log: $26 27 # endif/* _ CALLJS_HELPER_H __*/
1/* Example 2 // FileName: calljs_helper.cpp 3 // Creator: linyehui 5 // Brief: Call the JS function on the embedded page of The WebBrowser control, iframe can also be adjusted to 6 // 7 // $ Id: $8 // --------------------------------------------------------------------- */9 10 # include "stdafx. h "11 # include" calljs_helper.h "12 13 //------------------------------------------------------ ------------------- 14 15 CComPtr <IWebBrowser2> HtmlWindowToHtmlWebBrowser (CComPtr <IHTMLWindow2> spWindow) 16 {17 ATLASSERT (spWindow! = NULL); 18 CComQIPtr <IServiceProvider> spServiceProvider = spWindow; 19 if (spServiceProvider = NULL) 20 {21 return CComPtr <IWebBrowser2> (); 22} 23 24 CComPtr <IWebBrowser2> spWebBrws; 25 HRESULT hRes = spServiceProvider-> QueryService (IID_IWebBrowserApp, IID_IWebBrowser2, (void **) & spWebBrws); 26 if (hRes! = S_ OK) 27 {28 return CComPtr <IWebBrowser2> (); 29} 30 31 return spWebBrws; 32} 33 34 // Converts a IHTMLWindow2 object to a IHTMLDocument2. Returns NULL in case of failure. 35 // It takes into account accessing the DOM into SS frames loaded from different domains. 36 CComPtr <IHTMLDocument2> HtmlWindowToHtmlDocument (CComPtr <IHTMLWindow2> spWindow) 37 {38 ATLASSERT (spWindow! = NULL); 39 CComPtr <IHTMLDocument2> spDocument; 40 HRESULT hRes = spWindow-> get_document (& spDocument); 41 if (S_ OK = hRes) & (spDocument! = NULL) 42 {43 // The html document was properly retrieved. 44 return spDocument; 45} 46 47 // hRes cocould be E_ACCESSDENIED that means a security restriction that 48 // prevents scripting unsupported SS frames that loads documents from different internet domains. 49 CComPtr <IWebBrowser2> spBrws = HtmlWindowToHtmlWebBrowser (spWindow); 50 if (spBrws = NULL) 51 {52 return CComPtr <IHTMLDocument2> (); 53} 54 55 // Get the document object from the IWebBrowser2 object. 56 CComPtr <IDispatch> spDisp; 57 hRes = spBrws-> get_Document (& spDisp); 58 spDocument = spDisp; 59 return spDocument; 60} 61 62 bool CallFunctionInDocument (63 CComPtr <IHTMLDocument2> spDocument2, 64 LPCTSTR lpFuncName, 65 const vector <wstring> & paramArray, 66 CComVariant * pVarResult) 67 {68 if (! SpDocument2) 69 return false; 70 71 CComPtr <IDispatch> spScript; 72 if (FAILED (spDocument2-> get_Script (& spScript) {return false ;} 73 74 CComBSTR bstrMember (lpFuncName); 75 DISPID dispid = NULL; 76 HRESULT hr = spScript-> GetIDsOfNames (IID_NULL, & bstrMember, 1, LOCALE_SYSTEM_DEFAULT, & dispid ); 77 if (FAILED (hr) {return false;} 78 79 const int arraySize = paramArray. size (); 80 81 DISPPARAMS dis Pparams; 82 memset (& dispparams, 0, sizeof dispparams); 83 dispparams. cArgs = arraySize; 84 dispparams. rgvarg = new VARIANT [dispparams. cArgs]; 85 86 for (int I = 0; I <arraySize; I ++) 87 {88 CComBSTR bstr = paramArray [arraySize-1-I]. c_str (); // back reading 89 bstr. copyTo (& dispparams. rgvarg [I]. bstrVal); 90 dispparams. rgvarg [I]. vt = VT_BSTR; 91} 92 dispparams. cNamedArgs = 0; 93 94 running info e XcepInfo; 95 memset (& amp; Memory info, 0, sizeof memory info); 96 CComVariant vaResult; 97 UINT nArgErr = (UINT)-1; // initialize to invalid arg 98 99 hr = spScript-> Invoke (dispid, IID_NULL, 0, DISPATCH_METHOD, & dispparams, & vaResult, & cmdinfo, & nArgErr ); 100 101 delete [] dispparams. rgvarg; 102 if (FAILED (hr) {return false;} 103 104 if (pVarResult) {* pVarResult = vaResult;} 105 106 return true; 107} 1 08 109 void EnumFrame (110 CComPtr <IHTMLDocument2> spIHTMLDocument2, 111 LPCTSTR lpFuncName, const vector <wstring> & paramArray, 112 CComVariant * pVarResult) 113 {114 if (! SpIHTMLDocument2) 115 return; 116 117 CComPtr <IHTMLFramesCollection2> spFramesCollection2; 118 spIHTMLDocument2-> get_frames (& spFramesCollection2); 119 120 long nFrameCount = 0; 121 HRESULT hr = spFramesCollection2-> get_length (& nFrameCount); 122 if (FAILED (hr) | 0 = nFrameCount) 123 return; 124 125 for (long I = 0; I <nFrameCount; I ++) 126 {127 CComVariant vDispWin2; 128 hr = spFramesCollection2-> Item (& CComVariant (I), & vDispWin2); 129 if (FAILED (hr) 130 continue; 131 132 CComQIPtr <IHTMLWindow2> spWin2 = vDispWin2.pdispVal; 133 if (! SpWin2) 134 continue; 135 136 CComPtr <IHTMLDocument2> spDoc2; 137 spDoc2 = HtmlWindowToHtmlDocument (spWin2); 138 if (! SpDoc2) 139 continue; 140 141 CallFunctionInDocument (spDoc2, lpFuncName, paramArray, pVarResult); 142} 143 144 145 bool calljs_helper: CallFunction (146 CComPtr <IWebBrowser2> spIWebBrowser, 147 LPCTSTR lpFuncName, 148 const vector <wstring> & paramArray, 149 CComVariant * pVarResult, 150 bool bEnumFrame) 151 {152 if (! SpIWebBrowser) 153 return false; 154 155 CComPtr <IDispatch> spDispDoc; 156 HRESULT hr = spIWebBrowser-> get_Document (& spDispDoc); 157 if (FAILED (hr) 158 return false; 159 160 CComQIPtr <IHTMLDocument2> spDocument2 = spDispDoc; 161 if (! SpDocument2) 162 return false; 163 164 CallFunctionInDocument (spDocument2, lpFuncName, paramArray, pVarResult); 165 166 167 if (bEnumFrame) 168 {EnumFrame (spDocument2, lpFuncName, paramArray, pVarResult ); 169} 170 171 return true; 172} 173 174 // --------------------------------------------------------------------------- 175 // $ Log: $

 

The complete example code is downloaded here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.