随着IE11的出现,IHTMLWindow2::execScript()
不再受欢迎。推荐的方法是 instead。我正在通过它的C++ COM接口自动化IE,我一直无法找到如何实现这一点。有人能给我指一下我在搜索中明显错过的例子吗?如果无法通过eval
执行代码,那么在Internet的运行实例中注入JavaScript代码的适当方法是什么,因为execScript
不再可用了?
编辑:为我正在处理的项目工作的任何解决方案都必须在过程之外工作。我不使用浏览器帮助对象(BHO),或任何类型的IE插件。因此,任何涉及不能正确封送跨进程的接口的解决方案都不会对我有效。
发布于 2013-08-21 05:35:30
现在,我已经验证了eval
方法与IE9、IE10和IE11的一致性(跳过错误检查以避免错误):
CComVariant result;
CComDispatchDriver disp = m_htmlWindow; // of IHTMLWindow2
disp.Invoke1(L"eval", &CComVariant(L"confirm('See this?')"), &result);
result.ChangeType(VT_BSTR);
MessageBoxW(V_BSTR(&result));
感觉甚至比execScript
更好,因为它实际上返回了result
。它也适用于C#和WinForms‘WebBrowser
var result = webBrowser1.Document.InvokeScript("eval", new object[] { "confirm('see this?')" });
MessageBox.Show(result.ToString());
尽管如此,execScript
仍然适用于IE11预览:
CComVariant result;
m_htmlWindow->execScript(CComBSTR(L"confirm('See this too?')"), CComBSTR(L"JavaScript"), &result);
result.ChangeType(VT_BSTR);
MessageBoxW(V_BSTR(&result));
它仍然抛弃了result
,就像以前一样。
有点离题,但你不必在这方面坚持使用eval
。这种方法允许在加载页面的JavaScript window
对象的命名空间内执行任何可用的命名方法(通过IDispatch接口)。您可以调用自己的函数并将一个活动的COM对象传递给它,而不是一个字符串参数,例如:
// JavaScript
function AlertUser(user)
{
alert(user.name);
return user.age;
}
// C++
CComDispatchDriver disp = m_htmlWindow; // of IHTMLWindow2
disp.Invoke1(L"AlertUser", &CComVariant(userObject), &result);
如果可能的话,我更喜欢直接打给eval
的电话。
编辑的
要使这种方法适用于进程外调用,需要进行一些调整。正如@JimEvans在评论中指出的那样,Invoke
返回了错误0x80020006 (“未知名称”)。然而,test HTA app工作得很好,所以我想尝试使用IDispatchEx::GetDispId来进行名称解析。这确实有效(跳过错误检查):
CComDispatchDriver dispWindow;
htmlWindow->QueryInterface(&dispWindow);
CComPtr<IDispatchEx> dispexWindow;
htmlWindow->QueryInterface(&dispexWindow);
DISPID dispidEval = -1;
dispexWindow->GetDispID(CComBSTR("eval"), fdexNameCaseSensitive, &dispidEval);
dispWindow.Invoke1(dispidEval, &CComVariant("function DoAlert(text) { alert(text); }")); // inject
DISPID dispidDoAlert = -1;
dispexWindow->GetDispID(CComBSTR("DoAlert"), fdexNameCaseSensitive, &dispidDoAlert) );
dispWindow.Invoke1(dispidDoAlert, &CComVariant("Hello, World!")); // call
完整的C++测试应用程序在这里:http://pastebin.com/ccZr0cG2
更新
此更新将在子__execScript
的window
对象上创建iframe
方法.对要注入的代码进行了优化,以返回目标window
对象供以后使用(不需要进行一系列的程序外调用以获得iframe
对象,而是在主窗口的上下文中完成):
CComBSTR __execScriptCode(L"(window.__execScript = function(exp) { return eval(exp); }, window.self)");
下面是C++控制台应用程序(pastebin)的代码,为了方便起见,跳过了一些错误检查。还有一个相应的prototype in .HTA,它更具可读性。
//
// http://stackoverflow.com/questions/18342200/how-do-i-call-eval-in-ie-from-c/18349546//
//
#include <tchar.h>
#include <ExDisp.h>
#include <mshtml.h>
#include <dispex.h>
#include <atlbase.h>
#include <atlcomcli.h>
#define _S(a) \
{ HRESULT hr = (a); if (FAILED(hr)) return hr; }
#define disp_cast(disp) \
((CComDispatchDriver&)(void(static_cast<IDispatch*>(disp)), reinterpret_cast<CComDispatchDriver&>(disp)))
struct ComInit {
ComInit() { ::CoInitialize(NULL); }
~ComInit() { CoUninitialize(); }
};
int _tmain(int argc, _TCHAR* argv[])
{
ComInit comInit;
CComPtr<IWebBrowser2> ie;
_S( ie.CoCreateInstance(L"InternetExplorer.Application", NULL, CLSCTX_LOCAL_SERVER) );
_S( ie->put_Visible(VARIANT_TRUE) );
CComVariant ve;
_S( ie->Navigate2(&CComVariant(L"http://jsfiddle.net/"), &ve, &ve, &ve, &ve) );
// wait for page to finish loading
for (;;)
{
Sleep(250);
READYSTATE rs = READYSTATE_UNINITIALIZED;
ie->get_ReadyState(&rs);
if ( rs == READYSTATE_COMPLETE )
break;
}
// inject __execScript into the main window
CComPtr<IDispatch> dispDoc;
_S( ie->get_Document(&dispDoc) );
CComPtr<IHTMLDocument2> htmlDoc;
_S( dispDoc->QueryInterface(&htmlDoc) );
CComPtr<IHTMLWindow2> htmlWindow;
_S( htmlDoc->get_parentWindow(&htmlWindow) );
CComPtr<IDispatchEx> dispexWindow;
_S( htmlWindow->QueryInterface(&dispexWindow) );
CComBSTR __execScript("__execScript");
CComBSTR __execScriptCode(L"(window.__execScript = function(exp) { return eval(exp); }, window.self)");
DISPID dispid = -1;
_S( dispexWindow->GetDispID(CComBSTR("eval"), fdexNameCaseSensitive, &dispid) );
_S( disp_cast(dispexWindow).Invoke1(dispid, &CComVariant(__execScriptCode)) );
// inject __execScript into the child frame
WCHAR szCode[1024];
wsprintfW(szCode, L"document.all.tags(\"iframe\")[0].contentWindow.eval(\"%ls\")", __execScriptCode.m_str);
dispid = -1;
_S( dispexWindow->GetDispID(__execScript, fdexNameCaseSensitive, &dispid) );
CComVariant vIframe;
_S( disp_cast(dispexWindow).Invoke1(dispid, &CComVariant(szCode), &vIframe) ); // inject __execScript and return the iframe's window object
_S( vIframe.ChangeType(VT_DISPATCH) );
CComPtr<IDispatchEx> dispexIframe;
_S( V_DISPATCH(&vIframe)->QueryInterface(&dispexIframe) );
dispid = -1;
_S( dispexIframe->GetDispID(__execScript, fdexNameCaseSensitive, &dispid) );
_S( disp_cast(dispexIframe).Invoke1(dispid, &CComVariant("alert(document.URL)")) ); // call the code inside child iframe
return 0;
}
https://stackoverflow.com/questions/18342200
复制相似问题