- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 BHO 从 IE 11 的 Dom 中删除一行 Javascript。 (Internet Explorer 附加组件)
这方面的记录非常糟糕,很难找到最好的前进方向。
我已经设法用 C++ ATL/COM 编写了 BHO,并且它工作正常,但我无法找到从正文中实际删除/替换文本然后将更改注入(inject)回页面的最佳方法。
老实说,我没有时间阅读这本 1000 页的过时 COM 书 :-)。
这是我目前为 OnDocumentComplete 事件准备的内容:
void STDMETHODCALLTYPE CMyFooBHO::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
{
BSTR bstrURL = pvarURL->bstrVal;
if (_wcsicmp(bstrURL, ABOUT_BLANK) == 0)
{
return;
}
HRESULT hr = S_OK;
// Query for the IWebBrowser2 interface.
CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
// Is this event associated with the top-level browser?
if (spTempWebBrowser && m_spWebBrowser && m_spWebBrowser.IsEqualObject(spTempWebBrowser))
{
// Get the current document object from browser.
CComPtr<IDispatch> spDispDoc;
hr = m_spWebBrowser->get_Document(&spDispDoc);
if (SUCCEEDED(hr))
{
// Verify that what we get is a pointer to a IHTMLDocument2 interface.
// To be sure, let's query for the IHTMLDocument2 interface (through smart pointers).
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML;
spHTML = spDispDoc;
// Extract the source of the document if its HTML.
if (spHTML)
{
// Get the BODY object.
CComPtr<IHTMLElement> m_pBody;
hr = spHTML->get_body(&m_pBody);
if (SUCCEEDED(hr))
{
// Get the HTML text.
BSTR bstrHTMLText;
hr = m_pBody->get_outerHTML(&bstrHTMLText);
if (SUCCEEDED(hr))
{
// bstrHTMLText now contains the <body> ...whatever... </body> of the html page.
// ******** HERE ********
// What I want to do here is replace some text contained in bstrHTMLText
// i.e. Replace "ABC" with "DEF" if it exists in bstrHTMLText.
// Then replace the body of the original page with the edited bstrHTMLText.
// My actual goal is to remove one line of javascript.
}
}
}
}
}
}
请随时评论对现有代码的任何改进。
最佳答案
这不符合正常(应该做)的做法。
如果没有更好的答案,那么我认为它是最佳答案,我将其标记为最佳答案。
我很乐意听到任何改进意见或更新,或者向我展示一个更好的工作示例。
这是针对 IE 11 并在 Visual Studio 2015 中使用 C++ ATL/COM 编译的。
我已经尝试迭代 DOM 并更改它以及所有其他记录非常糟糕的变体。
阅读 html 似乎从来没有问题,即 get_innerText get_innerHTML get_outerHTML in它的形式多种多样,但 put_*** 似乎从来没有在大多数情况下起作用。为什么?似乎没有人能说或给我一个有效的例子。
我确实发现 get_body > get_innerHTML > put_innerHTML 确实有效。
所以找到这个我只是写了一个函数来在 CComBSTR 中搜索和替换。
这对我有用,但我想你可以将返回的内容作为主体内部 HTML 并运行其他一些如果您的要求不同,可以在其上使用 DOM 操作代码(不是内置的东西)。
这种做事方式的主要优点是不依赖于似乎有效的 c**p 未记录代码在 MS 想要的时候以某种神秘的方法。
这是测试 html 页面。我试图删除“警报(“你好”)”当页面完成加载时执行。
<!doctype html>
<head>
<title>Site</title>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
</head>
<body>
<div>If a dialog with hello appears then the BHO failed</div>
<script type="text/javascript">
window.onload = function(){
window.document.body.onload = foo;
};
function foo()
{
alert("hello");
}
</script>
</body>
<html>
// FooBHO.h : Declaration of the CFooBHO
#pragma once
#include "resource.h" // main symbols
#include "FooIEAddOn_i.h"
#include <shlguid.h> // IID_IWebBrowser2, DIID_DWebBrowserEvents2, etc.
#include <exdispid.h> // DISPID_DOCUMENTCOMPLETE, etc.
#include <mshtml.h> // DOM interfaces
#include <string>
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif
#define DISPID_DOCUMENTRELOAD 282
using namespace ATL;
using namespace std;
// CFooBHO
class ATL_NO_VTABLE CFooBHO : public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CFooBHO, &CLSID_FooBHO>,
public IObjectWithSiteImpl<CFooBHO>,
public IDispatchImpl<IFooBHO, &IID_IFooBHO, &LIBID_FooIEAddOnLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
public IDispEventImpl<1, CFooBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>
{
public:
CFooBHO()
{
}
// The STDMETHOD macro is an ATL convention that marks the method as virtual and ensures that it has the right calling convention for the public
// COM interface.It helps to demarcate COM interfaces from other public methods that may exist on the class.The STDMETHODIMP macro is likewise used
// when implementing the member method.
STDMETHOD(SetSite)(IUnknown *pUnkSite);
DECLARE_REGISTRY_RESOURCEID(IDR_FooBHO)
DECLARE_NOT_AGGREGATABLE(CFooBHO)
BEGIN_COM_MAP(CFooBHO)
COM_INTERFACE_ENTRY(IFooBHO)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_SINK_MAP(CFooBHO)
SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
END_SINK_MAP()
void STDMETHODCALLTYPE OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL);
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
private:
CComPtr<IWebBrowser2> m_spWebBrowser;
BOOL m_fAdvised;
static const wchar_t* ABOUT_BLANK;
void CFooBHO::ReplaceInCComBSTR(CComBSTR &strInput, const wstring &strOld, const wstring &strNew);
};
OBJECT_ENTRY_AUTO(__uuidof(FooBHO), CFooBHO)
// FooBHO.cpp : Implementation of CFooBHO
#include "stdafx.h"
#include "FooBHO.h"
#include "Strsafe.h"
const wchar_t* CFooBHO::ABOUT_BLANK = L"about:blank";
// The SetSite() method is where the BHO is initialized and where you would perform all the tasks that happen only
// once. When you navigate to a URL with Internet Explorer, you should wait for a couple of events to make sure the
// required document has been completely downloaded and then initialized. Only at this point can you safely access
// its content through the exposed object model, if any.
STDMETHODIMP CFooBHO::SetSite(IUnknown* pUnkSite)
{
if (pUnkSite != NULL)
{
// Cache the pointer to IWebBrowser2.
HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&m_spWebBrowser);
if (SUCCEEDED(hr))
{
// Register to sink events from DWebBrowserEvents2.
hr = DispEventAdvise(m_spWebBrowser);
if (SUCCEEDED(hr))
{
m_fAdvised = TRUE;
}
}
}
else
{
// Unregister event sink.
if (m_fAdvised)
{
DispEventUnadvise(m_spWebBrowser);
m_fAdvised = FALSE;
}
// Release cached pointers and other resources here.
m_spWebBrowser.Release();
}
// Call base class implementation.
return IObjectWithSiteImpl<CFooBHO>::SetSite(pUnkSite);
}
void STDMETHODCALLTYPE CFooBHO::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
{
BSTR bstrURL = pvarURL->bstrVal;
// Test for any specific URL here.
// Currently we are ignoring ABOUT:BLANK but allowing everything else.
if (_wcsicmp(bstrURL, ABOUT_BLANK) == 0)
{
return;
}
HRESULT hr = S_OK;
// Query for the IWebBrowser2 interface.
CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
// Is this event associated with the top-level browser?
if (spTempWebBrowser && m_spWebBrowser && m_spWebBrowser.IsEqualObject(spTempWebBrowser))
{
// Get the current document object from browser.
CComPtr<IDispatch> spDispDoc;
if (SUCCEEDED(m_spWebBrowser->get_Document(&spDispDoc)))
{
// Verify that what we get is a pointer to a IHTMLDocument2 interface.
// To be sure, let's query for the IHTMLDocument2 interface (through smart pointers).
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTMLDocument2 = spDispDoc;
// Extract the source of the document if its HTML.
if (spHTMLDocument2)
{
// Get the BODY object.
CComPtr<IHTMLElement> spBody;
if (SUCCEEDED(spHTMLDocument2->get_body(&spBody)))
{
// Get the Body HTML text.
CComBSTR bstrBodyHTMLText;
if (SUCCEEDED(spBody->get_innerHTML(&bstrBodyHTMLText)))
{
ReplaceInCComBSTR(bstrBodyHTMLText, L"alert(\"hello\");", L"");
spBody->put_innerHTML(bstrBodyHTMLText);
}
}
}
}
}
}
void CFooBHO::ReplaceInCComBSTR(CComBSTR &bstrInput, const wstring &strOld, const wstring &strNew)
{
wstring strOutput(bstrInput);
size_t iPos = 0;
size_t iLpos = 0;
while ((iPos = strOutput.find(strOld, iLpos)) != string::npos)
{
strOutput.replace(iPos, strOld.length(), strNew);
iLpos = iPos + 1;
}
::SysFreeString(bstrInput.m_str);
// Find and replace is complete; now update the CComBSTR.
bstrInput.m_str = ::SysAllocString(strOutput.c_str());
}
关于c++ - IE Explore 11 < c++ ATL COM Browser Helper Object (Add-on) 替换 DOM 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41214543/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!