总结:如何让下载的第三方网页执行本地脚本(利用CHtmlView系列控件)
2009-07-19 13:17
856 查看
如下给网页增加脚本,然后调用就可以了
其中GetElementByHPath函数可能没有,目的是找到body元素。
HRESULT AppendJavaScript(IDispatch* pDocument, LPCTSTR lpszText, bool bIsSrc)
{
CComQIPtr<IHTMLDocument2> pDocument2 = pDocument;
if (pDocument2 == NULL || lpszText == NULL || *lpszText == 0)
return E_FAIL;
CComQIPtr<IHTMLElement> pElement;
CComQIPtr<IHTMLScriptElement> pScriptElement;
pDocument2->createElement(CComBSTR("script"), &pElement);
pScriptElement = pElement;
if (pScriptElement == NULL)
return E_FAIL;
pScriptElement->put_type(L"text/ecmascript");
pScriptElement->put_defer(VARIANT_TRUE);
if (bIsSrc)
pScriptElement->put_src(_bstr_t(lpszText));
else
pScriptElement->put_text(_bstr_t(lpszText));
CComQIPtr<IHTMLElement> pBody = GetElementByHPath(pDocument2, L"TagName=body");
CComQIPtr<IHTMLDOMNode> pBodyNode = pBody;
CComQIPtr<IHTMLDOMNode> pNewNode;
HRESULT hr = pBodyNode->appendChild(CComQIPtr<IHTMLDOMNode>(pElement), &pNewNode);
return hr;
}
其中GetElementByHPath函数可能没有,目的是找到body元素。
HRESULT AppendJavaScript(IDispatch* pDocument, LPCTSTR lpszText, bool bIsSrc)
{
CComQIPtr<IHTMLDocument2> pDocument2 = pDocument;
if (pDocument2 == NULL || lpszText == NULL || *lpszText == 0)
return E_FAIL;
CComQIPtr<IHTMLElement> pElement;
CComQIPtr<IHTMLScriptElement> pScriptElement;
pDocument2->createElement(CComBSTR("script"), &pElement);
pScriptElement = pElement;
if (pScriptElement == NULL)
return E_FAIL;
pScriptElement->put_type(L"text/ecmascript");
pScriptElement->put_defer(VARIANT_TRUE);
if (bIsSrc)
pScriptElement->put_src(_bstr_t(lpszText));
else
pScriptElement->put_text(_bstr_t(lpszText));
CComQIPtr<IHTMLElement> pBody = GetElementByHPath(pDocument2, L"TagName=body");
CComQIPtr<IHTMLDOMNode> pBodyNode = pBody;
CComQIPtr<IHTMLDOMNode> pNewNode;
HRESULT hr = pBodyNode->appendChild(CComQIPtr<IHTMLDOMNode>(pElement), &pNewNode);
return hr;
}
相关文章推荐
- 如何把民间的控件工具集合:android-misc-widgets,下载到本地,并跑起来!!
- 绝对酷,如何解决asp.net中javascript脚本的问题(使用服务器控件执行客户端脚本)
- 利用python fabric模块写的批量操作远程主机脚本(命令执行,上传、下载文件)
- 利用TortoiseGit将shell脚本文件从远端Gitlib库克隆到本地后脚本无法执行
- Selenium-webdriver系列教程(3)———如何执行一段js脚本
- 如何使用WebBrowser控件执行页面的JavaScript脚本,WebBrowser执行JavaScript脚本
- 利用线程下载网页中的程序并另存到本地
- 如何设置WebView使得加载网页时能够执行JavaScript脚本中的alert()函数
- [22]Window PowerShell DSC学习系列---- 如何用PowerShell脚本查看DSC服务器的执行报表(Report)
- Linux系列教程(二十)——Linux的shell概述以及如何执行脚本
- 初学shell,今天遇到由wget下载到本地的网页源代码的乱码问题,无聊的写了一个转码的脚本
- 如何利用asp保存远程网页中的图片到本地来!
- Selenium-webdriver系列教程(三)————如何执行一段js脚本
- 利用jsoup 如何从网页中下载图片
- 如何利用cron进程来自动执行(总结)
- 如何利用asp保存远程网页中的图片到本地来!
- 自动化测试 - RFT系列教程4: RFT是如何识别控件的(二) 对象库与脚本
- 网页上如何执行本地程序
- 如何利用火狐控制台下载网页图片
- 利用线程下载网页中的程序并另存到本地(转载收藏)