我试图阻止由 TEmbeddedWB 或 TWebBrowser(或 TCppWebBrowser)加载的任何外部内容。我想阻止从互联网加载的任何内容,包括图像、javascript、外部 CSS、外部 [embed] 或 [object] 或 [applet] 或 [frame] 或 [iframe]、执行可以加载外部内容的 JavaScript 等。
这个问题由2部分组成:
- 将网络浏览器设置为“限制所有”(除了没有图像的基本 HTML)并检测此类内容是否存在
- 如果外部内容不存在,则正常,如果存在,则显示“下载栏”,单击后将网络浏览器置于“全部下载”模式并获取所有内容。
第一项有问题。在 TEmbeddedWB 中,您可以使用 DownloadOptions 开关阻止几乎所有内容,最重要的是 ForceOffline 开关,但即使所有这些都关闭,它仍然会通过一些东西,例如[object]
or [iframe]
标签。我知道是这种情况,因为我实现了 OnBeforeNavigate2 事件,它会触发这些标签中包含的 URL,并且还会在本地服务器的日志中创建一个条目。环境OfflineMode
and ForceOfflineMode
TEmbeddedWB 对这些项目没有帮助。
那么我怎样才能真正阻止所有呢?因此,它需要从基本 HTML 开始,并阻止外部元素(包括脚本和 CSS)。有没有办法在每次想要下载任何内容时触发一个事件,以便可以阻止它或通过阻止所有外部下载来避免首先触发此类事件?我是否需要调整 Internet Explorer 区域和安全性?任何指向正确方向的指针都会有所帮助。
第二项也很棘手,因为我需要检测是否存在有问题的标签(例如“小程序”、“脚本”、“链接”等。此检测不需要完美,但至少必须足以覆盖大多数这样的标签。我是这样做的:
//----------------------------------------------------------------------
// Check for external content (images, scripts, ActiveX, frames...)
//----------------------------------------------------------------------
try
{
bool HasExternalContent = false;
DelphiInterface<IHTMLDocument2> diDoc; // Smart pointer wrapper - should automatically call release() and do reference counting
diDoc = TEmbeddedWB->Document;
DelphiInterface<IHTMLElementCollection> diColApplets; DelphiInterface<IDispatch> diDispApplets; DelphiInterface<IHTMLObjectElement> diObj;
DelphiInterface<IHTMLElementCollection> diColEmbeds; DelphiInterface<IDispatch> diDispEmbeds;
DelphiInterface<IHTMLFramesCollection2> diColFrames; DelphiInterface<IDispatch> diDispFrames;
DelphiInterface<IHTMLElementCollection> diColImages; DelphiInterface<IDispatch> diDispImages; DelphiInterface<IHTMLImgElement> diImg;
DelphiInterface<IHTMLElementCollection> diColLinks; DelphiInterface<IDispatch> diDispLinks;
DelphiInterface<IHTMLElementCollection> diColPlugins; DelphiInterface<IDispatch> diDispPlugins;
DelphiInterface<IHTMLElementCollection> diColScripts; DelphiInterface<IDispatch> diDispScripts;
DelphiInterface<IHTMLStyleSheetsCollection> diColStyleSheets; DelphiInterface<IDispatch> diDispStyleSheets;
OleCheck(diDoc->Get_applets (diColApplets));
OleCheck(diDoc->Get_embeds (diColEmbeds));
OleCheck(diDoc->Get_frames (diColFrames));
OleCheck(diDoc->Get_images (diColImages));
OleCheck(diDoc->Get_links (diColLinks));
OleCheck(diDoc->Get_plugins (diColPlugins));
OleCheck(diDoc->Get_scripts (diColScripts));
OleCheck(diDoc->Get_styleSheets (diColStyleSheets));
// Scan for applets external links
for (int i = 0; i < diColApplets->length; i++)
{
OleCheck(diColApplets->item(i,i,diDispApplets));
if (diDispApplets != NULL)
{
diDispApplets->QueryInterface(IID_IHTMLObjectElement, (void**)&diObj);
if (diObj != NULL)
{
UnicodeString s1 = Sysutils::Trim(diObj->data),
s2 = Sysutils::Trim(diObj->codeBase),
s3 = Sysutils::Trim(diObj->classid);
if (StartsText("http", s1) || StartsText("http", s2) || StartsText("http", s3))
{
HasExternalContent = true;
break; // At least 1 found, bar will be shown, no further search needed
}
}
}
}
// Scan for images external links
for (int i = 0; i < diColImages->length; i++)
{
OleCheck(diColImages->item(i,i,diDispImages));
if (diDispImages != NULL) // Unnecessary? OleCheck throws exception if this applies?
{
diDispImages->QueryInterface(IID_IHTMLImgElement, (void**)&diImg);
if (diImg != NULL)
{
UnicodeString s1 = Sysutils::Trim(diImg->src);
// Case insensitive check
if (StartsText("http", s1))
{
HasExternalContent = true;
break; // At least 1 found, bar will be shown, no further search needed
}
}
}
}
}
catch (Exception &e)
{
// triggered by OleCheck
ShowMessage(e.Message);
}
有没有一种更简单的方法来扫描这个,或者唯一的方法是使用其他接口函数运行多个循环,例如Get_applets
, Get_embeds
, Get_stylesheets
等等类似于上面的代码?到目前为止,我发现我必须调用以下函数来涵盖所有这些:
OleCheck(diDoc->Get_applets (diColApplets));
OleCheck(diDoc->Get_embeds (diColEmbeds));
OleCheck(diDoc->Get_frames (diColFrames));
OleCheck(diDoc->Get_images (diColImages));
OleCheck(diDoc->Get_links (diColLinks));
OleCheck(diDoc->Get_plugins (diColPlugins));
OleCheck(diDoc->Get_scripts (diColScripts));
OleCheck(diDoc->Get_styleSheets (diColStyleSheets));
但如果可以更轻松地处理,我宁愿不实现那么多循环。它可以?