我试图阻止任何外部由TEmbeddedWB或TWebBrowser(或TCppWebBrowser)加载。我想阻止从互联网加载的任何内容,包括图像,JavaScript,外部CSS,外部[嵌入]或[对象]或[小程序]或[框架]或[iframe],执行可加载外部内容的JavaScript等。检测外部内容与TEmbeddedWB或TWebBrowser
此问题由两个部分组成:
- 把Web浏览器到“限制”的报告(除了没有图像基本的HTML),并且检测是否这样的内容存在
- 如果外部内容不存在确定的,如果它显示一个“下载栏”,点击后将网页浏览器放入“全部下载”模式并获取所有内容。
第一个项目的问题。在TEmbeddedWB中,您可以使用DownloadOptions开关阻止几乎任何事情,最重要的是ForceOffline开关,但即使关闭所有这些,仍会通过[object]
或[iframe]
标签。我知道这是因为我实现了OnBeforeNavigate2事件,它触发了这些标记中包含的URL,并且它还在本地服务器的日志中创建了一个条目。在TEmbeddedWB中设置OfflineMode
和ForceOfflineMode
对这些项目没有帮助。
那么我怎么能真正阻止所有?因此,它需要以阻止的外部元素(包括脚本和CSS)作为基本HTML开始。有没有办法在每次下载任何事件时触发事件,以阻止所有外部下载,从而阻止或避免触发此类事件?我需要摆弄Internet Explorer区域和安全吗?正确方向的任何指针都会有所帮助。
第二项也很棘手,因为我需要检测是否存在问题标签(例如“applet”,“脚本”,“链接”等。这种检测不需要是完美的,但它至少必须是。好到足以覆盖大部分此类标签的我已经做了这样的:
//----------------------------------------------------------------------
// 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));
但我宁愿没有实现,很多循环是否可以这样处理更容易。它可以?
句子*我想阻止任何从Internet加载的内容*表示您想要离线模式吗?如果是这样,只需将'TWebBrowser.Offline'设置为True ;-) – TLama
如果它非常简单,那将会很棒。我设置了Offline和ForceOffline标志,但它仍然会加载一些外部内容,例如[object]标签或某些框架。 – Coder12345
你的HTML有多复杂?你需要Internet Explorer吗? – Pol