Embedded Web Browser阻止弹出广告、窗口等技术
- 检测弹出窗口是不是自动弹出广告
在TEmbeddedWB控件的OnStatusTextChange事件处理函数中,保存Text参数。如果新窗口不是自动弹出而是由用户点击链接弹出,该参数中会保存新窗口中页面的URL。于是可以通过比较URL来进行判断。
procedure TForm1.wbMainStatusTextChange (ASender: TObject; const Text: WideString); begin mStatusText := Text; end;
- 阻止默认会弹出的新窗口
在TEmbeddedWB控件的OnNewWindow3事件触发的函数中,临时取消新窗口的创建,在判断新窗口不是广告以后,由当前窗口中的浏览器控件访问弹出的网页,从而阻止了网页的弹出。
procedure TForm1.wbMainNewWindow3 (ASender: TObject; var ppDisp: IDispatch; var Cancel: WordBool; dwFlags: Cardinal; const bstrUrlContext, bstrUrl: WideString); begin Cancel:=true; if (bstrUrl = mStatusText) then//用户点击 begin wbMain.Navigate(bstrUrl); end; end;
- 判断整个网页已读取完毕(排除frame/iframe的影响)
当TEmbeddedWB控件的OnDocumentComplete事件触发时,说明网页已经读取完成;但是如果网页中含有frame/iframe元素,在每个frame/iframe中的网页读取完成时都会触发OnDocumentComplete事件。为了排除这些frame/iframe的干扰,在OnDocumentComplete事件触发时,需要增加一个判断,即判断是不是整个网页完成。
procedure TForm1.wbMainDocumentComplete (ASender: TObject; const pDisp: IDispatch; var URL: OleVariant); begin if (wbMain.Application = pDisp) then begin // TODO: 此处添加需要的代码 end; end;